@thi.ng/date 2.5.5 → 2.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/round.js CHANGED
@@ -1,135 +1,88 @@
1
1
  import { DAY, HOUR, MINUTE, SECOND, WEEK } from "./api.js";
2
2
  import { ensureDate } from "./checks.js";
3
- /**
4
- * Rounds down `epoch` to minute precision.
5
- *
6
- * @param epoch -
7
- */
8
- export const floorSecond = (epoch) => {
9
- const d = ensureDate(epoch);
10
- return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds());
3
+ const floorSecond = (epoch) => {
4
+ const d = ensureDate(epoch);
5
+ return Date.UTC(
6
+ d.getUTCFullYear(),
7
+ d.getUTCMonth(),
8
+ d.getUTCDate(),
9
+ d.getUTCHours(),
10
+ d.getUTCMinutes(),
11
+ d.getUTCSeconds()
12
+ );
11
13
  };
12
- /**
13
- * Rounds down `epoch` to minute precision.
14
- *
15
- * @param epoch -
16
- */
17
- export const floorMinute = (epoch) => {
18
- const d = ensureDate(epoch);
19
- return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes());
14
+ const floorMinute = (epoch) => {
15
+ const d = ensureDate(epoch);
16
+ return Date.UTC(
17
+ d.getUTCFullYear(),
18
+ d.getUTCMonth(),
19
+ d.getUTCDate(),
20
+ d.getUTCHours(),
21
+ d.getUTCMinutes()
22
+ );
20
23
  };
21
- /**
22
- * Rounds down `epoch` to hour precision.
23
- *
24
- * @param epoch -
25
- */
26
- export const floorHour = (epoch) => {
27
- const d = ensureDate(epoch);
28
- return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours());
24
+ const floorHour = (epoch) => {
25
+ const d = ensureDate(epoch);
26
+ return Date.UTC(
27
+ d.getUTCFullYear(),
28
+ d.getUTCMonth(),
29
+ d.getUTCDate(),
30
+ d.getUTCHours()
31
+ );
29
32
  };
30
- /**
31
- * Rounds down `epoch` to day precision
32
- *
33
- * @param epoch -
34
- */
35
- export const floorDay = (epoch) => {
36
- const d = ensureDate(epoch);
37
- return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
33
+ const floorDay = (epoch) => {
34
+ const d = ensureDate(epoch);
35
+ return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
38
36
  };
39
- /**
40
- * Rounds down `epoch` to week precision. Assumes ISO8601 week logic, i.e. weeks
41
- * start on Monday.
42
- *
43
- * @param epoch -
44
- */
45
- export const floorWeek = (epoch) => {
46
- const d = ensureDate(epoch);
47
- const w = d.getUTCDay();
48
- return (Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()) -
49
- ((w || 7) - 1) * DAY);
37
+ const floorWeek = (epoch) => {
38
+ const d = ensureDate(epoch);
39
+ const w = d.getUTCDay();
40
+ return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()) - ((w || 7) - 1) * DAY;
50
41
  };
51
- /**
52
- * Rounds down `epoch` to month precision.
53
- *
54
- * @param epoch -
55
- */
56
- export const floorMonth = (epoch) => {
57
- const d = ensureDate(epoch);
58
- return Date.UTC(d.getUTCFullYear(), d.getUTCMonth());
42
+ const floorMonth = (epoch) => {
43
+ const d = ensureDate(epoch);
44
+ return Date.UTC(d.getUTCFullYear(), d.getUTCMonth());
59
45
  };
60
- /**
61
- * Rounds down `epoch` to month precision, but at beginning of a quarter.
62
- *
63
- * @param epoch -
64
- */
65
- export const floorQuarter = (epoch) => {
66
- const d = ensureDate(epoch);
67
- return Date.UTC(d.getUTCFullYear(), ((d.getUTCMonth() / 3) | 0) * 3);
46
+ const floorQuarter = (epoch) => {
47
+ const d = ensureDate(epoch);
48
+ return Date.UTC(d.getUTCFullYear(), (d.getUTCMonth() / 3 | 0) * 3);
68
49
  };
69
- /**
70
- * Rounds down `epoch` to year precision.
71
- *
72
- * @param epoch -
73
- */
74
- export const floorYear = (epoch) => Date.UTC(ensureDate(epoch).getUTCFullYear(), 0);
75
- /**
76
- * Rounds up `epoch` to minute precision.
77
- *
78
- * @param epoch -
79
- */
80
- export const ceilSecond = (epoch) => floorSecond(ensureDate(epoch).getTime() + SECOND);
81
- /**
82
- * Rounds up `epoch` to minute precision.
83
- *
84
- * @param epoch -
85
- */
86
- export const ceilMinute = (epoch) => floorMinute(ensureDate(epoch).getTime() + MINUTE);
87
- /**
88
- * Rounds up `epoch` to hour precision.
89
- *
90
- * @param epoch -
91
- */
92
- export const ceilHour = (epoch) => floorHour(ensureDate(epoch).getTime() + HOUR);
93
- /**
94
- * Rounds up `epoch` to day precision
95
- *
96
- * @param epoch -
97
- */
98
- export const ceilDay = (epoch) => floorDay(ensureDate(epoch).getTime() + DAY);
99
- /**
100
- * Rounds up `epoch` to week precision. Assumes ISO8601 week logic, i.e. weeks
101
- * start on Monday.
102
- *
103
- * @param epoch -
104
- */
105
- export const ceilWeek = (epoch) => floorWeek(ensureDate(epoch).getTime() + WEEK);
106
- /**
107
- * Rounds up `epoch` to month precision
108
- *
109
- * @param epoch -
110
- */
111
- export const ceilMonth = (epoch) => {
112
- const d = ensureDate(epoch);
113
- let y = d.getUTCFullYear();
114
- let m = d.getUTCMonth() + 1;
115
- m > 11 && y++;
116
- return Date.UTC(y, m % 12);
50
+ const floorYear = (epoch) => Date.UTC(ensureDate(epoch).getUTCFullYear(), 0);
51
+ const ceilSecond = (epoch) => floorSecond(ensureDate(epoch).getTime() + SECOND);
52
+ const ceilMinute = (epoch) => floorMinute(ensureDate(epoch).getTime() + MINUTE);
53
+ const ceilHour = (epoch) => floorHour(ensureDate(epoch).getTime() + HOUR);
54
+ const ceilDay = (epoch) => floorDay(ensureDate(epoch).getTime() + DAY);
55
+ const ceilWeek = (epoch) => floorWeek(ensureDate(epoch).getTime() + WEEK);
56
+ const ceilMonth = (epoch) => {
57
+ const d = ensureDate(epoch);
58
+ let y = d.getUTCFullYear();
59
+ let m = d.getUTCMonth() + 1;
60
+ m > 11 && y++;
61
+ return Date.UTC(y, m % 12);
117
62
  };
118
- /**
119
- * Rounds up `epoch` to month precision (beginning of next quarter)
120
- *
121
- * @param epoch -
122
- */
123
- export const ceilQuarter = (epoch) => {
124
- const d = ensureDate(epoch);
125
- let y = d.getUTCFullYear();
126
- let m = (((d.getUTCMonth() + 3) / 3) | 0) * 3;
127
- m > 11 && y++;
128
- return Date.UTC(y, m % 12);
63
+ const ceilQuarter = (epoch) => {
64
+ const d = ensureDate(epoch);
65
+ let y = d.getUTCFullYear();
66
+ let m = ((d.getUTCMonth() + 3) / 3 | 0) * 3;
67
+ m > 11 && y++;
68
+ return Date.UTC(y, m % 12);
69
+ };
70
+ const ceilYear = (epoch) => Date.UTC(ensureDate(epoch).getUTCFullYear() + 1, 0);
71
+ export {
72
+ ceilDay,
73
+ ceilHour,
74
+ ceilMinute,
75
+ ceilMonth,
76
+ ceilQuarter,
77
+ ceilSecond,
78
+ ceilWeek,
79
+ ceilYear,
80
+ floorDay,
81
+ floorHour,
82
+ floorMinute,
83
+ floorMonth,
84
+ floorQuarter,
85
+ floorSecond,
86
+ floorWeek,
87
+ floorYear
129
88
  };
130
- /**
131
- * Rounds up `epoch` to year precision
132
- *
133
- * @param epoch -
134
- */
135
- export const ceilYear = (epoch) => Date.UTC(ensureDate(epoch).getUTCFullYear() + 1, 0);
package/timecode.js CHANGED
@@ -1,46 +1,22 @@
1
1
  import { Z2 } from "@thi.ng/strings/pad-left";
2
2
  import { decomposeDuration } from "./duration.js";
3
- /**
4
- * Returns a time formatter for given FPS (frames / second, in [1..1000] range),
5
- * e.g. `HH:mm:ss:ff`. The returned function takes a single arg (time in
6
- * milliseconds) and returns formatted string.
7
- *
8
- * @remarks
9
- * The timecode considers days too, but only includes them in the result if the
10
- * day part is non-zero. The 4 separators between each field can be customized
11
- * via 2nd arg (default: all `:`).
12
- *
13
- * @example
14
- * ```ts
15
- * a = defTimecode(30);
16
- * a(HOUR + 2*MINUTE + 3*SECOND + 4*1000/30)
17
- * // "01:02:03:04"
18
- *
19
- * a(DAY);
20
- * // "01:00:00:00:00"
21
- *
22
- * b = defTimecode(30, ["d ", "h ", "' ", '" ']);
23
- * b(Day + HOUR + 2*MINUTE + 3*SECOND + 999)
24
- * // "01d 01h 02' 03" 29"
25
- * ```
26
- *
27
- * @param fps -
28
- * @param sep -
29
- */
30
- export const defTimecode = (fps, sep = "::::") => {
31
- const frame = 1000 / fps;
32
- return (t) => {
33
- const [_, __, d, h, m, s, ms] = decomposeDuration(t);
34
- const parts = [
35
- Z2(h),
36
- sep[1],
37
- Z2(m),
38
- sep[2],
39
- Z2(s),
40
- sep[3],
41
- Z2((ms / frame) | 0),
42
- ];
43
- d > 0 && parts.unshift(Z2(d), sep[0]);
44
- return parts.join("");
45
- };
3
+ const defTimecode = (fps, sep = "::::") => {
4
+ const frame = 1e3 / fps;
5
+ return (t) => {
6
+ const [_, __, d, h, m, s, ms] = decomposeDuration(t);
7
+ const parts = [
8
+ Z2(h),
9
+ sep[1],
10
+ Z2(m),
11
+ sep[2],
12
+ Z2(s),
13
+ sep[3],
14
+ Z2(ms / frame | 0)
15
+ ];
16
+ d > 0 && parts.unshift(Z2(d), sep[0]);
17
+ return parts.join("");
18
+ };
19
+ };
20
+ export {
21
+ defTimecode
46
22
  };
package/units.js CHANGED
@@ -1,20 +1,25 @@
1
1
  import { DAYS_IN_MONTH, DAYS_IN_MONTH_OFFSET } from "./api.js";
2
2
  import { isLeapYear } from "./checks.js";
3
- export const daysInMonth = (year, month) => {
4
- const days = DAYS_IN_MONTH[month];
5
- return days + ~~(month === 1 && isLeapYear(year));
3
+ const daysInMonth = (year, month) => {
4
+ const days = DAYS_IN_MONTH[month];
5
+ return days + ~~(month === 1 && isLeapYear(year));
6
6
  };
7
- export const dayInYear = (y, m, d) => DAYS_IN_MONTH_OFFSET[m] + d + ~~(m > 1 && isLeapYear(y));
8
- export const weekInYear = (y, m, d) => {
9
- const start = new Date(Date.UTC(y, 0, 1)).getDay() || 7;
10
- if (!m) {
11
- if (start === 5 && d < 4)
12
- return 53;
13
- if (start === 6 && d < 3)
14
- return 52 + ~~isLeapYear(y - 1);
15
- if (start === 7 && d < 2)
16
- return 52;
17
- }
18
- const offset = (start < 5 ? 8 : 15) - start;
19
- return Math.ceil((dayInYear(y, m, d) - offset) / 7 + 1);
7
+ const dayInYear = (y, m, d) => DAYS_IN_MONTH_OFFSET[m] + d + ~~(m > 1 && isLeapYear(y));
8
+ const weekInYear = (y, m, d) => {
9
+ const start = new Date(Date.UTC(y, 0, 1)).getDay() || 7;
10
+ if (!m) {
11
+ if (start === 5 && d < 4)
12
+ return 53;
13
+ if (start === 6 && d < 3)
14
+ return 52 + ~~isLeapYear(y - 1);
15
+ if (start === 7 && d < 2)
16
+ return 52;
17
+ }
18
+ const offset = (start < 5 ? 8 : 15) - start;
19
+ return Math.ceil((dayInYear(y, m, d) - offset) / 7 + 1);
20
+ };
21
+ export {
22
+ dayInYear,
23
+ daysInMonth,
24
+ weekInYear
20
25
  };