@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.
- package/CHANGELOG.md +11 -0
- package/README.md +2 -69
- package/deploy.nevermore.json +10 -0
- package/package.json +12 -3
- package/src/Shared/RelativeTimeUtils.lua +220 -0
- package/src/Shared/RelativeTimeUtils.spec.lua +308 -0
- package/src/Shared/Time.lua +664 -352
- package/src/Shared/Time.spec.lua +714 -0
- package/src/Shared/TimeCalendarUtils.lua +92 -0
- package/src/Shared/TimeCalendarUtils.spec.lua +156 -0
- package/src/Shared/TimeDurationUtils.lua +890 -0
- package/src/Shared/TimeDurationUtils.spec.lua +362 -0
- package/src/Shared/TimeLocalizationUtils.lua +964 -0
- package/src/Shared/TimeLocalizationUtils.spec.lua +229 -0
- package/src/jest.config.lua +3 -0
- package/src/node_modules.project.json +7 -0
- package/test/default.project.json +17 -0
- package/test/scripts/Server/ServerMain.server.lua +12 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [2.6.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.5.0...@quenty/time@2.6.0) (2026-09-23)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- Resolve luau-lsp and prettier lint failures ([c6face3](https://github.com/Quenty/NevermoreEngine/commit/c6face3b716827345bf7f82406723a1b0388d59b))
|
|
11
|
+
- TIme duration allows for centiseconds ([d9d5408](https://github.com/Quenty/NevermoreEngine/commit/d9d54088b122f8f80df0165b9bd2db05df224827))
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
- Refactor time package to support localized text + relative time formatting ([fab96a0](https://github.com/Quenty/NevermoreEngine/commit/fab96a01f941baf5e051f85ba7e0dc82bc04d1c5))
|
|
16
|
+
|
|
6
17
|
# [2.5.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.4.6...@quenty/time@2.5.0) (2026-07-14)
|
|
7
18
|
|
|
8
19
|
### Features
|
package/README.md
CHANGED
|
@@ -11,78 +11,11 @@
|
|
|
11
11
|
</a>
|
|
12
12
|
</div>
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
Time based parsing and formatting
|
|
15
15
|
|
|
16
16
|
<div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/Time">View docs →</a></div>
|
|
17
17
|
|
|
18
18
|
## Installation
|
|
19
19
|
```
|
|
20
20
|
npm install @quenty/time --save
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
## Usage
|
|
24
|
-
Usage is designed to be simple.
|
|
25
|
-
|
|
26
|
-
### `Time.getDaysMonthTable(year)`
|
|
27
|
-
Returns a Days in months table for the given year
|
|
28
|
-
|
|
29
|
-
### `Time.getSecond(currentTime)`
|
|
30
|
-
|
|
31
|
-
### `Time.getMinute(currentTime)`
|
|
32
|
-
|
|
33
|
-
### `Time.getHour(currentTime)`
|
|
34
|
-
|
|
35
|
-
### `Time.getDay(currentTime)`
|
|
36
|
-
|
|
37
|
-
### `Time.getYear(currentTime)`
|
|
38
|
-
|
|
39
|
-
### `Time.getYearShort(currentTime)`
|
|
40
|
-
|
|
41
|
-
### `Time.getYearShortFormatted(currentTime)`
|
|
42
|
-
|
|
43
|
-
### `Time.getMonth(currentTime)`
|
|
44
|
-
|
|
45
|
-
### `Time.getFormattedMonth(currentTime)`
|
|
46
|
-
|
|
47
|
-
### `Time.getDayOfTheMonth(currentTime)`
|
|
48
|
-
|
|
49
|
-
### `Time.getFormattedDayOfTheMonth(currentTime)`
|
|
50
|
-
|
|
51
|
-
### `Time.getMonthName(currentTime)`
|
|
52
|
-
|
|
53
|
-
### `Time.getMonthNameShort(currentTime)`
|
|
54
|
-
|
|
55
|
-
### `Time.getJulianDate(currentTime)`
|
|
56
|
-
|
|
57
|
-
### `Time.getDayOfTheWeek(currentTime)`
|
|
58
|
-
|
|
59
|
-
### `Time.getDayOfTheWeekName(currentTime)`
|
|
60
|
-
|
|
61
|
-
### `Time.getDayOfTheWeekNameShort(currentTime)`
|
|
62
|
-
|
|
63
|
-
### `Time.getOrdinalOfNumber(number)`
|
|
64
|
-
|
|
65
|
-
### `Time.getDayOfTheMonthOrdinal(currentTime)`
|
|
66
|
-
|
|
67
|
-
### `Time.getFormattedSecond(currentTime)`
|
|
68
|
-
|
|
69
|
-
### `Time.getFormattedMinute(currentTime)`
|
|
70
|
-
|
|
71
|
-
### `Time.getRegularHour(currentTime)`
|
|
72
|
-
|
|
73
|
-
### `Time.getHourFormatted(currentTime)`
|
|
74
|
-
|
|
75
|
-
### `Time.getRegularHourFormatted(currentTime)`
|
|
76
|
-
|
|
77
|
-
### `Time.getamOrpm(currentTime)`
|
|
78
|
-
|
|
79
|
-
### `Time.getAMorPM(currentTime)`
|
|
80
|
-
|
|
81
|
-
### `Time.getMilitaryHour(currentTime)`
|
|
82
|
-
|
|
83
|
-
### `Time.isLeapYear(currentTime)`
|
|
84
|
-
|
|
85
|
-
### `Time.getDaysInMonth(currentTime)`
|
|
86
|
-
|
|
87
|
-
### `Time.getFormattedTime(format, currentTime)`
|
|
88
|
-
|
|
21
|
+
```
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/time",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.6.0",
|
|
4
|
+
"description": "Time based parsing and formatting built on Roblox's DateTime, with PHP-style format codes. All calculations are in UTC.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
7
7
|
"Nevermore",
|
|
@@ -27,8 +27,17 @@
|
|
|
27
27
|
"contributors": [
|
|
28
28
|
"Quenty"
|
|
29
29
|
],
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@quenty/clienttranslator": "14.53.0",
|
|
32
|
+
"@quenty/loader": "10.11.1",
|
|
33
|
+
"@quenty/nevermore-test-runner": "1.6.0",
|
|
34
|
+
"@quentystudios/jest-lua": "3.10.0-quenty.2"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@quenty/loader": "workspace:*"
|
|
38
|
+
},
|
|
30
39
|
"publishConfig": {
|
|
31
40
|
"access": "public"
|
|
32
41
|
},
|
|
33
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "31423d2314075bdd1f14ebbbc3eb637c5e0cb6ce"
|
|
34
43
|
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[=[
|
|
3
|
+
Describes the distance between two times in words: `in 3 hours`, `a day ago`, `2 months`.
|
|
4
|
+
|
|
5
|
+
@class RelativeTimeUtils
|
|
6
|
+
]=]
|
|
7
|
+
|
|
8
|
+
local require = require(script.Parent.loader).load(script)
|
|
9
|
+
|
|
10
|
+
local Time = require("Time")
|
|
11
|
+
local TimeLocalizationUtils = require("TimeLocalizationUtils")
|
|
12
|
+
|
|
13
|
+
local RelativeTimeUtils = {}
|
|
14
|
+
|
|
15
|
+
-- Fractional months in (dateTime - other), calendar aware. Anchors on whole months, then adds the fraction of the month the remainder covers.
|
|
16
|
+
function RelativeTimeUtils._monthDiff(dateTime: DateTime, other: DateTime): number
|
|
17
|
+
local a = Time._toUniversalTime(dateTime)
|
|
18
|
+
local b = Time._toUniversalTime(other)
|
|
19
|
+
if a.Day < b.Day then
|
|
20
|
+
return -RelativeTimeUtils._monthDiff(other, dateTime)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
local wholeMonthDiff = (b.Year - a.Year) * 12 + (b.Month - a.Month)
|
|
24
|
+
local anchor = Time.add(dateTime, wholeMonthDiff, "month")
|
|
25
|
+
local remainder = other.UnixTimestampMillis - anchor.UnixTimestampMillis
|
|
26
|
+
local isBeforeAnchor = remainder < 0
|
|
27
|
+
local anchor2 = Time.add(dateTime, wholeMonthDiff + (if isBeforeAnchor then -1 else 1), "month")
|
|
28
|
+
local monthLength = math.abs(anchor2.UnixTimestampMillis - anchor.UnixTimestampMillis)
|
|
29
|
+
local fraction = if monthLength == 0 then 0 else remainder / monthLength
|
|
30
|
+
|
|
31
|
+
return -(wholeMonthDiff + fraction)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
-- (dateTime - other) in the given unit, fractional
|
|
35
|
+
function RelativeTimeUtils._diff(dateTime: DateTime, other: DateTime, unit: Time.TimeUnit): number
|
|
36
|
+
local normalized = Time._normalizeUnit(unit)
|
|
37
|
+
local millis = dateTime.UnixTimestampMillis - other.UnixTimestampMillis
|
|
38
|
+
|
|
39
|
+
if normalized == "second" then
|
|
40
|
+
return millis / 1000
|
|
41
|
+
elseif normalized == "minute" then
|
|
42
|
+
return millis / 60000
|
|
43
|
+
elseif normalized == "hour" then
|
|
44
|
+
return millis / 3600000
|
|
45
|
+
elseif normalized == "day" then
|
|
46
|
+
return millis / 86400000
|
|
47
|
+
elseif normalized == "week" then
|
|
48
|
+
return millis / 604800000
|
|
49
|
+
elseif normalized == "month" then
|
|
50
|
+
return RelativeTimeUtils._monthDiff(dateTime, other)
|
|
51
|
+
elseif normalized == "year" then
|
|
52
|
+
return RelativeTimeUtils._monthDiff(dateTime, other) / 12
|
|
53
|
+
else
|
|
54
|
+
error(string.format("Bad relative time unit %q", normalized))
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
--[=[
|
|
59
|
+
One step of the relative time scale. `key` picks the string, `limit` is the
|
|
60
|
+
largest rounded amount this step accepts (omit for the last step), and `unit` is one of
|
|
61
|
+
[Time.TimeUnit] other than milliseconds or quarters. A step without a unit reuses the
|
|
62
|
+
previous step's diff, which is how `45..89 seconds` becomes `a minute`.
|
|
63
|
+
|
|
64
|
+
@interface RelativeTimeThreshold
|
|
65
|
+
.key string
|
|
66
|
+
.limit number?
|
|
67
|
+
.unit TimeUnit?
|
|
68
|
+
@within RelativeTimeUtils
|
|
69
|
+
]=]
|
|
70
|
+
export type RelativeTimeThreshold = { key: string, limit: number?, unit: Time.TimeUnit? }
|
|
71
|
+
|
|
72
|
+
--[=[
|
|
73
|
+
See [TimeLocalizationUtils.RelativeTimeStringOverrides].
|
|
74
|
+
|
|
75
|
+
@type RelativeTimeStringOverrides TimeLocalizationUtils.RelativeTimeStringOverrides
|
|
76
|
+
@within RelativeTimeUtils
|
|
77
|
+
]=]
|
|
78
|
+
export type RelativeTimeStringOverrides = TimeLocalizationUtils.RelativeTimeStringOverrides
|
|
79
|
+
|
|
80
|
+
--[=[
|
|
81
|
+
Options for [RelativeTimeUtils.from], [RelativeTimeUtils.to], [RelativeTimeUtils.fromNow] and [RelativeTimeUtils.toNow].
|
|
82
|
+
|
|
83
|
+
@interface RelativeTimeOptions
|
|
84
|
+
.withoutSuffix boolean? -- Drop the `in` / `ago` wrapper
|
|
85
|
+
.thresholds { RelativeTimeThreshold }? -- Replaces the default scale
|
|
86
|
+
.rounding ((number) -> number)? -- Applied to the absolute amount, defaults to round half up
|
|
87
|
+
.locale string? -- Picks the strings through [TimeLocalizationUtils], defaults to English
|
|
88
|
+
.strings RelativeTimeStringOverrides? -- Overrides or extends the locale's strings keyed by threshold key, plus `future` and `past`
|
|
89
|
+
@within RelativeTimeUtils
|
|
90
|
+
]=]
|
|
91
|
+
export type RelativeTimeOptions = {
|
|
92
|
+
withoutSuffix: boolean?,
|
|
93
|
+
locale: string?,
|
|
94
|
+
thresholds: { RelativeTimeThreshold }?,
|
|
95
|
+
rounding: ((number) -> number)?,
|
|
96
|
+
strings: RelativeTimeStringOverrides?,
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
-- Default scale: seconds, then minutes, hours, days, months and years
|
|
100
|
+
local RELATIVE_TIME_THRESHOLDS: { RelativeTimeThreshold } = {
|
|
101
|
+
{ key = "s", limit = 44, unit = "second" },
|
|
102
|
+
{ key = "m", limit = 89 },
|
|
103
|
+
{ key = "mm", limit = 44, unit = "minute" },
|
|
104
|
+
{ key = "h", limit = 89 },
|
|
105
|
+
{ key = "hh", limit = 21, unit = "hour" },
|
|
106
|
+
{ key = "d", limit = 35 },
|
|
107
|
+
{ key = "dd", limit = 25, unit = "day" },
|
|
108
|
+
{ key = "M", limit = 45 },
|
|
109
|
+
{ key = "MM", limit = 10, unit = "month" },
|
|
110
|
+
{ key = "y", limit = 17 },
|
|
111
|
+
{ key = "yy", unit = "year" },
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function RelativeTimeUtils._roundHalfUp(value: number): number
|
|
115
|
+
return math.floor(value + 0.5)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
function RelativeTimeUtils._getRelativeTimeString(
|
|
119
|
+
options: RelativeTimeOptions?,
|
|
120
|
+
key: string
|
|
121
|
+
): TimeLocalizationUtils.RelativeTimeString
|
|
122
|
+
local overrides = if options then options.strings else nil
|
|
123
|
+
local localeStrings: any =
|
|
124
|
+
TimeLocalizationUtils.getRelativeTimeStringsForLocale(if options then options.locale else nil)
|
|
125
|
+
local value = (if overrides then overrides[key] else nil) or localeStrings[key]
|
|
126
|
+
assert(value, string.format("No relative time string for %q", key))
|
|
127
|
+
return value
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
-- Describes (dateTime - other): positive is the future.
|
|
131
|
+
function RelativeTimeUtils._relativeTime(dateTime: DateTime, other: DateTime, options: RelativeTimeOptions?): string
|
|
132
|
+
local thresholds = (if options then options.thresholds else nil) or RELATIVE_TIME_THRESHOLDS
|
|
133
|
+
local rounding = (if options then options.rounding else nil) or RelativeTimeUtils._roundHalfUp
|
|
134
|
+
|
|
135
|
+
local withoutSuffix = if options then options.withoutSuffix == true else false
|
|
136
|
+
|
|
137
|
+
local result = 0
|
|
138
|
+
local isFuture = false
|
|
139
|
+
local out = ""
|
|
140
|
+
|
|
141
|
+
for index, threshold in thresholds do
|
|
142
|
+
if threshold.unit then
|
|
143
|
+
result = RelativeTimeUtils._diff(dateTime, other, threshold.unit)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
local amount = rounding(math.abs(result))
|
|
147
|
+
isFuture = result > 0
|
|
148
|
+
|
|
149
|
+
if threshold.limit == nil or amount <= threshold.limit then
|
|
150
|
+
-- "1 minutes" reads as "a minute", "0 seconds" as "a few seconds"
|
|
151
|
+
local resolved = if amount <= 1 and index > 1 then thresholds[index - 1] else threshold
|
|
152
|
+
local format = RelativeTimeUtils._getRelativeTimeString(options, resolved.key)
|
|
153
|
+
if type(format) == "function" then
|
|
154
|
+
out = format(amount, withoutSuffix, resolved.key, isFuture)
|
|
155
|
+
else
|
|
156
|
+
out = string.gsub(format, "%%d", tostring(amount))
|
|
157
|
+
end
|
|
158
|
+
break
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
if withoutSuffix then
|
|
163
|
+
return out
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
local template = RelativeTimeUtils._getRelativeTimeString(options, if isFuture then "future" else "past")
|
|
167
|
+
assert(type(template) == "string", "future and past must be strings")
|
|
168
|
+
local formatted = string.gsub(template, "%%s", function()
|
|
169
|
+
return out
|
|
170
|
+
end)
|
|
171
|
+
|
|
172
|
+
return formatted
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
--[=[
|
|
176
|
+
Describes how far `currentTime` is from `compareTo`: a later time reads
|
|
177
|
+
`in a day`, an earlier one `a day ago`. See [RelativeTimeOptions] to drop the suffix or
|
|
178
|
+
change the scale.
|
|
179
|
+
|
|
180
|
+
```lua
|
|
181
|
+
print(RelativeTimeUtils.from("2025-09-23T00:00:00Z", "2025-09-22T00:00:00Z")) --> in a day
|
|
182
|
+
print(RelativeTimeUtils.from("2025-09-22T00:00:00Z", "2025-09-23T00:00:00Z", { withoutSuffix = true })) --> a day
|
|
183
|
+
```
|
|
184
|
+
]=]
|
|
185
|
+
function RelativeTimeUtils.from(
|
|
186
|
+
currentTime: Time.DateTimeLike,
|
|
187
|
+
compareTo: Time.DateTimeLike,
|
|
188
|
+
options: RelativeTimeOptions?
|
|
189
|
+
): string
|
|
190
|
+
return RelativeTimeUtils._relativeTime(Time._toDateTime(currentTime), Time._toDateTime(compareTo), options)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
--[=[
|
|
194
|
+
Describes how far `compareTo` is from `currentTime`. The mirror of
|
|
195
|
+
[RelativeTimeUtils.from]: `RelativeTimeUtils.to(a, b)` equals `RelativeTimeUtils.from(b, a)`.
|
|
196
|
+
]=]
|
|
197
|
+
function RelativeTimeUtils.to(
|
|
198
|
+
currentTime: Time.DateTimeLike,
|
|
199
|
+
compareTo: Time.DateTimeLike,
|
|
200
|
+
options: RelativeTimeOptions?
|
|
201
|
+
): string
|
|
202
|
+
return RelativeTimeUtils._relativeTime(Time._toDateTime(compareTo), Time._toDateTime(currentTime), options)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
--[=[
|
|
206
|
+
Describes how far `currentTime` is from now: `3 hours ago`.
|
|
207
|
+
]=]
|
|
208
|
+
function RelativeTimeUtils.fromNow(currentTime: Time.DateTimeLike, options: RelativeTimeOptions?): string
|
|
209
|
+
return RelativeTimeUtils.from(currentTime, nil, options)
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
--[=[
|
|
213
|
+
Describes how far now is from `currentTime`: `in 3 hours` for a time
|
|
214
|
+
three hours in the past.
|
|
215
|
+
]=]
|
|
216
|
+
function RelativeTimeUtils.toNow(currentTime: Time.DateTimeLike, options: RelativeTimeOptions?): string
|
|
217
|
+
return RelativeTimeUtils.to(currentTime, nil, options)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
return RelativeTimeUtils
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[[
|
|
3
|
+
@class RelativeTimeUtils.spec.lua
|
|
4
|
+
]]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local Jest = require("Jest")
|
|
9
|
+
local RelativeTimeUtils = require("RelativeTimeUtils")
|
|
10
|
+
|
|
11
|
+
local describe = Jest.Globals.describe
|
|
12
|
+
local expect = Jest.Globals.expect
|
|
13
|
+
local it = Jest.Globals.it
|
|
14
|
+
|
|
15
|
+
local BASE = 1758499200 -- 2025-09-22 Monday 00:00:00 UTC
|
|
16
|
+
local WITHOUT_SUFFIX = { withoutSuffix = true }
|
|
17
|
+
|
|
18
|
+
describe("RelativeTimeUtils.from", function()
|
|
19
|
+
local function fromOffset(seconds: number): string
|
|
20
|
+
return RelativeTimeUtils.from(BASE + seconds, BASE, WITHOUT_SUFFIX)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it("should pick the threshold for seconds, minutes and hours", function()
|
|
24
|
+
expect(fromOffset(0)).toBe("a few seconds")
|
|
25
|
+
expect(fromOffset(44)).toBe("a few seconds")
|
|
26
|
+
expect(fromOffset(45)).toBe("a minute")
|
|
27
|
+
expect(fromOffset(89)).toBe("a minute")
|
|
28
|
+
expect(fromOffset(90)).toBe("2 minutes")
|
|
29
|
+
expect(fromOffset(44 * 60)).toBe("44 minutes")
|
|
30
|
+
expect(fromOffset(45 * 60)).toBe("an hour")
|
|
31
|
+
expect(fromOffset(89 * 60)).toBe("an hour")
|
|
32
|
+
expect(fromOffset(90 * 60)).toBe("2 hours")
|
|
33
|
+
expect(fromOffset(21 * 3600)).toBe("21 hours")
|
|
34
|
+
end)
|
|
35
|
+
|
|
36
|
+
it("should pick the threshold for days and months", function()
|
|
37
|
+
expect(fromOffset(22 * 3600)).toBe("a day")
|
|
38
|
+
expect(fromOffset(35 * 3600)).toBe("a day")
|
|
39
|
+
expect(fromOffset(36 * 3600)).toBe("2 days")
|
|
40
|
+
expect(fromOffset(25 * 86400)).toBe("25 days")
|
|
41
|
+
expect(fromOffset(26 * 86400)).toBe("a month")
|
|
42
|
+
expect(fromOffset(45 * 86400)).toBe("a month")
|
|
43
|
+
expect(fromOffset(46 * 86400)).toBe("2 months")
|
|
44
|
+
end)
|
|
45
|
+
|
|
46
|
+
it("should count months and years on the calendar", function()
|
|
47
|
+
expect(RelativeTimeUtils.from("2026-07-22T00:00:00Z", BASE, WITHOUT_SUFFIX)).toBe("10 months")
|
|
48
|
+
expect(RelativeTimeUtils.from("2026-08-22T00:00:00Z", BASE, WITHOUT_SUFFIX)).toBe("a year")
|
|
49
|
+
expect(RelativeTimeUtils.from("2027-02-22T00:00:00Z", BASE, WITHOUT_SUFFIX)).toBe("a year")
|
|
50
|
+
expect(RelativeTimeUtils.from("2027-03-22T00:00:00Z", BASE, WITHOUT_SUFFIX)).toBe("2 years")
|
|
51
|
+
expect(RelativeTimeUtils.from("2035-09-22T00:00:00Z", BASE, WITHOUT_SUFFIX)).toBe("10 years")
|
|
52
|
+
expect(RelativeTimeUtils.from("2025-02-28T00:00:00Z", "2025-01-31T00:00:00Z", WITHOUT_SUFFIX)).toBe("a month")
|
|
53
|
+
end)
|
|
54
|
+
|
|
55
|
+
it("should say in for the future and ago for the past", function()
|
|
56
|
+
expect(RelativeTimeUtils.from(BASE + 86400, BASE)).toBe("in a day")
|
|
57
|
+
expect(RelativeTimeUtils.from(BASE - 86400, BASE)).toBe("a day ago")
|
|
58
|
+
expect(RelativeTimeUtils.from(BASE, BASE)).toBe("a few seconds ago")
|
|
59
|
+
expect(RelativeTimeUtils.from(BASE - 3 * 3600, BASE)).toBe("3 hours ago")
|
|
60
|
+
end)
|
|
61
|
+
|
|
62
|
+
it("should accept any DateTimeLike on both sides", function()
|
|
63
|
+
expect(RelativeTimeUtils.from(DateTime.fromUnixTimestamp(BASE + 3600), "2025-09-22T00:00:00Z")).toBe(
|
|
64
|
+
"in an hour"
|
|
65
|
+
)
|
|
66
|
+
end)
|
|
67
|
+
end)
|
|
68
|
+
|
|
69
|
+
describe("RelativeTimeUtils.to", function()
|
|
70
|
+
it("should mirror from", function()
|
|
71
|
+
expect(RelativeTimeUtils.to(BASE, BASE + 86400)).toBe("in a day")
|
|
72
|
+
expect(RelativeTimeUtils.to(BASE + 86400, BASE)).toBe("a day ago")
|
|
73
|
+
expect(RelativeTimeUtils.to(BASE, BASE + 90, WITHOUT_SUFFIX)).toBe("2 minutes")
|
|
74
|
+
end)
|
|
75
|
+
end)
|
|
76
|
+
|
|
77
|
+
describe("RelativeTimeUtils.fromNow / toNow", function()
|
|
78
|
+
it("should compare against the current time", function()
|
|
79
|
+
local hourAgo = DateTime.now().UnixTimestamp - 3600
|
|
80
|
+
|
|
81
|
+
expect(RelativeTimeUtils.fromNow(hourAgo)).toBe("an hour ago")
|
|
82
|
+
expect(RelativeTimeUtils.fromNow(hourAgo, WITHOUT_SUFFIX)).toBe("an hour")
|
|
83
|
+
expect(RelativeTimeUtils.toNow(hourAgo)).toBe("in an hour")
|
|
84
|
+
expect(RelativeTimeUtils.fromNow(nil)).toBe("a few seconds ago")
|
|
85
|
+
end)
|
|
86
|
+
end)
|
|
87
|
+
|
|
88
|
+
describe("RelativeTimeUtils.from with options", function()
|
|
89
|
+
it("should use custom thresholds and strings", function()
|
|
90
|
+
local thresholds: { RelativeTimeUtils.RelativeTimeThreshold } = {
|
|
91
|
+
{ key = "s", limit = 59, unit = "second" },
|
|
92
|
+
{ key = "mm", limit = 59, unit = "minute" },
|
|
93
|
+
{ key = "hh", limit = 23, unit = "hour" },
|
|
94
|
+
{ key = "ww", limit = 3, unit = "week" },
|
|
95
|
+
{ key = "dd", unit = "day" },
|
|
96
|
+
}
|
|
97
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = {
|
|
98
|
+
s = "%d seconds",
|
|
99
|
+
ww = "%d weeks",
|
|
100
|
+
}
|
|
101
|
+
local options: RelativeTimeUtils.RelativeTimeOptions = {
|
|
102
|
+
withoutSuffix = true,
|
|
103
|
+
thresholds = thresholds,
|
|
104
|
+
strings = strings,
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
expect(RelativeTimeUtils.from(BASE + 30, BASE, options)).toBe("30 seconds")
|
|
108
|
+
expect(RelativeTimeUtils.from(BASE + 90, BASE, options)).toBe("2 minutes")
|
|
109
|
+
expect(RelativeTimeUtils.from(BASE + 5 * 3600, BASE, options)).toBe("5 hours")
|
|
110
|
+
expect(RelativeTimeUtils.from(BASE + 14 * 86400, BASE, options)).toBe("2 weeks")
|
|
111
|
+
expect(RelativeTimeUtils.from(BASE + 40 * 86400, BASE, options)).toBe("40 days")
|
|
112
|
+
end)
|
|
113
|
+
|
|
114
|
+
it("should override the suffix strings", function()
|
|
115
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = { future = "%s from now", past = "%s earlier" }
|
|
116
|
+
local options = { strings = strings }
|
|
117
|
+
|
|
118
|
+
expect(RelativeTimeUtils.from(BASE + 3600, BASE, options)).toBe("an hour from now")
|
|
119
|
+
expect(RelativeTimeUtils.from(BASE - 3600, BASE, options)).toBe("an hour earlier")
|
|
120
|
+
end)
|
|
121
|
+
|
|
122
|
+
it("should use custom rounding", function()
|
|
123
|
+
expect(RelativeTimeUtils.from(BASE + 90, BASE, { withoutSuffix = true, rounding = math.floor })).toBe(
|
|
124
|
+
"a minute"
|
|
125
|
+
)
|
|
126
|
+
expect(RelativeTimeUtils.from(BASE + 90, BASE, { withoutSuffix = true, rounding = math.ceil })).toBe(
|
|
127
|
+
"2 minutes"
|
|
128
|
+
)
|
|
129
|
+
end)
|
|
130
|
+
|
|
131
|
+
it("should error on a threshold key without a string", function()
|
|
132
|
+
local thresholds: { RelativeTimeUtils.RelativeTimeThreshold } = { { key = "nope", unit = "second" } }
|
|
133
|
+
|
|
134
|
+
expect(function()
|
|
135
|
+
RelativeTimeUtils.from(BASE + 30, BASE, { thresholds = thresholds })
|
|
136
|
+
end).toThrow("No relative time string")
|
|
137
|
+
end)
|
|
138
|
+
end)
|
|
139
|
+
|
|
140
|
+
describe("RelativeTimeUtils.from with a locale", function()
|
|
141
|
+
local function from(seconds: number, locale: string, withoutSuffix: boolean?): string
|
|
142
|
+
return RelativeTimeUtils.from(BASE + seconds, BASE, { locale = locale, withoutSuffix = withoutSuffix })
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
it("should use the locale's strings and suffixes", function()
|
|
146
|
+
expect(from(-60, "fr-fr")).toBe("il y a une minute")
|
|
147
|
+
expect(from(3 * 3600, "es-es")).toBe("en 3 horas")
|
|
148
|
+
expect(from(-2 * 86400, "pt-br")).toBe("há 2 dias")
|
|
149
|
+
expect(from(60, "ja-jp")).toBe("1分後")
|
|
150
|
+
expect(from(3 * 86400, "zh-cn")).toBe("3 天内")
|
|
151
|
+
expect(from(-3 * 86400, "zh-tw")).toBe("3 天前")
|
|
152
|
+
expect(from(3600, "ko-kr")).toBe("한 시간 후")
|
|
153
|
+
expect(from(5 * 60, "tr-tr")).toBe("5 dakika sonra")
|
|
154
|
+
end)
|
|
155
|
+
|
|
156
|
+
it("should inflect German after in and vor", function()
|
|
157
|
+
expect(from(60, "de-de")).toBe("in einer Minute")
|
|
158
|
+
expect(from(-60, "de-de")).toBe("vor einer Minute")
|
|
159
|
+
expect(from(60, "de-de", true)).toBe("eine Minute")
|
|
160
|
+
expect(from(-3 * 86400, "de-de")).toBe("vor 3 Tagen")
|
|
161
|
+
expect(from(3 * 86400, "de-de", true)).toBe("3 Tage")
|
|
162
|
+
end)
|
|
163
|
+
|
|
164
|
+
it("should pick Russian and Polish plural forms", function()
|
|
165
|
+
expect(from(2 * 60, "ru-ru")).toBe("через 2 минуты")
|
|
166
|
+
expect(from(5 * 60, "ru-ru")).toBe("через 5 минут")
|
|
167
|
+
expect(from(21 * 60, "ru-ru")).toBe("через 21 минуту")
|
|
168
|
+
expect(from(21 * 60, "ru-ru", true)).toBe("21 минута")
|
|
169
|
+
expect(from(-60, "ru-ru")).toBe("минуту назад")
|
|
170
|
+
expect(from(2 * 3600, "pl-pl")).toBe("za 2 godziny")
|
|
171
|
+
expect(from(5 * 3600, "pl-pl")).toBe("za 5 godzin")
|
|
172
|
+
expect(from(60, "pl-pl")).toBe("za minutę")
|
|
173
|
+
expect(from(60, "pl-pl", true)).toBe("minuta")
|
|
174
|
+
end)
|
|
175
|
+
|
|
176
|
+
it("should resolve regional variants and let overrides win", function()
|
|
177
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = { h = "une petite heure" }
|
|
178
|
+
|
|
179
|
+
expect(from(-60, "fr-ca")).toBe("il y a une minute")
|
|
180
|
+
expect(RelativeTimeUtils.from(BASE + 3600, BASE, { locale = "fr-fr", strings = strings })).toBe(
|
|
181
|
+
"dans une petite heure"
|
|
182
|
+
)
|
|
183
|
+
end)
|
|
184
|
+
end)
|
|
185
|
+
|
|
186
|
+
describe("RelativeTimeUtils localization edge cases", function()
|
|
187
|
+
local function from(seconds: number, locale: string, withoutSuffix: boolean?): string
|
|
188
|
+
return RelativeTimeUtils.from(BASE + seconds, BASE, { locale = locale, withoutSuffix = withoutSuffix })
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
it("should call function valued strings with the amount, suffix flag, key and direction", function()
|
|
192
|
+
local calls: { { any } } = {}
|
|
193
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = {
|
|
194
|
+
mm = function(amount: number, withoutSuffix: boolean, key: string, isFuture: boolean): string
|
|
195
|
+
table.insert(calls, { amount :: any, withoutSuffix, key, isFuture })
|
|
196
|
+
return "custom"
|
|
197
|
+
end,
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
expect(RelativeTimeUtils.from(BASE + 5 * 60, BASE, { strings = strings })).toBe("in custom")
|
|
201
|
+
expect(RelativeTimeUtils.from(BASE - 5 * 60, BASE, { strings = strings, withoutSuffix = true })).toBe("custom")
|
|
202
|
+
expect(calls).toEqual({
|
|
203
|
+
{ 5 :: any, false, "mm", true },
|
|
204
|
+
{ 5 :: any, true, "mm", false },
|
|
205
|
+
})
|
|
206
|
+
end)
|
|
207
|
+
|
|
208
|
+
it("should let a string override replace a locale's function", function()
|
|
209
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = { m = "1 Min." }
|
|
210
|
+
|
|
211
|
+
expect(RelativeTimeUtils.from(BASE + 60, BASE, { locale = "de-de", strings = strings })).toBe("in 1 Min.")
|
|
212
|
+
expect(RelativeTimeUtils.from(BASE + 3600, BASE, { locale = "de-de", strings = strings })).toBe(
|
|
213
|
+
"in einer Stunde"
|
|
214
|
+
)
|
|
215
|
+
end)
|
|
216
|
+
|
|
217
|
+
it("should reject function valued future and past", function()
|
|
218
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = {
|
|
219
|
+
future = function(): string
|
|
220
|
+
return "soon"
|
|
221
|
+
end,
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
expect(function()
|
|
225
|
+
RelativeTimeUtils.from(BASE + 60, BASE, { strings = strings })
|
|
226
|
+
end).toThrow("future and past must be strings")
|
|
227
|
+
end)
|
|
228
|
+
|
|
229
|
+
it("should use the singular step for a rounded amount of 1 in every locale", function()
|
|
230
|
+
expect(from(60, "de-de")).toBe("in einer Minute")
|
|
231
|
+
expect(from(60 * 60, "ru-ru")).toBe("через час")
|
|
232
|
+
expect(from(-24 * 3600, "pl-pl")).toBe("1 dzień temu")
|
|
233
|
+
expect(from(24 * 3600, "ja-jp")).toBe("1日後")
|
|
234
|
+
end)
|
|
235
|
+
|
|
236
|
+
it("should treat 11 to 14 as many in Russian even past 100", function()
|
|
237
|
+
expect(from(11 * 60, "ru-ru")).toBe("через 11 минут")
|
|
238
|
+
expect(from(12 * 3600, "ru-ru")).toBe("через 12 часов")
|
|
239
|
+
expect(from(14 * 3600, "ru-ru")).toBe("через 14 часов")
|
|
240
|
+
expect(RelativeTimeUtils.from("2136-09-22T00:00:00Z", BASE, { locale = "ru-ru", withoutSuffix = true })).toBe(
|
|
241
|
+
"111 лет"
|
|
242
|
+
)
|
|
243
|
+
expect(RelativeTimeUtils.from("2126-09-22T00:00:00Z", BASE, { locale = "ru-ru" })).toBe("через 101 год")
|
|
244
|
+
end)
|
|
245
|
+
|
|
246
|
+
it("should keep Polish teens and 22 apart", function()
|
|
247
|
+
expect(from(12 * 3600, "pl-pl")).toBe("za 12 godzin")
|
|
248
|
+
expect(from(22 * 60, "pl-pl")).toBe("za 22 minuty")
|
|
249
|
+
expect(RelativeTimeUtils.from("2137-09-22T00:00:00Z", BASE, { locale = "pl-pl", withoutSuffix = true })).toBe(
|
|
250
|
+
"112 lat"
|
|
251
|
+
)
|
|
252
|
+
expect(RelativeTimeUtils.from("2047-09-22T00:00:00Z", BASE, { locale = "pl-pl" })).toBe("za 22 lata")
|
|
253
|
+
expect(RelativeTimeUtils.from("2037-09-22T00:00:00Z", BASE, { locale = "pl-pl" })).toBe("za 12 lat")
|
|
254
|
+
end)
|
|
255
|
+
|
|
256
|
+
it("should inflect German months and years after vor", function()
|
|
257
|
+
expect(RelativeTimeUtils.from("2025-07-01T00:00:00Z", BASE, { locale = "de-de" })).toBe("vor 3 Monaten")
|
|
258
|
+
expect(RelativeTimeUtils.from("2025-07-01T00:00:00Z", BASE, { locale = "de-de", withoutSuffix = true })).toBe(
|
|
259
|
+
"3 Monate"
|
|
260
|
+
)
|
|
261
|
+
expect(RelativeTimeUtils.from("2020-09-22T00:00:00Z", BASE, { locale = "de-de" })).toBe("vor 5 Jahren")
|
|
262
|
+
expect(RelativeTimeUtils.from("2024-09-22T00:00:00Z", BASE, { locale = "de-de" })).toBe("vor einem Jahr")
|
|
263
|
+
end)
|
|
264
|
+
|
|
265
|
+
it("should place the suffix where the language puts it", function()
|
|
266
|
+
expect(from(3600, "it-it")).toBe("tra un'ora")
|
|
267
|
+
expect(from(-3600, "it-it")).toBe("un'ora fa")
|
|
268
|
+
expect(from(-3600, "id-id")).toBe("sejam yang lalu")
|
|
269
|
+
expect(from(2 * 86400, "vi-vn")).toBe("2 ngày tới")
|
|
270
|
+
expect(from(-2 * 86400, "th-th")).toBe("2 วันที่แล้ว")
|
|
271
|
+
expect(from(2 * 86400, "ar")).toBe("بعد 2 أيام")
|
|
272
|
+
end)
|
|
273
|
+
|
|
274
|
+
it("should combine a locale with custom thresholds", function()
|
|
275
|
+
local thresholds: { RelativeTimeUtils.RelativeTimeThreshold } = {
|
|
276
|
+
{ key = "s", limit = 59, unit = "second" },
|
|
277
|
+
{ key = "mm", limit = 59, unit = "minute" },
|
|
278
|
+
{ key = "hh", limit = 23, unit = "hour" },
|
|
279
|
+
{ key = "ww", unit = "week" },
|
|
280
|
+
}
|
|
281
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = { ww = "%d semaines" }
|
|
282
|
+
|
|
283
|
+
expect(
|
|
284
|
+
RelativeTimeUtils.from(
|
|
285
|
+
BASE + 14 * 86400,
|
|
286
|
+
BASE,
|
|
287
|
+
{ locale = "fr-fr", thresholds = thresholds, strings = strings }
|
|
288
|
+
)
|
|
289
|
+
).toBe("dans 2 semaines")
|
|
290
|
+
expect(
|
|
291
|
+
RelativeTimeUtils.from(
|
|
292
|
+
BASE + 5 * 3600,
|
|
293
|
+
BASE,
|
|
294
|
+
{ locale = "fr-fr", thresholds = thresholds, strings = strings }
|
|
295
|
+
)
|
|
296
|
+
).toBe("dans 5 heures")
|
|
297
|
+
expect(function()
|
|
298
|
+
RelativeTimeUtils.from(BASE + 14 * 86400, BASE, { locale = "fr-fr", thresholds = thresholds })
|
|
299
|
+
end).toThrow("No relative time string")
|
|
300
|
+
end)
|
|
301
|
+
|
|
302
|
+
it("should resolve the locale loosely", function()
|
|
303
|
+
expect(from(-60, "FR-FR")).toBe("il y a une minute")
|
|
304
|
+
expect(from(-60, "fr_CA")).toBe("il y a une minute")
|
|
305
|
+
expect(from(-60, "zh-Hant")).toBe("1 分鐘前")
|
|
306
|
+
expect(from(-60, "xx-yy")).toBe("a minute ago")
|
|
307
|
+
end)
|
|
308
|
+
end)
|