-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
consistent intervals with interval.every
- Loading branch information
Showing
14 changed files
with
111 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,20 @@ | ||
import {timeInterval} from "./interval.js"; | ||
import {durationDay, durationMinute} from "./duration.js"; | ||
import {timeEpoch} from "./epoch.js"; | ||
|
||
export const timeDay = timeInterval( | ||
date => date.setHours(0, 0, 0, 0), | ||
(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 | ||
timeEpoch | ||
); | ||
|
||
export const timeDays = timeDay.range; | ||
|
||
export 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; | ||
}); | ||
export const utcDay = timeInterval( | ||
(date) => date.setUTCHours(0, 0, 0, 0), | ||
(date, step) => date.setUTCDate(date.getUTCDate() + step), | ||
(start, end) => (end - start) / durationDay | ||
); | ||
|
||
export const utcDays = utcDay.range; | ||
|
||
export 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); | ||
}); | ||
|
||
export const unixDays = unixDay.range; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const timeEpoch = new Date(1970, 0, 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,20 @@ | ||
import {timeInterval} from "./interval.js"; | ||
import {durationHour, durationMinute, durationSecond} from "./duration.js"; | ||
import {timeEpoch} from "./epoch.js"; | ||
|
||
export 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(); | ||
}); | ||
export const timeHour = timeInterval( | ||
(date) => date.setTime(date - date.getMilliseconds() - date.getSeconds() * durationSecond - date.getMinutes() * durationMinute), | ||
(date, step) => date.setTime(+date + step * durationHour), | ||
(start, end) => (end - start) / durationHour, | ||
timeEpoch | ||
); | ||
|
||
export const timeHours = timeHour.range; | ||
|
||
export 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(); | ||
}); | ||
export const utcHour = timeInterval( | ||
(date) => date.setUTCMinutes(0, 0, 0), | ||
(date, step) => date.setTime(+date + step * durationHour), | ||
(start, end) => (end - start) / durationHour | ||
); | ||
|
||
export const utcHours = utcHour.range; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,20 @@ | ||
import {timeInterval} from "./interval.js"; | ||
import {durationMinute, durationSecond} from "./duration.js"; | ||
import {timeEpoch} from "./epoch.js"; | ||
|
||
export 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(); | ||
}); | ||
export const timeMinute = timeInterval( | ||
(date) => date.setTime(date - date.getMilliseconds() - date.getSeconds() * durationSecond), | ||
(date, step) => date.setTime(+date + step * durationMinute), | ||
(start, end) => (end - start) / durationMinute, | ||
timeEpoch | ||
); | ||
|
||
export const timeMinutes = timeMinute.range; | ||
|
||
export 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(); | ||
}); | ||
export const utcMinute = timeInterval( | ||
(date) => date.setUTCSeconds(0, 0), | ||
(date, step) => date.setTime(+date + step * durationMinute), | ||
(start, end) => (end - start) / durationMinute | ||
); | ||
|
||
export const utcMinutes = utcMinute.range; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,19 @@ | ||
import {timeEpoch} from "./epoch.js"; | ||
import {timeInterval} from "./interval.js"; | ||
|
||
export 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(); | ||
}); | ||
export const timeMonth = timeInterval( | ||
(date) => (date.setDate(1), date.setHours(0, 0, 0, 0)), | ||
(date, step) => date.setMonth(date.getMonth() + step), | ||
(start, end) => end.getMonth() - start.getMonth() + (end.getFullYear() - start.getFullYear()) * 12, | ||
timeEpoch | ||
); | ||
|
||
export const timeMonths = timeMonth.range; | ||
|
||
export 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(); | ||
}); | ||
export const utcMonth = timeInterval( | ||
(date) => (date.setUTCDate(1), date.setUTCHours(0, 0, 0, 0)), | ||
(date, step) => date.setUTCMonth(date.getUTCMonth() + step), | ||
(start, end) => end.getUTCMonth() - start.getUTCMonth() + (end.getUTCFullYear() - start.getUTCFullYear()) * 12 | ||
); | ||
|
||
export const utcMonths = utcMonth.range; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,10 @@ | ||
import {timeInterval} from "./interval.js"; | ||
import {durationSecond} from "./duration.js"; | ||
|
||
export 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(); | ||
}); | ||
export const second = timeInterval( | ||
(date) => date.setTime(date - date.getMilliseconds()), | ||
(date, step) => date.setTime(+date + step * durationSecond), | ||
(start, end) => (end - start) / durationSecond | ||
); | ||
|
||
export const seconds = second.range; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,35 @@ | ||
import {timeEpoch} from "./epoch.js"; | ||
import {timeInterval} from "./interval.js"; | ||
|
||
export 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(); | ||
}); | ||
export const timeYear = timeInterval( | ||
(date) => (date.setMonth(0, 1), date.setHours(0, 0, 0, 0)), | ||
(date, step) => date.setFullYear(date.getFullYear() + step), | ||
(start, end) => end.getFullYear() - start.getFullYear(), | ||
timeEpoch | ||
); | ||
|
||
// 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); | ||
}); | ||
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) | ||
); | ||
}; | ||
|
||
export const timeYears = timeYear.range; | ||
|
||
export 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(); | ||
}); | ||
export const utcYear = timeInterval( | ||
(date) => (date.setUTCMonth(0, 1), date.setUTCHours(0, 0, 0, 0)), | ||
(date, step) => date.setUTCFullYear(date.getUTCFullYear() + step), | ||
(start, end) => end.getUTCFullYear() - start.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); | ||
}); | ||
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) | ||
); | ||
}; | ||
|
||
export const utcYears = utcYear.range; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.