@thi.ng/date 2.5.4 → 2.5.6
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/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/api.js +45 -55
- package/checks.js +8 -17
- package/datetime.js +318 -321
- package/duration.js +53 -97
- package/format.js +243 -388
- package/i18n/de.js +77 -81
- package/i18n/en.js +77 -81
- package/i18n/es.js +42 -43
- package/i18n/fr.js +44 -45
- package/i18n/it.js +44 -45
- package/i18n.js +47 -118
- package/internal/precision.js +6 -16
- package/iterators.js +41 -93
- package/package.json +9 -7
- package/relative.js +108 -168
- package/round.js +77 -124
- package/timecode.js +19 -43
- package/units.js +21 -16
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
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
|
-
|
|
41
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const daysInMonth = (year, month) => {
|
|
4
|
+
const days = DAYS_IN_MONTH[month];
|
|
5
|
+
return days + ~~(month === 1 && isLeapYear(year));
|
|
6
6
|
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
};
|