@quenty/time 2.4.6 → 2.6.0

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.
@@ -0,0 +1,714 @@
1
+ --!strict
2
+ --[[
3
+ @class Time.spec.lua
4
+ ]]
5
+
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local Jest = require("Jest")
9
+ local Time = require("Time")
10
+
11
+ local describe = Jest.Globals.describe
12
+ local expect = Jest.Globals.expect
13
+ local it = Jest.Globals.it
14
+
15
+ -- All timestamps are UTC unix seconds.
16
+ local EPOCH = 0 -- 1970-01-01 Thursday 00:00:00
17
+ local Y2000_FEB_29 = 951782400 -- 2000-02-29 Tuesday 00:00:00, day 60
18
+ local Y2000_MAR_1 = 951868800 -- 2000-03-01 Wednesday 00:00:00, day 61
19
+ local Y2000_LAST_SECOND = 978307199 -- 2000-12-31 Sunday 23:59:59, day 366
20
+ local Y2005_JAN_1 = 1104537600 -- 2005-01-01 Saturday 00:00:00
21
+ local Y2024_JAN_1 = 1704067200 -- 2024-01-01 Monday 00:00:00
22
+ local Y2024_FEB_29 = 1709164800 -- 2024-02-29 Thursday 00:00:00, day 60
23
+ local Y2024_LAST_SECOND = 1735689599 -- 2024-12-31 Tuesday 23:59:59, day 366
24
+ local Y2025_SEP_7 = 1757203200 -- 2025-09-07 Sunday 00:00:00
25
+ local Y2025_SEP_8 = 1757289600 -- 2025-09-08 Monday 00:00:00
26
+ local Y2025_SEP_22 = 1758499200 -- 2025-09-22 Monday 00:00:00, day 265
27
+ local Y2025_SEP_22_11_59_59 = 1758542399 -- 2025-09-22 Monday 11:59:59
28
+ local Y2025_SEP_22_NOON = 1758542400 -- 2025-09-22 Monday 12:00:00
29
+ local Y2025_SEP_22_13_00 = 1758546000 -- 2025-09-22 Monday 13:00:00
30
+ local Y2025_SEP_22_LAST_SECOND = 1758585599 -- 2025-09-22 Monday 23:59:59
31
+
32
+ describe("Time.getDaysMonthTable", function()
33
+ it("should give February 29 days in a leap year", function()
34
+ expect(Time.getDaysMonthTable(2024)).toEqual({ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 })
35
+ end)
36
+
37
+ it("should give February 28 days in a common year", function()
38
+ expect(Time.getDaysMonthTable(2023)).toEqual({ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 })
39
+ end)
40
+
41
+ it("should treat centuries as common years unless divisible by 400", function()
42
+ expect(Time.getDaysMonthTable(1900)[2]).toBe(28)
43
+ expect(Time.getDaysMonthTable(2000)[2]).toBe(29)
44
+ expect(Time.getDaysMonthTable(2100)[2]).toBe(28)
45
+ end)
46
+
47
+ it("should return a read-only table", function()
48
+ expect(table.isfrozen(Time.getDaysMonthTable(2024))).toBe(true)
49
+ end)
50
+ end)
51
+
52
+ describe("Time.getSecond / getMinute / getHour", function()
53
+ it("should be zero at the epoch", function()
54
+ expect(Time.getSecond(EPOCH)).toBe(0)
55
+ expect(Time.getMinute(EPOCH)).toBe(0)
56
+ expect(Time.getHour(EPOCH)).toBe(0)
57
+ end)
58
+
59
+ it("should extract the clock components of a time", function()
60
+ expect(Time.getSecond(Y2025_SEP_22_LAST_SECOND)).toBe(59)
61
+ expect(Time.getMinute(Y2025_SEP_22_LAST_SECOND)).toBe(59)
62
+ expect(Time.getHour(Y2025_SEP_22_LAST_SECOND)).toBe(23)
63
+ end)
64
+
65
+ it("should report noon as hour 12", function()
66
+ expect(Time.getHour(Y2025_SEP_22_NOON)).toBe(12)
67
+ end)
68
+
69
+ it("should floor fractional seconds", function()
70
+ expect(Time.getSecond(1.9)).toBe(1)
71
+ expect(Time.getMinute(119.9)).toBe(1)
72
+ end)
73
+ end)
74
+
75
+ describe("Time.getDay", function()
76
+ it("should be day 1 on January 1st", function()
77
+ expect(Time.getDay(EPOCH)).toBe(1)
78
+ expect(Time.getDay(Y2024_JAN_1)).toBe(1)
79
+ end)
80
+
81
+ it("should count February 29th as day 60", function()
82
+ expect(Time.getDay(Y2024_FEB_29)).toBe(60)
83
+ end)
84
+
85
+ it("should reach day 366 at the end of a leap year", function()
86
+ expect(Time.getDay(Y2024_LAST_SECOND)).toBe(366)
87
+ expect(Time.getDay(Y2000_LAST_SECOND)).toBe(366)
88
+ end)
89
+
90
+ it("should compute the day of the year", function()
91
+ expect(Time.getDay(Y2025_SEP_22)).toBe(265)
92
+ expect(Time.getDay(Y2025_SEP_22_LAST_SECOND)).toBe(265)
93
+ end)
94
+ end)
95
+
96
+ describe("Time.getYear", function()
97
+ it("should be 1970 at the epoch", function()
98
+ expect(Time.getYear(EPOCH)).toBe(1970)
99
+ end)
100
+
101
+ it("should stay in the same year until the last second", function()
102
+ expect(Time.getYear(Y2000_LAST_SECOND)).toBe(2000)
103
+ expect(Time.getYear(Y2024_LAST_SECOND)).toBe(2024)
104
+ end)
105
+
106
+ it("should roll over on January 1st", function()
107
+ expect(Time.getYear(Y2024_JAN_1)).toBe(2024)
108
+ expect(Time.getYear(Y2025_SEP_22)).toBe(2025)
109
+ end)
110
+ end)
111
+
112
+ describe("Time.getYearShort", function()
113
+ it("should return the last two digits of the year", function()
114
+ expect(Time.getYearShort(Y2005_JAN_1)).toBe(5)
115
+ expect(Time.getYearShort(Y2024_JAN_1)).toBe(24)
116
+ end)
117
+
118
+ it("should zero pad the formatted short year", function()
119
+ expect(Time.getYearShortFormatted(Y2005_JAN_1)).toBe("05")
120
+ expect(Time.getYearShortFormatted(Y2024_JAN_1)).toBe("24")
121
+ end)
122
+ end)
123
+
124
+ describe("Time.getMonth", function()
125
+ it("should be January at the epoch", function()
126
+ expect(Time.getMonth(EPOCH)).toBe(1)
127
+ end)
128
+
129
+ it("should handle the leap day boundary", function()
130
+ expect(Time.getMonth(Y2000_FEB_29)).toBe(2)
131
+ expect(Time.getMonth(Y2000_MAR_1)).toBe(3)
132
+ expect(Time.getMonth(Y2024_FEB_29)).toBe(2)
133
+ end)
134
+
135
+ it("should be December at the end of the year", function()
136
+ expect(Time.getMonth(Y2000_LAST_SECOND)).toBe(12)
137
+ expect(Time.getMonth(Y2024_LAST_SECOND)).toBe(12)
138
+ end)
139
+
140
+ it("should compute the month", function()
141
+ expect(Time.getMonth(Y2025_SEP_22)).toBe(9)
142
+ end)
143
+
144
+ it("should zero pad the formatted month", function()
145
+ expect(Time.getFormattedMonth(Y2000_FEB_29)).toBe("02")
146
+ expect(Time.getFormattedMonth(Y2000_LAST_SECOND)).toBe("12")
147
+ end)
148
+ end)
149
+
150
+ describe("Time.getDayOfTheMonth", function()
151
+ it("should be the 1st at the epoch", function()
152
+ expect(Time.getDayOfTheMonth(EPOCH)).toBe(1)
153
+ end)
154
+
155
+ it("should handle the leap day boundary", function()
156
+ expect(Time.getDayOfTheMonth(Y2000_FEB_29)).toBe(29)
157
+ expect(Time.getDayOfTheMonth(Y2000_MAR_1)).toBe(1)
158
+ end)
159
+
160
+ it("should be the 31st at the end of the year", function()
161
+ expect(Time.getDayOfTheMonth(Y2000_LAST_SECOND)).toBe(31)
162
+ end)
163
+
164
+ it("should compute the day of the month", function()
165
+ expect(Time.getDayOfTheMonth(Y2025_SEP_22)).toBe(22)
166
+ expect(Time.getDayOfTheMonth(Y2025_SEP_22_LAST_SECOND)).toBe(22)
167
+ end)
168
+
169
+ it("should zero pad the formatted day of the month", function()
170
+ expect(Time.getFormattedDayOfTheMonth(EPOCH)).toBe("01")
171
+ expect(Time.getFormattedDayOfTheMonth(Y2000_FEB_29)).toBe("29")
172
+ end)
173
+ end)
174
+
175
+ describe("Time.getMonthName", function()
176
+ it("should return the full month name", function()
177
+ expect(Time.getMonthName(EPOCH)).toBe("January")
178
+ expect(Time.getMonthName(Y2000_FEB_29)).toBe("February")
179
+ expect(Time.getMonthName(Y2025_SEP_22)).toBe("September")
180
+ expect(Time.getMonthName(Y2000_LAST_SECOND)).toBe("December")
181
+ end)
182
+
183
+ it("should return the short month name", function()
184
+ expect(Time.getMonthNameShort(EPOCH)).toBe("Jan")
185
+ expect(Time.getMonthNameShort(Y2000_FEB_29)).toBe("Feb")
186
+ expect(Time.getMonthNameShort(Y2025_SEP_22)).toBe("Sep")
187
+ expect(Time.getMonthNameShort(Y2000_LAST_SECOND)).toBe("Dec")
188
+ end)
189
+ end)
190
+
191
+ describe("Time.getJulianDate", function()
192
+ it("should be the Julian day number of the epoch", function()
193
+ expect(Time.getJulianDate(EPOCH)).toBe(2440588)
194
+ end)
195
+
196
+ it("should compute the Julian day number", function()
197
+ expect(Time.getJulianDate(Y2024_JAN_1)).toBe(2460311)
198
+ expect(Time.getJulianDate(Y2025_SEP_22)).toBe(2460941)
199
+ end)
200
+
201
+ it("should not change within a day", function()
202
+ expect(Time.getJulianDate(Y2025_SEP_22_LAST_SECOND)).toBe(Time.getJulianDate(Y2025_SEP_22))
203
+ end)
204
+ end)
205
+
206
+ describe("Time.getDayOfTheWeek", function()
207
+ it("should number the week from Sunday as 0", function()
208
+ expect(Time.getDayOfTheWeek(Y2025_SEP_7)).toBe(0)
209
+ expect(Time.getDayOfTheWeek(Y2025_SEP_8)).toBe(1)
210
+ expect(Time.getDayOfTheWeek(EPOCH)).toBe(4)
211
+ expect(Time.getDayOfTheWeek(Y2005_JAN_1)).toBe(6)
212
+ end)
213
+
214
+ it("should return the full day name", function()
215
+ expect(Time.getDayOfTheWeekName(Y2025_SEP_7)).toBe("Sunday")
216
+ expect(Time.getDayOfTheWeekName(Y2025_SEP_8)).toBe("Monday")
217
+ expect(Time.getDayOfTheWeekName(EPOCH)).toBe("Thursday")
218
+ expect(Time.getDayOfTheWeekName(Y2005_JAN_1)).toBe("Saturday")
219
+ end)
220
+
221
+ it("should return the short day name", function()
222
+ expect(Time.getDayOfTheWeekNameShort(Y2025_SEP_7)).toBe("Sun")
223
+ expect(Time.getDayOfTheWeekNameShort(Y2025_SEP_8)).toBe("Mon")
224
+ expect(Time.getDayOfTheWeekNameShort(EPOCH)).toBe("Thu")
225
+ expect(Time.getDayOfTheWeekNameShort(Y2005_JAN_1)).toBe("Sat")
226
+ end)
227
+ end)
228
+
229
+ describe("Time.getOrdinalOfNumber", function()
230
+ it("should use st, nd, rd for 1, 2, 3", function()
231
+ expect(Time.getOrdinalOfNumber(1)).toBe("st")
232
+ expect(Time.getOrdinalOfNumber(2)).toBe("nd")
233
+ expect(Time.getOrdinalOfNumber(3)).toBe("rd")
234
+ expect(Time.getOrdinalOfNumber(4)).toBe("th")
235
+ end)
236
+
237
+ it("should use th for the teens", function()
238
+ expect(Time.getOrdinalOfNumber(11)).toBe("th")
239
+ expect(Time.getOrdinalOfNumber(12)).toBe("th")
240
+ expect(Time.getOrdinalOfNumber(13)).toBe("th")
241
+ expect(Time.getOrdinalOfNumber(111)).toBe("th")
242
+ expect(Time.getOrdinalOfNumber(112)).toBe("th")
243
+ end)
244
+
245
+ it("should use st, nd, rd for 21, 22, 23", function()
246
+ expect(Time.getOrdinalOfNumber(21)).toBe("st")
247
+ expect(Time.getOrdinalOfNumber(22)).toBe("nd")
248
+ expect(Time.getOrdinalOfNumber(23)).toBe("rd")
249
+ expect(Time.getOrdinalOfNumber(31)).toBe("st")
250
+ expect(Time.getOrdinalOfNumber(101)).toBe("st")
251
+ end)
252
+
253
+ it("should return the ordinal of the day of the month", function()
254
+ expect(Time.getDayOfTheMonthOrdinal(EPOCH)).toBe("st")
255
+ expect(Time.getDayOfTheMonthOrdinal(Y2025_SEP_22)).toBe("nd")
256
+ expect(Time.getDayOfTheMonthOrdinal(Y2000_LAST_SECOND)).toBe("st")
257
+ end)
258
+ end)
259
+
260
+ describe("Time formatted clock components", function()
261
+ it("should zero pad seconds and minutes", function()
262
+ expect(Time.getFormattedSecond(EPOCH)).toBe("00")
263
+ expect(Time.getFormattedSecond(65)).toBe("05")
264
+ expect(Time.getFormattedSecond(Y2025_SEP_22_LAST_SECOND)).toBe("59")
265
+ expect(Time.getFormattedMinute(EPOCH)).toBe("00")
266
+ expect(Time.getFormattedMinute(300)).toBe("05")
267
+ expect(Time.getFormattedMinute(Y2025_SEP_22_LAST_SECOND)).toBe("59")
268
+ end)
269
+
270
+ it("should zero pad the 24-hour hour", function()
271
+ expect(Time.getHourFormatted(EPOCH)).toBe("00")
272
+ expect(Time.getHourFormatted(Y2025_SEP_22_13_00)).toBe("13")
273
+ expect(Time.getMilitaryHour(EPOCH)).toBe("00")
274
+ expect(Time.getMilitaryHour(Y2025_SEP_22_LAST_SECOND)).toBe("23")
275
+ end)
276
+ end)
277
+
278
+ describe("Time.getRegularHour", function()
279
+ it("should report midnight as 12", function()
280
+ expect(Time.getRegularHour(EPOCH)).toBe(12)
281
+ end)
282
+
283
+ it("should report noon as 12", function()
284
+ expect(Time.getRegularHour(Y2025_SEP_22_NOON)).toBe(12)
285
+ end)
286
+
287
+ it("should wrap the afternoon to 1-11", function()
288
+ expect(Time.getRegularHour(Y2025_SEP_22_13_00)).toBe(1)
289
+ expect(Time.getRegularHour(Y2025_SEP_22_LAST_SECOND)).toBe(11)
290
+ end)
291
+
292
+ it("should keep the morning as 1-11", function()
293
+ expect(Time.getRegularHour(Y2025_SEP_22_11_59_59)).toBe(11)
294
+ end)
295
+
296
+ it("should zero pad the 12-hour hour", function()
297
+ expect(Time.getRegularHourFormatted(Y2025_SEP_22_13_00)).toBe("01")
298
+ expect(Time.getRegularHourFormatted(Y2025_SEP_22_NOON)).toBe("12")
299
+ end)
300
+ end)
301
+
302
+ describe("Time.getamOrpm", function()
303
+ it("should be am before noon", function()
304
+ expect(Time.getamOrpm(EPOCH)).toBe("am")
305
+ expect(Time.getamOrpm(Y2025_SEP_22_11_59_59)).toBe("am")
306
+ expect(Time.getAMorPM(EPOCH)).toBe("AM")
307
+ expect(Time.getAMorPM(Y2025_SEP_22_11_59_59)).toBe("AM")
308
+ end)
309
+
310
+ it("should be pm from noon onwards", function()
311
+ expect(Time.getamOrpm(Y2025_SEP_22_NOON)).toBe("pm")
312
+ expect(Time.getamOrpm(Y2025_SEP_22_LAST_SECOND)).toBe("pm")
313
+ expect(Time.getAMorPM(Y2025_SEP_22_NOON)).toBe("PM")
314
+ expect(Time.getAMorPM(Y2025_SEP_22_LAST_SECOND)).toBe("PM")
315
+ end)
316
+ end)
317
+
318
+ describe("Time.isLeapYear", function()
319
+ it("should detect leap years", function()
320
+ expect(Time.isLeapYear(Y2000_FEB_29)).toBe(true)
321
+ expect(Time.isLeapYear(Y2024_JAN_1)).toBe(true)
322
+ end)
323
+
324
+ it("should detect common years", function()
325
+ expect(Time.isLeapYear(Y2005_JAN_1)).toBe(false)
326
+ expect(Time.isLeapYear(Y2025_SEP_22)).toBe(false)
327
+ end)
328
+ end)
329
+
330
+ describe("Time.getDaysInMonth", function()
331
+ it("should return the days in the month of the given time", function()
332
+ expect(Time.getDaysInMonth(EPOCH)).toBe(31)
333
+ expect(Time.getDaysInMonth(Y2024_FEB_29)).toBe(29)
334
+ expect(Time.getDaysInMonth(Y2025_SEP_22)).toBe(30)
335
+ expect(Time.getDaysInMonth(Y2000_LAST_SECOND)).toBe(31)
336
+ end)
337
+ end)
338
+
339
+ describe("Time.format", function()
340
+ it("should pass Roblox tokens through to FormatUniversalTime", function()
341
+ expect(Time.format("YYYY-MM-DD HH:mm:ss", Y2025_SEP_22_LAST_SECOND)).toBe("2025-09-22 23:59:59")
342
+ expect(Time.format("YYYY-MM-DD HH:mm:ss", EPOCH)).toBe("1970-01-01 00:00:00")
343
+ expect(Time.format("dddd, MMMM D YYYY", Y2025_SEP_22)).toBe("Monday, September 22 2025")
344
+ expect(Time.format("ddd MMM D", Y2005_JAN_1)).toBe("Sat Jan 1")
345
+ expect(Time.format("LL", Y2025_SEP_22)).toBe("September 22, 2025")
346
+ end)
347
+
348
+ it("should format a 12-hour clock", function()
349
+ expect(Time.format("h:mm a", Y2025_SEP_22_13_00)).toBe("1:00 pm")
350
+ expect(Time.format("hh:mm A", Y2025_SEP_22_NOON)).toBe("12:00 PM")
351
+ expect(Time.format("h:mm a", EPOCH)).toBe("12:00 am")
352
+ end)
353
+
354
+ it("should format the short year and day of the year", function()
355
+ expect(Time.format("YY", Y2005_JAN_1)).toBe("05")
356
+ expect(Time.format("DDD", Y2024_LAST_SECOND)).toBe("366")
357
+ end)
358
+
359
+ it("should add ordinal suffixes", function()
360
+ expect(Time.format("Do", Y2025_SEP_22)).toBe("22nd")
361
+ expect(Time.format("Do", EPOCH)).toBe("1st")
362
+ expect(Time.format("Do", Y2000_LAST_SECOND)).toBe("31st")
363
+ expect(Time.format("DDDo", Y2025_SEP_22)).toBe("265th")
364
+ expect(Time.format("MMMM Do, YYYY", Y2025_SEP_22)).toBe("September 22nd, 2025")
365
+ end)
366
+
367
+ it("should format days in month, leap year, unix timestamp and Julian day", function()
368
+ expect(Time.format("t", Y2025_SEP_22)).toBe("30")
369
+ expect(Time.format("t", Y2024_FEB_29)).toBe("29")
370
+ expect(Time.format("LY", Y2024_FEB_29)).toBe("true")
371
+ expect(Time.format("LY", Y2025_SEP_22)).toBe("false")
372
+ expect(Time.format("X", Y2025_SEP_22)).toBe(tostring(Y2025_SEP_22))
373
+ expect(Time.format("J", Y2025_SEP_22)).toBe("2460941")
374
+ end)
375
+
376
+ it("should mix extension tokens with Roblox tokens", function()
377
+ expect(Time.format("dddd Do [of] MMMM (t [days])", Y2025_SEP_22)).toBe("Monday 22nd of September (30 days)")
378
+ expect(Time.format("D/t LY", Y2024_FEB_29)).toBe("29/29 true")
379
+ end)
380
+
381
+ it("should leave punctuation alone", function()
382
+ expect(Time.format("(YYYY) -- /MM/", Y2024_JAN_1)).toBe("(2024) -- /01/")
383
+ expect(Time.format("", Y2024_JAN_1)).toBe("")
384
+ end)
385
+
386
+ it("should treat bracketed text as literal", function()
387
+ expect(Time.format("[YYYY] YYYY", Y2024_JAN_1)).toBe("YYYY 2024")
388
+ expect(Time.format("[Do] Do", Y2025_SEP_22)).toBe("Do 22nd")
389
+ expect(Time.format("[Today is] dddd", Y2025_SEP_22)).toBe("Today is Monday")
390
+ end)
391
+
392
+ it("should default to the current time", function()
393
+ local expected = os.date("!%Y", os.time())
394
+
395
+ expect(Time.format("YYYY")).toBe(expected)
396
+ end)
397
+ end)
398
+
399
+ describe("Time with a locale", function()
400
+ it("should default to en-us", function()
401
+ expect(Time.getMonthName(Y2025_SEP_22)).toBe(Time.getMonthName(Y2025_SEP_22, "en-us"))
402
+ expect(Time.getDayOfTheMonthOrdinal(Y2025_SEP_22)).toBe(Time.getDayOfTheMonthOrdinal(Y2025_SEP_22, "en-us"))
403
+ end)
404
+
405
+ it("should localize ordinals", function()
406
+ expect(Time.getOrdinalOfNumber(22, "fr-fr")).toBe("e")
407
+ expect(Time.getOrdinalOfNumber(1, "fr-fr")).toBe("er")
408
+ expect(Time.getDayOfTheMonthOrdinal(Y2025_SEP_22, "de-de")).toBe(".")
409
+ expect(Time.format("Do", Y2025_SEP_22, "de-de")).toBe("22.")
410
+ expect(Time.format("Do", Y2025_SEP_22, "zh-cn")).toBe("第22")
411
+ end)
412
+
413
+ it("should localize names", function()
414
+ expect(Time.getMonthName(Y2025_SEP_22, "fr-fr")).toBe("septembre")
415
+ expect(Time.getDayOfTheWeekName(Y2025_SEP_22, "fr-fr")).toBe("lundi")
416
+ expect(Time.getMonthName(Y2025_SEP_22, "de-de")).toBe("September")
417
+ expect(Time.getDayOfTheWeekName(Y2025_SEP_22, "de-de")).toBe("Montag")
418
+ end)
419
+
420
+ it("should localize format output while keeping extension tokens", function()
421
+ expect(Time.format("dddd Do MMMM YYYY", Y2025_SEP_22, "fr-fr")).toBe("lundi 22e septembre 2025")
422
+ expect(Time.format("LL", Y2025_SEP_22, "en-gb")).toBe("22 September 2025")
423
+ end)
424
+
425
+ it("should keep padded numbers stable across locales", function()
426
+ expect(Time.getFormattedMonth(Y2025_SEP_22, "fr-fr")).toBe("09")
427
+ expect(Time.getHourFormatted(Y2025_SEP_22_13_00, "fr-fr")).toBe("13")
428
+ expect(Time.getFormattedSecond(Y2025_SEP_22_LAST_SECOND, "de-de")).toBe("59")
429
+ end)
430
+ end)
431
+
432
+ describe("Time with DateTime inputs", function()
433
+ it("should accept a DateTime anywhere a timestamp is accepted", function()
434
+ local dateTime = DateTime.fromUnixTimestamp(Y2025_SEP_22_13_00)
435
+
436
+ expect(Time.getYear(dateTime)).toBe(2025)
437
+ expect(Time.getMonth(dateTime)).toBe(9)
438
+ expect(Time.getDayOfTheMonth(dateTime)).toBe(22)
439
+ expect(Time.getHour(dateTime)).toBe(13)
440
+ expect(Time.getDayOfTheWeekName(dateTime)).toBe("Monday")
441
+ expect(Time.format("YYYY-MM-DD h:mm a", dateTime)).toBe("2025-09-22 1:00 pm")
442
+ end)
443
+
444
+ it("should agree between a DateTime and its timestamp", function()
445
+ local dateTime = DateTime.fromUnixTimestamp(Y2000_LAST_SECOND)
446
+
447
+ expect(Time.format("dddd, MMMM Do YYYY HH:mm:ss", dateTime)).toBe(
448
+ Time.format("dddd, MMMM Do YYYY HH:mm:ss", Y2000_LAST_SECOND)
449
+ )
450
+ end)
451
+
452
+ it("should accept an ISO 8601 string", function()
453
+ expect(Time.getUnixTimestamp("2025-09-22T13:00:00Z")).toBe(Y2025_SEP_22_13_00)
454
+ expect(Time.format("YYYY-MM-DD HH:mm", "2025-09-22T13:00:00Z")).toBe("2025-09-22 13:00")
455
+ expect(Time.getDayOfTheWeekName("2000-12-31T23:59:59Z")).toBe("Sunday")
456
+ end)
457
+
458
+ it("should reject a string that is not an ISO date", function()
459
+ expect(function()
460
+ Time.getYear("yesterday")
461
+ end).toThrow("Bad ISO date")
462
+ end)
463
+
464
+ it("should use the current time for nil", function()
465
+ local now = DateTime.now()
466
+
467
+ expect(Time.getYear(nil)).toBe(Time.getYear(now))
468
+ expect(math.abs(Time.getUnixTimestamp(nil) - now.UnixTimestamp) <= 1).toBe(true)
469
+ end)
470
+
471
+ it("should return the unix timestamp", function()
472
+ expect(Time.getUnixTimestamp(Y2025_SEP_22)).toBe(Y2025_SEP_22)
473
+ expect(Time.getUnixTimestamp(DateTime.fromUnixTimestamp(Y2025_SEP_22))).toBe(Y2025_SEP_22)
474
+ expect(Time.getUnixTimestamp(Y2025_SEP_22 + 0.75)).toBe(Y2025_SEP_22)
475
+ end)
476
+ end)
477
+
478
+ describe("Time.add", function()
479
+ local function iso(dateTime: DateTime): string
480
+ return dateTime:ToIsoDate()
481
+ end
482
+
483
+ it("should add fixed units", function()
484
+ expect(iso(Time.add(Y2025_SEP_22, 10, "day"))).toBe("2025-10-02T00:00:00Z")
485
+ expect(iso(Time.add(Y2025_SEP_22, 2, "week"))).toBe("2025-10-06T00:00:00Z")
486
+ expect(iso(Time.add(Y2025_SEP_22, 25, "hour"))).toBe("2025-09-23T01:00:00Z")
487
+ expect(iso(Time.add(Y2025_SEP_22, 90, "minute"))).toBe("2025-09-22T01:30:00Z")
488
+ expect(iso(Time.add(Y2025_SEP_22, 1.5, "second"))).toBe("2025-09-22T00:00:01Z")
489
+ expect(Time.add(Y2025_SEP_22, 250, "millisecond").UnixTimestampMillis).toBe(Y2025_SEP_22 * 1000 + 250)
490
+ end)
491
+
492
+ it("should move months on the calendar and clamp the day", function()
493
+ expect(iso(Time.add("2025-01-31T12:30:00Z", 1, "month"))).toBe("2025-02-28T12:30:00Z")
494
+ expect(iso(Time.add("2024-01-31T00:00:00Z", 1, "month"))).toBe("2024-02-29T00:00:00Z")
495
+ expect(iso(Time.add("2025-12-15T00:00:00Z", 1, "month"))).toBe("2026-01-15T00:00:00Z")
496
+ expect(iso(Time.add("2025-01-15T00:00:00Z", -1, "month"))).toBe("2024-12-15T00:00:00Z")
497
+ expect(iso(Time.add("2025-03-31T00:00:00Z", -1, "month"))).toBe("2025-02-28T00:00:00Z")
498
+ expect(iso(Time.add(Y2025_SEP_22, 14, "month"))).toBe("2026-11-22T00:00:00Z")
499
+ end)
500
+
501
+ it("should move quarters and years on the calendar", function()
502
+ expect(iso(Time.add(Y2025_SEP_22, 1, "quarter"))).toBe("2025-12-22T00:00:00Z")
503
+ expect(iso(Time.add("2024-02-29T00:00:00Z", 1, "year"))).toBe("2025-02-28T00:00:00Z")
504
+ expect(iso(Time.add("2024-02-29T00:00:00Z", 4, "years"))).toBe("2028-02-29T00:00:00Z")
505
+ end)
506
+
507
+ it("should accept plural and short units", function()
508
+ expect(iso(Time.add(Y2025_SEP_22, 1, "days"))).toBe(iso(Time.add(Y2025_SEP_22, 1, "d")))
509
+ expect(iso(Time.add(Y2025_SEP_22, 1, "M"))).toBe(iso(Time.add(Y2025_SEP_22, 1, "month")))
510
+ expect(iso(Time.add(Y2025_SEP_22, 1, "m"))).toBe(iso(Time.add(Y2025_SEP_22, 1, "minute")))
511
+ expect(iso(Time.add(Y2025_SEP_22, 1, "y"))).toBe(iso(Time.add(Y2025_SEP_22, 1, "year")))
512
+ expect(iso(Time.add(Y2025_SEP_22, 1, "Q"))).toBe(iso(Time.add(Y2025_SEP_22, 3, "months")))
513
+ end)
514
+
515
+ it("should reject unknown units", function()
516
+ expect(function()
517
+ Time.add(Y2025_SEP_22, 1, "fortnight" :: any)
518
+ end).toThrow("Bad unit")
519
+ end)
520
+
521
+ it("should return a DateTime usable everywhere else", function()
522
+ expect(Time.format("dddd Do MMMM", Time.add(Y2025_SEP_22, 1, "day"))).toBe("Tuesday 23rd September")
523
+ expect(Time.getDayOfTheWeekName(Time.add(nil, 0, "day"))).toBe(Time.getDayOfTheWeekName(nil))
524
+ end)
525
+ end)
526
+
527
+ describe("Time.subtract", function()
528
+ it("should be add with a negated value", function()
529
+ expect(Time.subtract(Y2025_SEP_22, 1, "day"):ToIsoDate()).toBe("2025-09-21T00:00:00Z")
530
+ expect(Time.subtract("2025-03-31T00:00:00Z", 1, "month"):ToIsoDate()).toBe("2025-02-28T00:00:00Z")
531
+ expect(Time.subtract(Y2025_SEP_22, -1, "hour"):ToIsoDate()).toBe("2025-09-22T01:00:00Z")
532
+ end)
533
+ end)
534
+
535
+ describe("Time.startOf / endOf", function()
536
+ local SAMPLE = DateTime.fromUnixTimestampMillis(1758548730250) -- 2025-09-22T13:45:30.250Z, a Monday
537
+
538
+ local function iso(dateTime: DateTime): string
539
+ return dateTime:ToIsoDate()
540
+ end
541
+
542
+ it("should snap to the start of clock units", function()
543
+ expect(Time.startOf(SAMPLE, "millisecond").UnixTimestampMillis).toBe(SAMPLE.UnixTimestampMillis)
544
+ expect(Time.startOf(SAMPLE, "second").UnixTimestampMillis).toBe(1758548730000)
545
+ expect(iso(Time.startOf(SAMPLE, "minute"))).toBe("2025-09-22T13:45:00Z")
546
+ expect(iso(Time.startOf(SAMPLE, "hour"))).toBe("2025-09-22T13:00:00Z")
547
+ expect(iso(Time.startOf(SAMPLE, "day"))).toBe("2025-09-22T00:00:00Z")
548
+ expect(iso(Time.startOf(SAMPLE, "date"))).toBe("2025-09-22T00:00:00Z")
549
+ end)
550
+
551
+ it("should snap to the start of calendar units", function()
552
+ expect(iso(Time.startOf(SAMPLE, "week"))).toBe("2025-09-21T00:00:00Z")
553
+ expect(iso(Time.startOf("2025-09-21T12:00:00Z", "week"))).toBe("2025-09-21T00:00:00Z")
554
+ expect(iso(Time.startOf("2025-09-27T12:00:00Z", "week"))).toBe("2025-09-21T00:00:00Z")
555
+ expect(iso(Time.startOf(SAMPLE, "month"))).toBe("2025-09-01T00:00:00Z")
556
+ expect(iso(Time.startOf(SAMPLE, "quarter"))).toBe("2025-07-01T00:00:00Z")
557
+ expect(iso(Time.startOf("2025-12-31T23:59:59Z", "quarter"))).toBe("2025-10-01T00:00:00Z")
558
+ expect(iso(Time.startOf(SAMPLE, "year"))).toBe("2025-01-01T00:00:00Z")
559
+ end)
560
+
561
+ it("should end one millisecond before the next unit starts", function()
562
+ expect(Time.endOf(SAMPLE, "second").UnixTimestampMillis).toBe(1758548730999)
563
+ expect(Time.endOf(SAMPLE, "day").UnixTimestampMillis).toBe(
564
+ Time.startOf("2025-09-23T00:00:00Z", "day").UnixTimestampMillis - 1
565
+ )
566
+ expect(iso(Time.endOf(SAMPLE, "day"))).toBe("2025-09-22T23:59:59Z")
567
+ expect(iso(Time.endOf(SAMPLE, "week"))).toBe("2025-09-27T23:59:59Z")
568
+ expect(iso(Time.endOf(SAMPLE, "month"))).toBe("2025-09-30T23:59:59Z")
569
+ expect(iso(Time.endOf("2024-02-10T00:00:00Z", "month"))).toBe("2024-02-29T23:59:59Z")
570
+ expect(iso(Time.endOf(SAMPLE, "quarter"))).toBe("2025-09-30T23:59:59Z")
571
+ expect(iso(Time.endOf(SAMPLE, "year"))).toBe("2025-12-31T23:59:59Z")
572
+ end)
573
+
574
+ it("should accept short units and any DateTimeLike", function()
575
+ expect(iso(Time.startOf(Y2025_SEP_22_13_00, "M"))).toBe("2025-09-01T00:00:00Z")
576
+ expect(iso(Time.endOf("2025-09-22T13:00:00Z", "y"))).toBe("2025-12-31T23:59:59Z")
577
+ expect(Time.getHour(Time.startOf(nil, "day"))).toBe(0)
578
+ end)
579
+ end)
580
+
581
+ describe("Time.get", function()
582
+ local SAMPLE = DateTime.fromUnixTimestampMillis(1758548730250) -- 2025-09-22T13:45:30.250Z, a Monday
583
+
584
+ it("should read every field", function()
585
+ expect(Time.get(SAMPLE, "year")).toBe(2025)
586
+ expect(Time.get(SAMPLE, "month")).toBe(9)
587
+ expect(Time.get(SAMPLE, "date")).toBe(22)
588
+ expect(Time.get(SAMPLE, "day")).toBe(1)
589
+ expect(Time.get(SAMPLE, "hour")).toBe(13)
590
+ expect(Time.get(SAMPLE, "minute")).toBe(45)
591
+ expect(Time.get(SAMPLE, "second")).toBe(30)
592
+ expect(Time.get(SAMPLE, "millisecond")).toBe(250)
593
+ end)
594
+
595
+ it("should accept plural and short fields", function()
596
+ expect(Time.get(SAMPLE, "y")).toBe(2025)
597
+ expect(Time.get(SAMPLE, "M")).toBe(9)
598
+ expect(Time.get(SAMPLE, "D")).toBe(22)
599
+ expect(Time.get(SAMPLE, "d")).toBe(1)
600
+ expect(Time.get(SAMPLE, "h")).toBe(13)
601
+ expect(Time.get(SAMPLE, "m")).toBe(45)
602
+ expect(Time.get(SAMPLE, "s")).toBe(30)
603
+ expect(Time.get(SAMPLE, "ms")).toBe(250)
604
+ expect(Time.get(SAMPLE, "hours")).toBe(13)
605
+ end)
606
+
607
+ it("should accept any DateTimeLike", function()
608
+ expect(Time.get("2000-12-31T23:59:59Z", "day")).toBe(0)
609
+ expect(Time.get(Y2025_SEP_22_13_00, "hour")).toBe(13)
610
+ end)
611
+
612
+ it("should reject unknown fields", function()
613
+ expect(function()
614
+ Time.get(SAMPLE, "week" :: any)
615
+ end).toThrow("Bad field")
616
+ end)
617
+ end)
618
+
619
+ describe("Time.set", function()
620
+ local SAMPLE = "2025-09-22T13:45:30Z" -- a Monday
621
+
622
+ local function iso(dateTime: DateTime): string
623
+ return dateTime:ToIsoDate()
624
+ end
625
+
626
+ it("should replace calendar fields", function()
627
+ expect(iso(Time.set(SAMPLE, "year", 2030))).toBe("2030-09-22T13:45:30Z")
628
+ expect(iso(Time.set(SAMPLE, "month", 12))).toBe("2025-12-22T13:45:30Z")
629
+ expect(iso(Time.set(SAMPLE, "date", 1))).toBe("2025-09-01T13:45:30Z")
630
+ end)
631
+
632
+ it("should roll calendar fields over like JavaScript dates", function()
633
+ expect(iso(Time.set(SAMPLE, "month", 13))).toBe("2026-01-22T13:45:30Z")
634
+ expect(iso(Time.set(SAMPLE, "month", 0))).toBe("2024-12-22T13:45:30Z")
635
+ expect(iso(Time.set("2025-01-31T00:00:00Z", "month", 2))).toBe("2025-03-03T00:00:00Z")
636
+ expect(iso(Time.set("2024-02-29T00:00:00Z", "year", 2025))).toBe("2025-03-01T00:00:00Z")
637
+ expect(iso(Time.set(SAMPLE, "date", 31))).toBe("2025-10-01T13:45:30Z")
638
+ expect(iso(Time.set(SAMPLE, "date", 0))).toBe("2025-08-31T13:45:30Z")
639
+ end)
640
+
641
+ it("should move within the week when setting the day", function()
642
+ expect(iso(Time.set(SAMPLE, "day", 0))).toBe("2025-09-21T13:45:30Z")
643
+ expect(iso(Time.set(SAMPLE, "day", 1))).toBe("2025-09-22T13:45:30Z")
644
+ expect(iso(Time.set(SAMPLE, "day", 6))).toBe("2025-09-27T13:45:30Z")
645
+ expect(iso(Time.set(SAMPLE, "day", 8))).toBe("2025-09-29T13:45:30Z")
646
+ end)
647
+
648
+ it("should replace and roll clock fields", function()
649
+ expect(iso(Time.set(SAMPLE, "hour", 0))).toBe("2025-09-22T00:45:30Z")
650
+ expect(iso(Time.set(SAMPLE, "hour", 25))).toBe("2025-09-23T01:45:30Z")
651
+ expect(iso(Time.set(SAMPLE, "minute", 0))).toBe("2025-09-22T13:00:30Z")
652
+ expect(iso(Time.set(SAMPLE, "second", 90))).toBe("2025-09-22T13:46:30Z")
653
+ expect(Time.set(SAMPLE, "millisecond", 250).UnixTimestampMillis).toBe(1758548730250)
654
+ end)
655
+
656
+ it("should round trip through get", function()
657
+ for _, field in { "year", "month", "date", "day", "hour", "minute", "second", "millisecond" } do
658
+ local value = Time.get(SAMPLE, field :: Time.TimeField)
659
+ expect(Time.get(Time.set(SAMPLE, field :: Time.TimeField, value), field :: Time.TimeField)).toBe(value)
660
+ end
661
+
662
+ expect(Time.get(Time.set(SAMPLE, "h", 7), "hour")).toBe(7)
663
+ end)
664
+ end)
665
+
666
+ describe("Time setters", function()
667
+ local SAMPLE = "2025-09-22T13:45:30Z" -- a Monday, day 265
668
+
669
+ local function iso(dateTime: DateTime): string
670
+ return dateTime:ToIsoDate()
671
+ end
672
+
673
+ it("should mirror the clock getters", function()
674
+ expect(iso(Time.setSecond(SAMPLE, 5))).toBe("2025-09-22T13:45:05Z")
675
+ expect(iso(Time.setMinute(SAMPLE, 5))).toBe("2025-09-22T13:05:30Z")
676
+ expect(iso(Time.setHour(SAMPLE, 5))).toBe("2025-09-22T05:45:30Z")
677
+ expect(Time.getSecond(Time.setSecond(SAMPLE, 5))).toBe(5)
678
+ expect(Time.getMinute(Time.setMinute(SAMPLE, 5))).toBe(5)
679
+ expect(Time.getHour(Time.setHour(SAMPLE, 5))).toBe(5)
680
+ end)
681
+
682
+ it("should keep the half of the day when setting the 12-hour hour", function()
683
+ expect(iso(Time.setRegularHour(SAMPLE, 3))).toBe("2025-09-22T15:45:30Z")
684
+ expect(iso(Time.setRegularHour(SAMPLE, 12))).toBe("2025-09-22T12:45:30Z")
685
+ expect(iso(Time.setRegularHour("2025-09-22T01:00:00Z", 3))).toBe("2025-09-22T03:00:00Z")
686
+ expect(iso(Time.setRegularHour("2025-09-22T01:00:00Z", 12))).toBe("2025-09-22T00:00:00Z")
687
+ expect(Time.getRegularHour(Time.setRegularHour(SAMPLE, 3))).toBe(3)
688
+ end)
689
+
690
+ it("should mirror the calendar getters", function()
691
+ expect(iso(Time.setYear(SAMPLE, 2030))).toBe("2030-09-22T13:45:30Z")
692
+ expect(iso(Time.setMonth(SAMPLE, 2))).toBe("2025-02-22T13:45:30Z")
693
+ expect(iso(Time.setDayOfTheMonth(SAMPLE, 30))).toBe("2025-09-30T13:45:30Z")
694
+ expect(iso(Time.setDayOfTheWeek(SAMPLE, 5))).toBe("2025-09-26T13:45:30Z")
695
+ expect(Time.getYear(Time.setYear(SAMPLE, 2030))).toBe(2030)
696
+ expect(Time.getMonth(Time.setMonth(SAMPLE, 2))).toBe(2)
697
+ expect(Time.getDayOfTheMonth(Time.setDayOfTheMonth(SAMPLE, 30))).toBe(30)
698
+ expect(Time.getDayOfTheWeek(Time.setDayOfTheWeek(SAMPLE, 5))).toBe(5)
699
+ end)
700
+
701
+ it("should set the day of the year and keep the clock", function()
702
+ expect(iso(Time.setDay(SAMPLE, 1))).toBe("2025-01-01T13:45:30Z")
703
+ expect(iso(Time.setDay(SAMPLE, 60))).toBe("2025-03-01T13:45:30Z")
704
+ expect(iso(Time.setDay("2024-09-22T13:45:30Z", 60))).toBe("2024-02-29T13:45:30Z")
705
+ expect(iso(Time.setDay(SAMPLE, 366))).toBe("2026-01-01T13:45:30Z")
706
+ expect(Time.getDay(Time.setDay(SAMPLE, 265))).toBe(265)
707
+ end)
708
+
709
+ it("should set the Julian day and keep the clock", function()
710
+ expect(iso(Time.setJulianDate(SAMPLE, 2440588))).toBe("1970-01-01T13:45:30Z")
711
+ expect(iso(Time.setJulianDate(SAMPLE, 2460941))).toBe("2025-09-22T13:45:30Z")
712
+ expect(Time.getJulianDate(Time.setJulianDate(SAMPLE, 2460311))).toBe(2460311)
713
+ end)
714
+ end)