446 lines
14 KiB
JavaScript
446 lines
14 KiB
JavaScript
|
// https://d3js.org/d3-time/ v3.1.0 Copyright 2010-2022 Mike Bostock
|
||
|
(function (global, factory) {
|
||
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-array')) :
|
||
|
typeof define === 'function' && define.amd ? define(['exports', 'd3-array'], factory) :
|
||
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}, global.d3));
|
||
|
})(this, (function (exports, d3Array) { 'use strict';
|
||
|
|
||
|
const t0 = new Date, t1 = new Date;
|
||
|
|
||
|
function timeInterval(floori, offseti, count, field) {
|
||
|
|
||
|
function interval(date) {
|
||
|
return floori(date = arguments.length === 0 ? new Date : new Date(+date)), date;
|
||
|
}
|
||
|
|
||
|
interval.floor = (date) => {
|
||
|
return floori(date = new Date(+date)), date;
|
||
|
};
|
||
|
|
||
|
interval.ceil = (date) => {
|
||
|
return floori(date = new Date(date - 1)), offseti(date, 1), floori(date), date;
|
||
|
};
|
||
|
|
||
|
interval.round = (date) => {
|
||
|
const d0 = interval(date), d1 = interval.ceil(date);
|
||
|
return date - d0 < d1 - date ? d0 : d1;
|
||
|
};
|
||
|
|
||
|
interval.offset = (date, step) => {
|
||
|
return offseti(date = new Date(+date), step == null ? 1 : Math.floor(step)), date;
|
||
|
};
|
||
|
|
||
|
interval.range = (start, stop, step) => {
|
||
|
const range = [];
|
||
|
start = interval.ceil(start);
|
||
|
step = step == null ? 1 : Math.floor(step);
|
||
|
if (!(start < stop) || !(step > 0)) return range; // also handles Invalid Date
|
||
|
let previous;
|
||
|
do range.push(previous = new Date(+start)), offseti(start, step), floori(start);
|
||
|
while (previous < start && start < stop);
|
||
|
return range;
|
||
|
};
|
||
|
|
||
|
interval.filter = (test) => {
|
||
|
return timeInterval((date) => {
|
||
|
if (date >= date) while (floori(date), !test(date)) date.setTime(date - 1);
|
||
|
}, (date, step) => {
|
||
|
if (date >= date) {
|
||
|
if (step < 0) while (++step <= 0) {
|
||
|
while (offseti(date, -1), !test(date)) {} // eslint-disable-line no-empty
|
||
|
} else while (--step >= 0) {
|
||
|
while (offseti(date, +1), !test(date)) {} // eslint-disable-line no-empty
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
};
|
||
|
|
||
|
if (count) {
|
||
|
interval.count = (start, end) => {
|
||
|
t0.setTime(+start), t1.setTime(+end);
|
||
|
floori(t0), floori(t1);
|
||
|
return Math.floor(count(t0, t1));
|
||
|
};
|
||
|
|
||
|
interval.every = (step) => {
|
||
|
step = Math.floor(step);
|
||
|
return !isFinite(step) || !(step > 0) ? null
|
||
|
: !(step > 1) ? interval
|
||
|
: interval.filter(field
|
||
|
? (d) => field(d) % step === 0
|
||
|
: (d) => interval.count(0, d) % step === 0);
|
||
|
};
|
||
|
}
|
||
|
|
||
|
return interval;
|
||
|
}
|
||
|
|
||
|
const millisecond = timeInterval(() => {
|
||
|
// noop
|
||
|
}, (date, step) => {
|
||
|
date.setTime(+date + step);
|
||
|
}, (start, end) => {
|
||
|
return end - start;
|
||
|
});
|
||
|
|
||
|
// An optimized implementation for this simple case.
|
||
|
millisecond.every = (k) => {
|
||
|
k = Math.floor(k);
|
||
|
if (!isFinite(k) || !(k > 0)) return null;
|
||
|
if (!(k > 1)) return millisecond;
|
||
|
return timeInterval((date) => {
|
||
|
date.setTime(Math.floor(date / k) * k);
|
||
|
}, (date, step) => {
|
||
|
date.setTime(+date + step * k);
|
||
|
}, (start, end) => {
|
||
|
return (end - start) / k;
|
||
|
});
|
||
|
};
|
||
|
|
||
|
const milliseconds = millisecond.range;
|
||
|
|
||
|
const durationSecond = 1000;
|
||
|
const durationMinute = durationSecond * 60;
|
||
|
const durationHour = durationMinute * 60;
|
||
|
const durationDay = durationHour * 24;
|
||
|
const durationWeek = durationDay * 7;
|
||
|
const durationMonth = durationDay * 30;
|
||
|
const durationYear = durationDay * 365;
|
||
|
|
||
|
const second = timeInterval((date) => {
|
||
|
date.setTime(date - date.getMilliseconds());
|
||
|
}, (date, step) => {
|
||
|
date.setTime(+date + step * durationSecond);
|
||
|
}, (start, end) => {
|
||
|
return (end - start) / durationSecond;
|
||
|
}, (date) => {
|
||
|
return date.getUTCSeconds();
|
||
|
});
|
||
|
|
||
|
const seconds = second.range;
|
||
|
|
||
|
const timeMinute = timeInterval((date) => {
|
||
|
date.setTime(date - date.getMilliseconds() - date.getSeconds() * durationSecond);
|
||
|
}, (date, step) => {
|
||
|
date.setTime(+date + step * durationMinute);
|
||
|
}, (start, end) => {
|
||
|
return (end - start) / durationMinute;
|
||
|
}, (date) => {
|
||
|
return date.getMinutes();
|
||
|
});
|
||
|
|
||
|
const timeMinutes = timeMinute.range;
|
||
|
|
||
|
const utcMinute = timeInterval((date) => {
|
||
|
date.setUTCSeconds(0, 0);
|
||
|
}, (date, step) => {
|
||
|
date.setTime(+date + step * durationMinute);
|
||
|
}, (start, end) => {
|
||
|
return (end - start) / durationMinute;
|
||
|
}, (date) => {
|
||
|
return date.getUTCMinutes();
|
||
|
});
|
||
|
|
||
|
const utcMinutes = utcMinute.range;
|
||
|
|
||
|
const timeHour = timeInterval((date) => {
|
||
|
date.setTime(date - date.getMilliseconds() - date.getSeconds() * durationSecond - date.getMinutes() * durationMinute);
|
||
|
}, (date, step) => {
|
||
|
date.setTime(+date + step * durationHour);
|
||
|
}, (start, end) => {
|
||
|
return (end - start) / durationHour;
|
||
|
}, (date) => {
|
||
|
return date.getHours();
|
||
|
});
|
||
|
|
||
|
const timeHours = timeHour.range;
|
||
|
|
||
|
const utcHour = timeInterval((date) => {
|
||
|
date.setUTCMinutes(0, 0, 0);
|
||
|
}, (date, step) => {
|
||
|
date.setTime(+date + step * durationHour);
|
||
|
}, (start, end) => {
|
||
|
return (end - start) / durationHour;
|
||
|
}, (date) => {
|
||
|
return date.getUTCHours();
|
||
|
});
|
||
|
|
||
|
const utcHours = utcHour.range;
|
||
|
|
||
|
const timeDay = timeInterval(
|
||
|
date => date.setHours(0, 0, 0, 0),
|
||
|
(date, step) => date.setDate(date.getDate() + step),
|
||
|
(start, end) => (end - start - (end.getTimezoneOffset() - start.getTimezoneOffset()) * durationMinute) / durationDay,
|
||
|
date => date.getDate() - 1
|
||
|
);
|
||
|
|
||
|
const timeDays = timeDay.range;
|
||
|
|
||
|
const utcDay = timeInterval((date) => {
|
||
|
date.setUTCHours(0, 0, 0, 0);
|
||
|
}, (date, step) => {
|
||
|
date.setUTCDate(date.getUTCDate() + step);
|
||
|
}, (start, end) => {
|
||
|
return (end - start) / durationDay;
|
||
|
}, (date) => {
|
||
|
return date.getUTCDate() - 1;
|
||
|
});
|
||
|
|
||
|
const utcDays = utcDay.range;
|
||
|
|
||
|
const unixDay = timeInterval((date) => {
|
||
|
date.setUTCHours(0, 0, 0, 0);
|
||
|
}, (date, step) => {
|
||
|
date.setUTCDate(date.getUTCDate() + step);
|
||
|
}, (start, end) => {
|
||
|
return (end - start) / durationDay;
|
||
|
}, (date) => {
|
||
|
return Math.floor(date / durationDay);
|
||
|
});
|
||
|
|
||
|
const unixDays = unixDay.range;
|
||
|
|
||
|
function timeWeekday(i) {
|
||
|
return timeInterval((date) => {
|
||
|
date.setDate(date.getDate() - (date.getDay() + 7 - i) % 7);
|
||
|
date.setHours(0, 0, 0, 0);
|
||
|
}, (date, step) => {
|
||
|
date.setDate(date.getDate() + step * 7);
|
||
|
}, (start, end) => {
|
||
|
return (end - start - (end.getTimezoneOffset() - start.getTimezoneOffset()) * durationMinute) / durationWeek;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
const timeSunday = timeWeekday(0);
|
||
|
const timeMonday = timeWeekday(1);
|
||
|
const timeTuesday = timeWeekday(2);
|
||
|
const timeWednesday = timeWeekday(3);
|
||
|
const timeThursday = timeWeekday(4);
|
||
|
const timeFriday = timeWeekday(5);
|
||
|
const timeSaturday = timeWeekday(6);
|
||
|
|
||
|
const timeSundays = timeSunday.range;
|
||
|
const timeMondays = timeMonday.range;
|
||
|
const timeTuesdays = timeTuesday.range;
|
||
|
const timeWednesdays = timeWednesday.range;
|
||
|
const timeThursdays = timeThursday.range;
|
||
|
const timeFridays = timeFriday.range;
|
||
|
const timeSaturdays = timeSaturday.range;
|
||
|
|
||
|
function utcWeekday(i) {
|
||
|
return timeInterval((date) => {
|
||
|
date.setUTCDate(date.getUTCDate() - (date.getUTCDay() + 7 - i) % 7);
|
||
|
date.setUTCHours(0, 0, 0, 0);
|
||
|
}, (date, step) => {
|
||
|
date.setUTCDate(date.getUTCDate() + step * 7);
|
||
|
}, (start, end) => {
|
||
|
return (end - start) / durationWeek;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
const utcSunday = utcWeekday(0);
|
||
|
const utcMonday = utcWeekday(1);
|
||
|
const utcTuesday = utcWeekday(2);
|
||
|
const utcWednesday = utcWeekday(3);
|
||
|
const utcThursday = utcWeekday(4);
|
||
|
const utcFriday = utcWeekday(5);
|
||
|
const utcSaturday = utcWeekday(6);
|
||
|
|
||
|
const utcSundays = utcSunday.range;
|
||
|
const utcMondays = utcMonday.range;
|
||
|
const utcTuesdays = utcTuesday.range;
|
||
|
const utcWednesdays = utcWednesday.range;
|
||
|
const utcThursdays = utcThursday.range;
|
||
|
const utcFridays = utcFriday.range;
|
||
|
const utcSaturdays = utcSaturday.range;
|
||
|
|
||
|
const timeMonth = timeInterval((date) => {
|
||
|
date.setDate(1);
|
||
|
date.setHours(0, 0, 0, 0);
|
||
|
}, (date, step) => {
|
||
|
date.setMonth(date.getMonth() + step);
|
||
|
}, (start, end) => {
|
||
|
return end.getMonth() - start.getMonth() + (end.getFullYear() - start.getFullYear()) * 12;
|
||
|
}, (date) => {
|
||
|
return date.getMonth();
|
||
|
});
|
||
|
|
||
|
const timeMonths = timeMonth.range;
|
||
|
|
||
|
const utcMonth = timeInterval((date) => {
|
||
|
date.setUTCDate(1);
|
||
|
date.setUTCHours(0, 0, 0, 0);
|
||
|
}, (date, step) => {
|
||
|
date.setUTCMonth(date.getUTCMonth() + step);
|
||
|
}, (start, end) => {
|
||
|
return end.getUTCMonth() - start.getUTCMonth() + (end.getUTCFullYear() - start.getUTCFullYear()) * 12;
|
||
|
}, (date) => {
|
||
|
return date.getUTCMonth();
|
||
|
});
|
||
|
|
||
|
const utcMonths = utcMonth.range;
|
||
|
|
||
|
const timeYear = timeInterval((date) => {
|
||
|
date.setMonth(0, 1);
|
||
|
date.setHours(0, 0, 0, 0);
|
||
|
}, (date, step) => {
|
||
|
date.setFullYear(date.getFullYear() + step);
|
||
|
}, (start, end) => {
|
||
|
return end.getFullYear() - start.getFullYear();
|
||
|
}, (date) => {
|
||
|
return date.getFullYear();
|
||
|
});
|
||
|
|
||
|
// An optimized implementation for this simple case.
|
||
|
timeYear.every = (k) => {
|
||
|
return !isFinite(k = Math.floor(k)) || !(k > 0) ? null : timeInterval((date) => {
|
||
|
date.setFullYear(Math.floor(date.getFullYear() / k) * k);
|
||
|
date.setMonth(0, 1);
|
||
|
date.setHours(0, 0, 0, 0);
|
||
|
}, (date, step) => {
|
||
|
date.setFullYear(date.getFullYear() + step * k);
|
||
|
});
|
||
|
};
|
||
|
|
||
|
const timeYears = timeYear.range;
|
||
|
|
||
|
const utcYear = timeInterval((date) => {
|
||
|
date.setUTCMonth(0, 1);
|
||
|
date.setUTCHours(0, 0, 0, 0);
|
||
|
}, (date, step) => {
|
||
|
date.setUTCFullYear(date.getUTCFullYear() + step);
|
||
|
}, (start, end) => {
|
||
|
return end.getUTCFullYear() - start.getUTCFullYear();
|
||
|
}, (date) => {
|
||
|
return date.getUTCFullYear();
|
||
|
});
|
||
|
|
||
|
// An optimized implementation for this simple case.
|
||
|
utcYear.every = (k) => {
|
||
|
return !isFinite(k = Math.floor(k)) || !(k > 0) ? null : timeInterval((date) => {
|
||
|
date.setUTCFullYear(Math.floor(date.getUTCFullYear() / k) * k);
|
||
|
date.setUTCMonth(0, 1);
|
||
|
date.setUTCHours(0, 0, 0, 0);
|
||
|
}, (date, step) => {
|
||
|
date.setUTCFullYear(date.getUTCFullYear() + step * k);
|
||
|
});
|
||
|
};
|
||
|
|
||
|
const utcYears = utcYear.range;
|
||
|
|
||
|
function ticker(year, month, week, day, hour, minute) {
|
||
|
|
||
|
const tickIntervals = [
|
||
|
[second, 1, durationSecond],
|
||
|
[second, 5, 5 * durationSecond],
|
||
|
[second, 15, 15 * durationSecond],
|
||
|
[second, 30, 30 * durationSecond],
|
||
|
[minute, 1, durationMinute],
|
||
|
[minute, 5, 5 * durationMinute],
|
||
|
[minute, 15, 15 * durationMinute],
|
||
|
[minute, 30, 30 * durationMinute],
|
||
|
[ hour, 1, durationHour ],
|
||
|
[ hour, 3, 3 * durationHour ],
|
||
|
[ hour, 6, 6 * durationHour ],
|
||
|
[ hour, 12, 12 * durationHour ],
|
||
|
[ day, 1, durationDay ],
|
||
|
[ day, 2, 2 * durationDay ],
|
||
|
[ week, 1, durationWeek ],
|
||
|
[ month, 1, durationMonth ],
|
||
|
[ month, 3, 3 * durationMonth ],
|
||
|
[ year, 1, durationYear ]
|
||
|
];
|
||
|
|
||
|
function ticks(start, stop, count) {
|
||
|
const reverse = stop < start;
|
||
|
if (reverse) [start, stop] = [stop, start];
|
||
|
const interval = count && typeof count.range === "function" ? count : tickInterval(start, stop, count);
|
||
|
const ticks = interval ? interval.range(start, +stop + 1) : []; // inclusive stop
|
||
|
return reverse ? ticks.reverse() : ticks;
|
||
|
}
|
||
|
|
||
|
function tickInterval(start, stop, count) {
|
||
|
const target = Math.abs(stop - start) / count;
|
||
|
const i = d3Array.bisector(([,, step]) => step).right(tickIntervals, target);
|
||
|
if (i === tickIntervals.length) return year.every(d3Array.tickStep(start / durationYear, stop / durationYear, count));
|
||
|
if (i === 0) return millisecond.every(Math.max(d3Array.tickStep(start, stop, count), 1));
|
||
|
const [t, step] = tickIntervals[target / tickIntervals[i - 1][2] < tickIntervals[i][2] / target ? i - 1 : i];
|
||
|
return t.every(step);
|
||
|
}
|
||
|
|
||
|
return [ticks, tickInterval];
|
||
|
}
|
||
|
|
||
|
const [utcTicks, utcTickInterval] = ticker(utcYear, utcMonth, utcSunday, unixDay, utcHour, utcMinute);
|
||
|
const [timeTicks, timeTickInterval] = ticker(timeYear, timeMonth, timeSunday, timeDay, timeHour, timeMinute);
|
||
|
|
||
|
exports.timeDay = timeDay;
|
||
|
exports.timeDays = timeDays;
|
||
|
exports.timeFriday = timeFriday;
|
||
|
exports.timeFridays = timeFridays;
|
||
|
exports.timeHour = timeHour;
|
||
|
exports.timeHours = timeHours;
|
||
|
exports.timeInterval = timeInterval;
|
||
|
exports.timeMillisecond = millisecond;
|
||
|
exports.timeMilliseconds = milliseconds;
|
||
|
exports.timeMinute = timeMinute;
|
||
|
exports.timeMinutes = timeMinutes;
|
||
|
exports.timeMonday = timeMonday;
|
||
|
exports.timeMondays = timeMondays;
|
||
|
exports.timeMonth = timeMonth;
|
||
|
exports.timeMonths = timeMonths;
|
||
|
exports.timeSaturday = timeSaturday;
|
||
|
exports.timeSaturdays = timeSaturdays;
|
||
|
exports.timeSecond = second;
|
||
|
exports.timeSeconds = seconds;
|
||
|
exports.timeSunday = timeSunday;
|
||
|
exports.timeSundays = timeSundays;
|
||
|
exports.timeThursday = timeThursday;
|
||
|
exports.timeThursdays = timeThursdays;
|
||
|
exports.timeTickInterval = timeTickInterval;
|
||
|
exports.timeTicks = timeTicks;
|
||
|
exports.timeTuesday = timeTuesday;
|
||
|
exports.timeTuesdays = timeTuesdays;
|
||
|
exports.timeWednesday = timeWednesday;
|
||
|
exports.timeWednesdays = timeWednesdays;
|
||
|
exports.timeWeek = timeSunday;
|
||
|
exports.timeWeeks = timeSundays;
|
||
|
exports.timeYear = timeYear;
|
||
|
exports.timeYears = timeYears;
|
||
|
exports.unixDay = unixDay;
|
||
|
exports.unixDays = unixDays;
|
||
|
exports.utcDay = utcDay;
|
||
|
exports.utcDays = utcDays;
|
||
|
exports.utcFriday = utcFriday;
|
||
|
exports.utcFridays = utcFridays;
|
||
|
exports.utcHour = utcHour;
|
||
|
exports.utcHours = utcHours;
|
||
|
exports.utcMillisecond = millisecond;
|
||
|
exports.utcMilliseconds = milliseconds;
|
||
|
exports.utcMinute = utcMinute;
|
||
|
exports.utcMinutes = utcMinutes;
|
||
|
exports.utcMonday = utcMonday;
|
||
|
exports.utcMondays = utcMondays;
|
||
|
exports.utcMonth = utcMonth;
|
||
|
exports.utcMonths = utcMonths;
|
||
|
exports.utcSaturday = utcSaturday;
|
||
|
exports.utcSaturdays = utcSaturdays;
|
||
|
exports.utcSecond = second;
|
||
|
exports.utcSeconds = seconds;
|
||
|
exports.utcSunday = utcSunday;
|
||
|
exports.utcSundays = utcSundays;
|
||
|
exports.utcThursday = utcThursday;
|
||
|
exports.utcThursdays = utcThursdays;
|
||
|
exports.utcTickInterval = utcTickInterval;
|
||
|
exports.utcTicks = utcTicks;
|
||
|
exports.utcTuesday = utcTuesday;
|
||
|
exports.utcTuesdays = utcTuesdays;
|
||
|
exports.utcWednesday = utcWednesday;
|
||
|
exports.utcWednesdays = utcWednesdays;
|
||
|
exports.utcWeek = utcSunday;
|
||
|
exports.utcWeeks = utcSundays;
|
||
|
exports.utcYear = utcYear;
|
||
|
exports.utcYears = utcYears;
|
||
|
|
||
|
}));
|