@nivinjoseph/n-date 1.0.1
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/.editorconfig +14 -0
- package/.vscode/launch.json +56 -0
- package/.vscode/settings.json +111 -0
- package/.vscode/tasks.json +12 -0
- package/.yarn/releases/yarn-4.14.1.cjs +940 -0
- package/.yarnrc.yml +8 -0
- package/LICENSE +21 -0
- package/README.md +21 -0
- package/dist/date-time-format.d.ts +11 -0
- package/dist/date-time-format.d.ts.map +1 -0
- package/dist/date-time-format.js +11 -0
- package/dist/date-time-format.js.map +1 -0
- package/dist/date-time-span.d.ts +70 -0
- package/dist/date-time-span.d.ts.map +1 -0
- package/dist/date-time-span.js +122 -0
- package/dist/date-time-span.js.map +1 -0
- package/dist/date-time.d.ts +391 -0
- package/dist/date-time.d.ts.map +1 -0
- package/dist/date-time.js +753 -0
- package/dist/date-time.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/tsconfig.json +13 -0
- package/docs/README.md +33 -0
- package/docs/date-time-span.md +72 -0
- package/docs/date-time.md +169 -0
- package/docs/formats.md +56 -0
- package/docs/getting-started.md +151 -0
- package/eslint.config.js +596 -0
- package/package.json +57 -0
- package/src/date-time-format.ts +35 -0
- package/src/date-time-span.ts +130 -0
- package/src/date-time.ts +950 -0
- package/src/index.ts +3 -0
- package/test/date-time-comparison.test.ts +1579 -0
- package/test/date-time-create.test.ts +1147 -0
- package/test/date-time-math.test.ts +324 -0
- package/test/date-time-properties.test.ts +200 -0
- package/test/date-time-utility.test.ts +432 -0
- package/test/date-time-validations.test.ts +521 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { describe, test } from "node:test";
|
|
3
|
+
import { DateTime, DateTimeFormat_DEFAULT } from "../src/index.js";
|
|
4
|
+
import { given } from "@nivinjoseph/n-defensive";
|
|
5
|
+
import { ArgumentException } from "@nivinjoseph/n-exception";
|
|
6
|
+
import { Duration } from "@nivinjoseph/n-util";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
await describe("DateTime Math", async () =>
|
|
10
|
+
{
|
|
11
|
+
await describe("Add time", async () =>
|
|
12
|
+
{
|
|
13
|
+
const dateTime = new DateTime({ value: "2024-01-01 10:00:00", zone: "utc" });
|
|
14
|
+
const dateTimeNonLeapYear = new DateTime({ value: "2025-01-01 10:00:00", zone: "utc" });
|
|
15
|
+
const dateTimeStartDateDst = new DateTime({ value: "2024-03-10 00:00:00", zone: "America/Los_Angeles" });
|
|
16
|
+
const dateTimeEndDateDst = new DateTime({ value: "2024-11-03 00:00:00", zone: "America/Los_Angeles" });
|
|
17
|
+
|
|
18
|
+
async function checkIsCorrect(dateTime: DateTime, duration: Duration, time: string, expectedValue: string): Promise<void>
|
|
19
|
+
{
|
|
20
|
+
given(dateTime, "dateTime").ensureHasValue().ensureIsType(DateTime);
|
|
21
|
+
given(duration, "duration").ensureHasValue().ensureIsInstanceOf(Duration);
|
|
22
|
+
given(time, "time").ensureHasValue().ensureIsString();
|
|
23
|
+
given(expectedValue, "expectedValue").ensureHasValue().ensureIsString()
|
|
24
|
+
.ensure(t => DateTime.validateDateTimeFormat(t, DateTimeFormat_DEFAULT));
|
|
25
|
+
|
|
26
|
+
await test(`Given a DateTime (${dateTime.toString()}), and a duration of ${time}
|
|
27
|
+
when duration is added to the dateTime
|
|
28
|
+
then it should return value of ${expectedValue}`,
|
|
29
|
+
() =>
|
|
30
|
+
{
|
|
31
|
+
assert.strictEqual(dateTime.addTime(duration).value, expectedValue);
|
|
32
|
+
}
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
await describe("Add milliseconds", async () =>
|
|
37
|
+
{
|
|
38
|
+
await checkIsCorrect(dateTime, Duration.fromMilliSeconds(0), "0 milliseconds", dateTime.value);
|
|
39
|
+
await checkIsCorrect(dateTime, Duration.fromMilliSeconds(1), "1 millisecond", dateTime.value);
|
|
40
|
+
await checkIsCorrect(dateTime, Duration.fromMilliSeconds(60000), "60000 milliseconds", "2024-01-01 10:01:00");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
await describe("Add seconds", async () =>
|
|
44
|
+
{
|
|
45
|
+
await checkIsCorrect(dateTime, Duration.fromSeconds(0), "0 seconds", dateTime.value);
|
|
46
|
+
await checkIsCorrect(dateTime, Duration.fromSeconds(1), "1 second", "2024-01-01 10:00:01");
|
|
47
|
+
await checkIsCorrect(dateTime, Duration.fromSeconds(60), "60 seconds", "2024-01-01 10:01:00");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
await describe("Add minutes", async () =>
|
|
51
|
+
{
|
|
52
|
+
await checkIsCorrect(dateTime, Duration.fromMinutes(0), "0 minutes", dateTime.value);
|
|
53
|
+
await checkIsCorrect(dateTime, Duration.fromMinutes(1), "1 minute", "2024-01-01 10:01:00");
|
|
54
|
+
await checkIsCorrect(dateTime, Duration.fromMinutes(60), "60 minutes", "2024-01-01 11:00:00");
|
|
55
|
+
|
|
56
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromMinutes(60), "60 minute", "2024-03-10 01:00:00");
|
|
57
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromMinutes(120), "120 minute", "2024-03-10 03:00:00");
|
|
58
|
+
|
|
59
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromMinutes(60), "60 minute", "2024-11-03 01:00:00");
|
|
60
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromMinutes(120), "120 minute", "2024-11-03 01:00:00");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
await describe("Add hours", async () =>
|
|
64
|
+
{
|
|
65
|
+
await checkIsCorrect(dateTime, Duration.fromHours(0), "0 hours", dateTime.value);
|
|
66
|
+
await checkIsCorrect(dateTime, Duration.fromHours(1), "1 hour", "2024-01-01 11:00:00");
|
|
67
|
+
await checkIsCorrect(dateTime, Duration.fromHours(24), "24 hours", "2024-01-02 10:00:00");
|
|
68
|
+
await checkIsCorrect(dateTime, Duration.fromHours(48), "48 hours", "2024-01-03 10:00:00");
|
|
69
|
+
|
|
70
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromHours(1), "1 hour", "2024-03-10 01:00:00");
|
|
71
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromHours(2), "2 hours", "2024-03-10 03:00:00");
|
|
72
|
+
|
|
73
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromHours(1), "1 hour", "2024-11-03 01:00:00");
|
|
74
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromHours(2), "2 hours", "2024-11-03 01:00:00");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
await describe("Add days", async () =>
|
|
78
|
+
{
|
|
79
|
+
await checkIsCorrect(dateTime, Duration.fromDays(0), "0 days", dateTime.value);
|
|
80
|
+
await checkIsCorrect(dateTime, Duration.fromDays(1), "1 day", "2024-01-02 10:00:00");
|
|
81
|
+
await checkIsCorrect(dateTime, Duration.fromDays(31), "31 days", "2024-02-01 10:00:00");
|
|
82
|
+
await checkIsCorrect(dateTime, Duration.fromDays(366), "366 days", "2025-01-01 10:00:00");
|
|
83
|
+
await checkIsCorrect(dateTimeNonLeapYear, Duration.fromDays(365), "365 days", "2026-01-01 10:00:00");
|
|
84
|
+
|
|
85
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromDays(1), "1 day", "2024-03-11 01:00:00");
|
|
86
|
+
|
|
87
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromDays(1), "1 day", "2024-11-03 23:00:00");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
await describe("Add weeks", async () =>
|
|
91
|
+
{
|
|
92
|
+
await checkIsCorrect(dateTime, Duration.fromWeeks(0), "0 weeks", dateTime.value);
|
|
93
|
+
await checkIsCorrect(dateTime, Duration.fromWeeks(1), "1 week", "2024-01-08 10:00:00");
|
|
94
|
+
await checkIsCorrect(dateTime, Duration.fromWeeks(366 / 7), `366 / 7 weeks`, "2025-01-01 10:00:00");
|
|
95
|
+
await checkIsCorrect(dateTimeNonLeapYear, Duration.fromWeeks(365 / 7), `365 / 7 weeks`, "2026-01-01 10:00:00");
|
|
96
|
+
|
|
97
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromWeeks(1), "1 week", "2024-03-17 01:00:00");
|
|
98
|
+
|
|
99
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromWeeks(1), "1 week", "2024-11-09 23:00:00");
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
await describe("Subtract time", async () =>
|
|
105
|
+
{
|
|
106
|
+
const dateTime = new DateTime({ value: "2024-01-01 10:00:00", zone: "utc" });
|
|
107
|
+
const dateTimeLeapYear = new DateTime({ value: "2025-01-01 10:00:00", zone: "utc" });
|
|
108
|
+
const dateTimeStartDateDst = new DateTime({ value: "2024-03-10 04:00:00", zone: "America/Los_Angeles" });
|
|
109
|
+
const dateTimeEndDateDst = new DateTime({ value: "2024-11-03 04:00:00", zone: "America/Los_Angeles" });
|
|
110
|
+
|
|
111
|
+
async function checkIsCorrect(dateTime: DateTime, duration: Duration, time: string, expectedValue: string): Promise<void>
|
|
112
|
+
{
|
|
113
|
+
given(dateTime, "dateTime").ensureHasValue().ensureIsType(DateTime);
|
|
114
|
+
given(duration, "duration").ensureHasValue().ensureIsInstanceOf(Duration);
|
|
115
|
+
given(time, "time").ensureHasValue().ensureIsString();
|
|
116
|
+
given(expectedValue, "expectedValue").ensureHasValue().ensureIsString()
|
|
117
|
+
.ensure(t => DateTime.validateDateTimeFormat(t, DateTimeFormat_DEFAULT));
|
|
118
|
+
|
|
119
|
+
await test(`Given a DateTime (${dateTime.toString()}), and a duration of ${time}
|
|
120
|
+
when duration is subtracted from the dateTime
|
|
121
|
+
then it should return a value of ${expectedValue}`,
|
|
122
|
+
() =>
|
|
123
|
+
{
|
|
124
|
+
assert.strictEqual(dateTime.subtractTime(duration).value, expectedValue);
|
|
125
|
+
}
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
await describe("Subtract milliseconds", async () =>
|
|
130
|
+
{
|
|
131
|
+
await checkIsCorrect(dateTime, Duration.fromMilliSeconds(0), "0 milliseconds", dateTime.value);
|
|
132
|
+
await checkIsCorrect(dateTime, Duration.fromMilliSeconds(1), "1 millisecond", "2024-01-01 09:59:59");
|
|
133
|
+
await checkIsCorrect(dateTime, Duration.fromMilliSeconds(60000), "60000 milliseconds", "2024-01-01 09:59:00");
|
|
134
|
+
await checkIsCorrect(dateTime, Duration.fromMilliSeconds(60001), "60001 milliseconds", "2024-01-01 09:58:59");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
await describe("Subtract seconds", async () =>
|
|
138
|
+
{
|
|
139
|
+
await checkIsCorrect(dateTime, Duration.fromSeconds(0), "0 seconds", dateTime.value);
|
|
140
|
+
await checkIsCorrect(dateTime, Duration.fromSeconds(1), "1 second", "2024-01-01 09:59:59");
|
|
141
|
+
await checkIsCorrect(dateTime, Duration.fromSeconds(60), "60 seconds", "2024-01-01 09:59:00");
|
|
142
|
+
await checkIsCorrect(dateTime, Duration.fromSeconds(61), "61 seconds", "2024-01-01 09:58:59");
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
await describe("Subtract minutes", async () =>
|
|
146
|
+
{
|
|
147
|
+
await checkIsCorrect(dateTime, Duration.fromMinutes(0), "0 minutes", dateTime.value);
|
|
148
|
+
await checkIsCorrect(dateTime, Duration.fromMinutes(1), "1 minute", "2024-01-01 09:59:00");
|
|
149
|
+
await checkIsCorrect(dateTime, Duration.fromMinutes(60), "60 minutes", "2024-01-01 09:00:00");
|
|
150
|
+
await checkIsCorrect(dateTime, Duration.fromMinutes(61), "61 minutes", "2024-01-01 08:59:00");
|
|
151
|
+
|
|
152
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromMinutes(60), "60 minute", "2024-03-10 03:00:00");
|
|
153
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromMinutes(120), "120 minute", "2024-03-10 01:00:00");
|
|
154
|
+
|
|
155
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromMinutes(60), "60 minute", "2024-11-03 03:00:00");
|
|
156
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromMinutes(120), "120 minute", "2024-11-03 02:00:00");
|
|
157
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromMinutes(180), "180 minute", "2024-11-03 01:00:00");
|
|
158
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromMinutes(240), "240 minute", "2024-11-03 01:00:00");
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
await describe("Subtract hours", async () =>
|
|
162
|
+
{
|
|
163
|
+
await checkIsCorrect(dateTime, Duration.fromHours(0), "0 hours", dateTime.value);
|
|
164
|
+
await checkIsCorrect(dateTime, Duration.fromHours(1), "1 hour", "2024-01-01 09:00:00");
|
|
165
|
+
await checkIsCorrect(dateTime, Duration.fromHours(24), "24 hours", "2023-12-31 10:00:00");
|
|
166
|
+
await checkIsCorrect(dateTime, Duration.fromHours(25), "25 hours", "2023-12-31 09:00:00");
|
|
167
|
+
|
|
168
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromHours(1), "1 hour", "2024-03-10 03:00:00");
|
|
169
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromHours(2), "2 hours", "2024-03-10 01:00:00");
|
|
170
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromHours(24), "24 hours", "2024-03-09 03:00:00");
|
|
171
|
+
|
|
172
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromHours(1), "1 hour", "2024-11-03 03:00:00");
|
|
173
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromHours(2), "2 hours", "2024-11-03 02:00:00");
|
|
174
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromHours(3), "3 hours", "2024-11-03 01:00:00");
|
|
175
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromHours(4), "4 hours", "2024-11-03 01:00:00");
|
|
176
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromHours(24), "24 hours", "2024-11-02 05:00:00");
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
await describe("Subtract days", async () =>
|
|
180
|
+
{
|
|
181
|
+
await checkIsCorrect(dateTime, Duration.fromDays(0), "0 days", dateTime.value);
|
|
182
|
+
await checkIsCorrect(dateTime, Duration.fromDays(1), "1 day", "2023-12-31 10:00:00");
|
|
183
|
+
await checkIsCorrect(dateTime, Duration.fromDays(31), "31 days", "2023-12-01 10:00:00");
|
|
184
|
+
await checkIsCorrect(dateTime, Duration.fromDays(365), "365 days", "2023-01-01 10:00:00");
|
|
185
|
+
await checkIsCorrect(dateTimeLeapYear, Duration.fromDays(366), "366 days", "2024-01-01 10:00:00");
|
|
186
|
+
|
|
187
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromDays(1), "1 day", "2024-03-09 03:00:00");
|
|
188
|
+
|
|
189
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromDays(1), "1 day", "2024-11-02 05:00:00");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
await describe("Subtract weeks", async () =>
|
|
193
|
+
{
|
|
194
|
+
await checkIsCorrect(dateTime, Duration.fromWeeks(0), "0 weeks", dateTime.value);
|
|
195
|
+
await checkIsCorrect(dateTime, Duration.fromWeeks(1), "1 week", "2023-12-25 10:00:00");
|
|
196
|
+
await checkIsCorrect(dateTime, Duration.fromWeeks(365 / 7), `365 / 7 weeks`, "2023-01-01 10:00:00");
|
|
197
|
+
await checkIsCorrect(dateTimeLeapYear, Duration.fromWeeks(366 / 7), `366 / 7 weeks`, "2024-01-01 10:00:00");
|
|
198
|
+
|
|
199
|
+
await checkIsCorrect(dateTimeStartDateDst, Duration.fromWeeks(1), "1 week", "2024-03-03 03:00:00");
|
|
200
|
+
|
|
201
|
+
await checkIsCorrect(dateTimeEndDateDst, Duration.fromWeeks(1), "1 week", "2024-10-27 05:00:00");
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
await describe("Add days", async () =>
|
|
207
|
+
{
|
|
208
|
+
const dateTime = new DateTime({ value: "2024-01-01 10:00:00", zone: "utc" });
|
|
209
|
+
const dateTimeNonLeapYear = new DateTime({ value: "2025-01-01 10:00:00", zone: "utc" });
|
|
210
|
+
|
|
211
|
+
await describe("Add days", async () =>
|
|
212
|
+
{
|
|
213
|
+
async function checkIsCorrect(dateTime: DateTime, days: number, expectedValue: string): Promise<void>
|
|
214
|
+
{
|
|
215
|
+
given(dateTime, "dateTime").ensureHasValue().ensureIsType(DateTime);
|
|
216
|
+
given(days, "days").ensureHasValue().ensureIsNumber();
|
|
217
|
+
given(expectedValue, "expectedValue").ensureHasValue().ensureIsString()
|
|
218
|
+
.ensure(t => DateTime.validateDateTimeFormat(t, DateTimeFormat_DEFAULT));
|
|
219
|
+
|
|
220
|
+
await test(`Given a DateTime (${dateTime.toString()}), and number of days ${days}
|
|
221
|
+
when number of days is added to the dateTime
|
|
222
|
+
then it should return value of ${expectedValue}`,
|
|
223
|
+
() =>
|
|
224
|
+
{
|
|
225
|
+
assert.strictEqual(dateTime.addDays(days).value, expectedValue);
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
await checkIsCorrect(dateTime, 0, dateTime.value);
|
|
231
|
+
await checkIsCorrect(dateTime, 1, "2024-01-02 10:00:00");
|
|
232
|
+
await checkIsCorrect(dateTime, 31, "2024-02-01 10:00:00");
|
|
233
|
+
await checkIsCorrect(dateTime, 366, "2025-01-01 10:00:00");
|
|
234
|
+
await checkIsCorrect(dateTimeNonLeapYear, 365, "2026-01-01 10:00:00");
|
|
235
|
+
|
|
236
|
+
const dateTimeStartDateDst = new DateTime({ value: "2024-03-10 00:00:00", zone: "America/Los_Angeles" });
|
|
237
|
+
await checkIsCorrect(dateTimeStartDateDst, 1, "2024-03-11 00:00:00");
|
|
238
|
+
|
|
239
|
+
const dateTimeEndDateDst = new DateTime({ value: "2024-11-03 00:00:00", zone: "America/Los_Angeles" });
|
|
240
|
+
await checkIsCorrect(dateTimeEndDateDst, 1, "2024-11-04 00:00:00");
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
await describe("Invalid Params", async () =>
|
|
244
|
+
{
|
|
245
|
+
async function checkIsInvalidParam(days: number, reason: string): Promise<void>
|
|
246
|
+
{
|
|
247
|
+
await test(`Given a DateTime (${dateTime.toString()}), and and number of days ${days}
|
|
248
|
+
when days is added to dateTime and ${reason}
|
|
249
|
+
then it should throw a validation error`,
|
|
250
|
+
() =>
|
|
251
|
+
{
|
|
252
|
+
assert.throws(() => dateTime.addDays(days), ArgumentException);
|
|
253
|
+
}
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
await checkIsInvalidParam(0.5, "number of days is not integer");
|
|
258
|
+
await checkIsInvalidParam(-1, "number of days is negative");
|
|
259
|
+
await checkIsInvalidParam(-1.5, "number of days is negative integer");
|
|
260
|
+
await checkIsInvalidParam(1.5, "number of days is not integer");
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
await describe("Subtract days", async () =>
|
|
265
|
+
{
|
|
266
|
+
const dateTime = new DateTime({ value: "2024-01-01 10:00:00", zone: "utc" });
|
|
267
|
+
const dateTimeLeapYear = new DateTime({ value: "2025-01-01 10:00:00", zone: "utc" });
|
|
268
|
+
|
|
269
|
+
await describe("Subtract days", async () =>
|
|
270
|
+
{
|
|
271
|
+
async function checkIsCorrect(dateTime: DateTime, days: number, expectedValue: string): Promise<void>
|
|
272
|
+
{
|
|
273
|
+
given(dateTime, "dateTime").ensureHasValue().ensureIsType(DateTime);
|
|
274
|
+
given(days, "days").ensureHasValue().ensureIsNumber();
|
|
275
|
+
given(expectedValue, "expectedValue").ensureHasValue().ensureIsString()
|
|
276
|
+
.ensure(t => DateTime.validateDateTimeFormat(t, DateTimeFormat_DEFAULT));
|
|
277
|
+
|
|
278
|
+
await test(`Given a DateTime (${dateTime.toString()}), and number of days ${days}
|
|
279
|
+
when number of days is added to the dateTime
|
|
280
|
+
then it should return value of ${expectedValue}`,
|
|
281
|
+
() =>
|
|
282
|
+
{
|
|
283
|
+
assert.strictEqual(dateTime.subtractDays(days).value, expectedValue);
|
|
284
|
+
}
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
await checkIsCorrect(dateTime, 0, dateTime.value);
|
|
290
|
+
await checkIsCorrect(dateTime, 1, "2023-12-31 10:00:00");
|
|
291
|
+
await checkIsCorrect(dateTime, 31, "2023-12-01 10:00:00");
|
|
292
|
+
await checkIsCorrect(dateTime, 365, "2023-01-01 10:00:00");
|
|
293
|
+
await checkIsCorrect(dateTimeLeapYear, 366, "2024-01-01 10:00:00");
|
|
294
|
+
await checkIsCorrect(new DateTime({ value: "2024-03-10 10:00:00", zone: "America/Los_Angeles" }), 1, "2024-03-09 10:00:00");
|
|
295
|
+
|
|
296
|
+
const dateTimeStartDateDst = new DateTime({ value: "2024-03-10 04:00:00", zone: "America/Los_Angeles" });
|
|
297
|
+
await checkIsCorrect(dateTimeStartDateDst, 1, "2024-03-09 04:00:00");
|
|
298
|
+
|
|
299
|
+
const dateTimeEndDateDst = new DateTime({ value: "2024-11-03 04:00:00", zone: "America/Los_Angeles" });
|
|
300
|
+
await checkIsCorrect(dateTimeEndDateDst, 1, "2024-11-02 04:00:00");
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
await describe("Invalid Params", async () =>
|
|
304
|
+
{
|
|
305
|
+
async function checkIsInvalidParam(days: number, reason: string): Promise<void>
|
|
306
|
+
{
|
|
307
|
+
await test(`Given a DateTime (${dateTime.toString()}), and and number of days ${days}
|
|
308
|
+
when days is subtracted from dateTime and ${reason}
|
|
309
|
+
then it should throw a validation error`,
|
|
310
|
+
() =>
|
|
311
|
+
{
|
|
312
|
+
assert.throws(() => dateTime.subtractDays(days), ArgumentException);
|
|
313
|
+
}
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
await checkIsInvalidParam(0.5, "number of days is not integer");
|
|
318
|
+
await checkIsInvalidParam(-1, "number of days is negative");
|
|
319
|
+
await checkIsInvalidParam(-1.5, "number of days is negative integer");
|
|
320
|
+
await checkIsInvalidParam(1.5, "number of days is not integer");
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { describe, test } from "node:test";
|
|
3
|
+
import { DateTime } from "../src/index.js";
|
|
4
|
+
import { Duration } from "@nivinjoseph/n-util";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
await describe("DateTime Properties", async () =>
|
|
8
|
+
{
|
|
9
|
+
await describe("Timestamp", async () =>
|
|
10
|
+
{
|
|
11
|
+
await test(`Given a luxon date time
|
|
12
|
+
when DateTime is created from that value and zone
|
|
13
|
+
then both should have same timeStamp`,
|
|
14
|
+
() =>
|
|
15
|
+
{
|
|
16
|
+
assert.strictEqual(new DateTime({ value: "2024-01-01 10:00", zone: "utc" }).timestamp, 1704103200);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
await test(`Given a value epoch start ("1970-01-01 00:00") in utc
|
|
20
|
+
when DateTime is created from that
|
|
21
|
+
then it should have timeStamp 0`,
|
|
22
|
+
() =>
|
|
23
|
+
{
|
|
24
|
+
assert.strictEqual(new DateTime({ value: "1970-01-01 00:00", zone: "utc" }).timestamp, 0);
|
|
25
|
+
}
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
await test(`Given a value before epoch start ("1969-12-31 23:59") in utc
|
|
29
|
+
when DateTime is created from that
|
|
30
|
+
then it should have timeStamp negative (-60)`,
|
|
31
|
+
() =>
|
|
32
|
+
{
|
|
33
|
+
assert.strictEqual(new DateTime({ value: "1969-12-31 23:59", zone: "utc" }).timestamp, -60);
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
await test(`Given a value epoch start ("1970-01-01 00:01") in utc
|
|
38
|
+
when DateTime is created from that
|
|
39
|
+
then it should have timeStamp positive (60)`,
|
|
40
|
+
() =>
|
|
41
|
+
{
|
|
42
|
+
assert.strictEqual(new DateTime({ value: "1970-01-01 00:01", zone: "utc" }).timestamp, 60);
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
await describe("Date code", async () =>
|
|
49
|
+
{
|
|
50
|
+
await test(`Given a date time value (2024-01-01 10:00)
|
|
51
|
+
when DateTime is created from that value
|
|
52
|
+
then date code should be 20240101`,
|
|
53
|
+
() =>
|
|
54
|
+
{
|
|
55
|
+
assert.strictEqual(new DateTime({ value: "2024-01-01 10:00", zone: "utc" }).dateCode, "20240101");
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
await describe("Time code", async () =>
|
|
61
|
+
{
|
|
62
|
+
await test(`Given a date time value (2024-01-01 10:00)
|
|
63
|
+
when DateTime is created from that value
|
|
64
|
+
then time code should be 1000`,
|
|
65
|
+
() =>
|
|
66
|
+
{
|
|
67
|
+
assert.strictEqual(new DateTime({ value: "2024-01-01 10:00", zone: "utc" }).timeCode, "100000");
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
await describe("Date value", async () =>
|
|
73
|
+
{
|
|
74
|
+
const value = "2024-01-01 10:00";
|
|
75
|
+
const dateTime = new DateTime({ value, zone: "utc" });
|
|
76
|
+
const dateValue = value.split(" ").takeFirst();
|
|
77
|
+
|
|
78
|
+
await test(`Given a valid value (${value})
|
|
79
|
+
when a DateTime is created from that value
|
|
80
|
+
then date value should be ${dateValue}`,
|
|
81
|
+
() =>
|
|
82
|
+
{
|
|
83
|
+
assert.strictEqual(dateTime.dateValue, dateValue);
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
await test(`Given a valid value (${value})
|
|
88
|
+
when a DateTime is created from that value
|
|
89
|
+
then the date value property should be in valid date format`,
|
|
90
|
+
() =>
|
|
91
|
+
{
|
|
92
|
+
assert.ok(dateTime.dateValue.matchesFormat("####-##-##"));
|
|
93
|
+
}
|
|
94
|
+
);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
await describe("Time value", async () =>
|
|
98
|
+
{
|
|
99
|
+
const value = "2024-01-01 10:00:00";
|
|
100
|
+
const dateTime = new DateTime({ value, zone: "utc" });
|
|
101
|
+
const timeValue = value.split(" ").takeLast();
|
|
102
|
+
|
|
103
|
+
await test(`Given a valid value (${value})
|
|
104
|
+
when a DateTime is created from that value
|
|
105
|
+
then time value should be ${timeValue}`,
|
|
106
|
+
() =>
|
|
107
|
+
{
|
|
108
|
+
assert.strictEqual(dateTime.timeValue, timeValue);
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
await test(`Given a valid value (${value})
|
|
113
|
+
when a DateTime is created from that value
|
|
114
|
+
then the time value property should be in valid time format`,
|
|
115
|
+
() =>
|
|
116
|
+
{
|
|
117
|
+
assert.ok(dateTime.timeValue.matchesFormat("##:##:##"));
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
await describe("Is past", async () =>
|
|
123
|
+
{
|
|
124
|
+
await test(`Given a DateTime with year 2000 (2000-01-01 10:00)
|
|
125
|
+
when it's checked that it's in the past
|
|
126
|
+
then it should return true`,
|
|
127
|
+
() =>
|
|
128
|
+
{
|
|
129
|
+
assert.ok(new DateTime({ value: "2000-01-01 10:00", zone: "utc" }).isPast);
|
|
130
|
+
}
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
await test(`Given a DateTime with year 3000 (3000-01-01 10:00)
|
|
134
|
+
when it's checked that it's in the past
|
|
135
|
+
then it should return false`,
|
|
136
|
+
() =>
|
|
137
|
+
{
|
|
138
|
+
assert.ok(!new DateTime({ value: "3000-01-01 10:00", zone: "utc" }).isPast);
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
await test(`Given a DateTime 1 minute before now
|
|
143
|
+
when it's checked that it's in the past
|
|
144
|
+
then it should return true`,
|
|
145
|
+
() =>
|
|
146
|
+
{
|
|
147
|
+
assert.ok(DateTime.now().subtractTime(Duration.fromMinutes(1)).isPast);
|
|
148
|
+
}
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
await test(`Given a DateTime 1 minute after now
|
|
152
|
+
when it's checked that it's in the past
|
|
153
|
+
then it should return false`,
|
|
154
|
+
() =>
|
|
155
|
+
{
|
|
156
|
+
assert.ok(!DateTime.now().addTime(Duration.fromMinutes(1)).isPast);
|
|
157
|
+
}
|
|
158
|
+
);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
await describe("Is future", async () =>
|
|
162
|
+
{
|
|
163
|
+
await test(`Given a DateTime with year 2000 (2000-01-01 10:00)
|
|
164
|
+
when it's checked that it's in the future
|
|
165
|
+
then it should return false`,
|
|
166
|
+
() =>
|
|
167
|
+
{
|
|
168
|
+
assert.ok(!new DateTime({ value: "2000-01-01 10:00", zone: "utc" }).isFuture);
|
|
169
|
+
}
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
await test(`Given a DateTime with year 3000 (3000-01-01 10:00)
|
|
173
|
+
when it's checked that it's in the future
|
|
174
|
+
then it should return true`,
|
|
175
|
+
() =>
|
|
176
|
+
{
|
|
177
|
+
assert.ok(new DateTime({ value: "3000-01-01 10:00", zone: "utc" }).isFuture);
|
|
178
|
+
}
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
await test(`Given a DateTime 1 minute before now
|
|
182
|
+
when it's checked that it's in the future
|
|
183
|
+
then it should return false`,
|
|
184
|
+
() =>
|
|
185
|
+
{
|
|
186
|
+
assert.ok(!DateTime.now().subtractTime(Duration.fromMinutes(1)).isFuture);
|
|
187
|
+
}
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
await test(`Given a DateTime 1 minute after now
|
|
191
|
+
when it's checked that it's in the future
|
|
192
|
+
then it should return true`,
|
|
193
|
+
() =>
|
|
194
|
+
{
|
|
195
|
+
assert.ok(DateTime.now().addTime(Duration.fromMinutes(1)).isFuture);
|
|
196
|
+
}
|
|
197
|
+
);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|