@quenty/time 2.5.0 → 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,92 @@
1
+ --!strict
2
+ --[=[
3
+ Calendar time: describes a time relative to a
4
+ reference day with phrases like `Today at 2:30 PM`, `Tomorrow at 9:00 AM`, `Last Friday at
5
+ 5:00 PM`, and falls back to a plain date outside of a week either way.
6
+
7
+ ```lua
8
+ TimeCalendarUtils.calendar("2025-09-23T18:30:00Z", "2025-09-22T13:00:00Z") --> Tomorrow at 6:30 PM
9
+ TimeCalendarUtils.calendar(os.time() - 3 * 86400) --> Last Friday at 1:00 PM (for example)
10
+ ```
11
+
12
+ @class TimeCalendarUtils
13
+ ]=]
14
+
15
+ local require = require(script.Parent.loader).load(script)
16
+
17
+ local Time = require("Time")
18
+ local TimeLocalizationUtils = require("TimeLocalizationUtils")
19
+
20
+ local TimeCalendarUtils = {}
21
+
22
+ --[=[
23
+ See [TimeLocalizationUtils.CalendarFormat].
24
+
25
+ @type CalendarFormat TimeLocalizationUtils.CalendarFormat
26
+ @within TimeCalendarUtils
27
+ ]=]
28
+ export type CalendarFormat = TimeLocalizationUtils.CalendarFormat
29
+
30
+ --[=[
31
+ Which phrase to use for each distance from the reference day. Missing keys fall back to the
32
+ locale's defaults from [TimeLocalizationUtils.getCalendarFormatsForLocale].
33
+
34
+ @type CalendarFormats TimeLocalizationUtils.CalendarFormatOverrides
35
+ @within TimeCalendarUtils
36
+ ]=]
37
+ export type CalendarFormats = TimeLocalizationUtils.CalendarFormatOverrides
38
+
39
+ type CalendarFormatKey = "sameDay" | "nextDay" | "nextWeek" | "lastDay" | "lastWeek" | "sameElse"
40
+
41
+ local MILLISECONDS_A_DAY = 86400000
42
+
43
+ -- The distance in days from the start of the reference day picks the phrase
44
+ function TimeCalendarUtils._getFormatKey(daysFromReferenceStart: number): CalendarFormatKey
45
+ if daysFromReferenceStart < -6 then
46
+ return "sameElse"
47
+ elseif daysFromReferenceStart < -1 then
48
+ return "lastWeek"
49
+ elseif daysFromReferenceStart < 0 then
50
+ return "lastDay"
51
+ elseif daysFromReferenceStart < 1 then
52
+ return "sameDay"
53
+ elseif daysFromReferenceStart < 2 then
54
+ return "nextDay"
55
+ elseif daysFromReferenceStart < 7 then
56
+ return "nextWeek"
57
+ else
58
+ return "sameElse"
59
+ end
60
+ end
61
+
62
+ --[=[
63
+ Describes `currentTime` relative to the day of `referenceTime` (now when nil). Times within the same day, the next day, the previous day, the next six days and
64
+ the previous six days each get their own phrase; anything further is a plain date.
65
+
66
+ `formats` overrides any of the phrases, see [CalendarFormats]. The `locale` picks the
67
+ default phrases through [TimeLocalizationUtils] and applies to every template.
68
+ ]=]
69
+ function TimeCalendarUtils.calendar(
70
+ currentTime: Time.DateTimeLike,
71
+ referenceTime: Time.DateTimeLike,
72
+ formats: CalendarFormats?,
73
+ locale: string?
74
+ ): string
75
+ local dateTime = Time._toDateTime(currentTime)
76
+ local reference = Time._toDateTime(referenceTime)
77
+ local referenceStartOfDay = Time.startOf(reference, "day")
78
+
79
+ local daysFromReferenceStart = (dateTime.UnixTimestampMillis - referenceStartOfDay.UnixTimestampMillis)
80
+ / MILLISECONDS_A_DAY
81
+ local key = TimeCalendarUtils._getFormatKey(daysFromReferenceStart)
82
+
83
+ local localeFormats: any = TimeLocalizationUtils.getCalendarFormatsForLocale(locale)
84
+ local format: CalendarFormat = (if formats then (formats :: any)[key] else nil) or localeFormats[key]
85
+ if type(format) == "function" then
86
+ return format(dateTime, reference)
87
+ end
88
+
89
+ return Time.format(format, dateTime, locale)
90
+ end
91
+
92
+ return TimeCalendarUtils
@@ -0,0 +1,156 @@
1
+ --!strict
2
+ --[[
3
+ @class TimeCalendarUtils.spec.lua
4
+ ]]
5
+
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local Jest = require("Jest")
9
+ local TimeCalendarUtils = require("TimeCalendarUtils")
10
+
11
+ local describe = Jest.Globals.describe
12
+ local expect = Jest.Globals.expect
13
+ local it = Jest.Globals.it
14
+
15
+ local REFERENCE = "2025-09-22T13:00:00Z" -- a Monday
16
+
17
+ local function calendar(currentTime: string): string
18
+ return TimeCalendarUtils.calendar(currentTime, REFERENCE)
19
+ end
20
+
21
+ describe("TimeCalendarUtils.calendar", function()
22
+ it("should describe the reference day", function()
23
+ expect(calendar("2025-09-22T09:05:00Z")).toBe("Today at 9:05 AM")
24
+ expect(calendar("2025-09-22T00:00:00Z")).toBe("Today at 12:00 AM")
25
+ expect(calendar("2025-09-22T23:59:59Z")).toBe("Today at 11:59 PM")
26
+ end)
27
+
28
+ it("should describe the surrounding days", function()
29
+ expect(calendar("2025-09-23T18:30:00Z")).toBe("Tomorrow at 6:30 PM")
30
+ expect(calendar("2025-09-21T00:00:00Z")).toBe("Yesterday at 12:00 AM")
31
+ expect(calendar("2025-09-21T23:59:59Z")).toBe("Yesterday at 11:59 PM")
32
+ end)
33
+
34
+ it("should name the weekday within a week either way", function()
35
+ expect(calendar("2025-09-24T10:00:00Z")).toBe("Wednesday at 10:00 AM")
36
+ expect(calendar("2025-09-28T23:59:00Z")).toBe("Sunday at 11:59 PM")
37
+ expect(calendar("2025-09-20T08:00:00Z")).toBe("Last Saturday at 8:00 AM")
38
+ expect(calendar("2025-09-16T00:00:00Z")).toBe("Last Tuesday at 12:00 AM")
39
+ end)
40
+
41
+ it("should fall back to a date further out", function()
42
+ expect(calendar("2025-09-29T00:00:00Z")).toBe("09/29/2025")
43
+ expect(calendar("2025-09-15T23:59:59Z")).toBe("09/15/2025")
44
+ expect(calendar("2026-01-01T00:00:00Z")).toBe("01/01/2026")
45
+ end)
46
+
47
+ it("should measure from the start of the reference day", function()
48
+ expect(TimeCalendarUtils.calendar("2025-09-23T00:00:00Z", "2025-09-22T23:59:59Z")).toBe("Tomorrow at 12:00 AM")
49
+ expect(TimeCalendarUtils.calendar("2025-09-22T00:00:00Z", "2025-09-22T23:59:59Z")).toBe("Today at 12:00 AM")
50
+ end)
51
+
52
+ it("should default the reference to now", function()
53
+ expect(string.sub(TimeCalendarUtils.calendar(nil, nil), 1, 8)).toBe("Today at")
54
+ expect(string.sub(TimeCalendarUtils.calendar(DateTime.now().UnixTimestamp + 86400, nil), 1, 11)).toBe(
55
+ "Tomorrow at"
56
+ )
57
+ end)
58
+
59
+ it("should accept template overrides with fallbacks", function()
60
+ local formats = { sameDay = "[Today]", sameElse = "YYYY-MM-DD" }
61
+
62
+ expect(TimeCalendarUtils.calendar("2025-09-22T09:05:00Z", REFERENCE, formats)).toBe("Today")
63
+ expect(TimeCalendarUtils.calendar("2026-01-01T00:00:00Z", REFERENCE, formats)).toBe("2026-01-01")
64
+ expect(TimeCalendarUtils.calendar("2025-09-23T18:30:00Z", REFERENCE, formats)).toBe("Tomorrow at 6:30 PM")
65
+ end)
66
+
67
+ it("should accept function overrides", function()
68
+ local formats: TimeCalendarUtils.CalendarFormats = {
69
+ nextWeek = function(dateTime: DateTime, referenceTime: DateTime): string
70
+ return string.format("%d days out", (dateTime.UnixTimestamp - referenceTime.UnixTimestamp) // 86400)
71
+ end,
72
+ }
73
+
74
+ expect(TimeCalendarUtils.calendar("2025-09-25T13:00:00Z", REFERENCE, formats)).toBe("3 days out")
75
+ end)
76
+
77
+ it("should use the locale's phrases and templates", function()
78
+ local wednesday = TimeCalendarUtils.calendar("2025-09-24T10:00:00Z", REFERENCE, nil, "fr-fr")
79
+ local today = TimeCalendarUtils.calendar("2025-09-22T10:00:00Z", REFERENCE, nil, "es-es")
80
+ local yesterday = TimeCalendarUtils.calendar("2025-09-21T10:00:00Z", REFERENCE, nil, "de-de")
81
+ local tomorrow = TimeCalendarUtils.calendar("2025-09-23T10:00:00Z", REFERENCE, nil, "ja-jp")
82
+
83
+ expect(string.find(wednesday, "^mercredi à ") ~= nil).toBe(true)
84
+ expect(string.find(today, "^hoy a las ") ~= nil).toBe(true)
85
+ expect(string.find(yesterday, "^gestern um .* Uhr$") ~= nil).toBe(true)
86
+ expect(string.find(tomorrow, "^明日 ") ~= nil).toBe(true)
87
+ end)
88
+
89
+ it("should let overrides win over the locale", function()
90
+ expect(TimeCalendarUtils.calendar("2025-09-22T10:00:00Z", REFERENCE, { sameDay = "[hoy]" }, "es-es")).toBe(
91
+ "hoy"
92
+ )
93
+ end)
94
+ end)
95
+
96
+ describe("TimeCalendarUtils localization edge cases", function()
97
+ it("should apply the locale to string overrides", function()
98
+ expect(TimeCalendarUtils.calendar("2025-09-22T10:00:00Z", REFERENCE, { sameDay = "dddd" }, "fr-fr")).toBe(
99
+ "lundi"
100
+ )
101
+ expect(TimeCalendarUtils.calendar("2025-09-22T10:00:00Z", REFERENCE, { sameDay = "dddd" }, "de-de")).toBe(
102
+ "Montag"
103
+ )
104
+ end)
105
+
106
+ it("should fill missing overrides from the locale, not English", function()
107
+ local tomorrow = TimeCalendarUtils.calendar("2025-09-23T10:00:00Z", REFERENCE, { sameDay = "[x]" }, "es-es")
108
+
109
+ expect(string.find(tomorrow, "^mañana a las ") ~= nil).toBe(true)
110
+ end)
111
+
112
+ it("should localize the fallback date", function()
113
+ local american = TimeCalendarUtils.calendar("2026-01-05T00:00:00Z", REFERENCE, nil, "en-us")
114
+ local french = TimeCalendarUtils.calendar("2026-01-05T00:00:00Z", REFERENCE, nil, "fr-fr")
115
+
116
+ expect(american).toBe("01/05/2026")
117
+ expect(french).never.toBe(american)
118
+ expect(string.find(french, "^%d%d[/.]%d%d[/.]%d%d%d%d$") ~= nil).toBe(true)
119
+ end)
120
+
121
+ it("should keep languages that do not space before the time", function()
122
+ local today = TimeCalendarUtils.calendar("2025-09-22T10:00:00Z", REFERENCE, nil, "zh-cn")
123
+
124
+ expect(string.find(today, "^今天%d") ~= nil).toBe(true)
125
+ end)
126
+
127
+ it("should treat the zh-cjv alias like zh-cn", function()
128
+ expect(TimeCalendarUtils.calendar("2025-09-22T10:00:00Z", REFERENCE, nil, "zh-cjv")).toBe(
129
+ TimeCalendarUtils.calendar("2025-09-22T10:00:00Z", REFERENCE, nil, "zh-cn")
130
+ )
131
+ end)
132
+
133
+ it("should name the weekday in the locale for next and last week", function()
134
+ local next = TimeCalendarUtils.calendar("2025-09-25T10:00:00Z", REFERENCE, nil, "pt-br")
135
+ local last = TimeCalendarUtils.calendar("2025-09-18T10:00:00Z", REFERENCE, nil, "pt-br")
136
+
137
+ expect(string.find(next, "^Quinta%-feira às ") ~= nil).toBe(true)
138
+ expect(string.find(last, "^Quinta%-feira passado às ") ~= nil).toBe(true)
139
+ end)
140
+
141
+ it("should fall back to English for an unknown locale", function()
142
+ expect(TimeCalendarUtils.calendar("2025-09-22T09:05:00Z", REFERENCE, nil, "xx-yy")).toBe("Today at 9:05 AM")
143
+ end)
144
+
145
+ it("should give function overrides the raw times regardless of locale", function()
146
+ local formats: TimeCalendarUtils.CalendarFormats = {
147
+ sameDay = function(dateTime: DateTime, referenceTime: DateTime): string
148
+ return dateTime:ToIsoDate() .. " vs " .. referenceTime:ToIsoDate()
149
+ end,
150
+ }
151
+
152
+ expect(TimeCalendarUtils.calendar("2025-09-22T10:00:00Z", REFERENCE, formats, "ja-jp")).toBe(
153
+ "2025-09-22T10:00:00Z vs 2025-09-22T13:00:00Z"
154
+ )
155
+ end)
156
+ end)