@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.
- package/CHANGELOG.md +23 -67
- 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 +665 -353
- 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,139 +3,95 @@
|
|
|
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
|
-
|
|
7
|
-
|
|
6
|
+
# [2.6.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.5.0...@quenty/time@2.6.0) (2026-09-23)
|
|
8
7
|
|
|
9
8
|
### Bug Fixes
|
|
10
9
|
|
|
11
|
-
|
|
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
12
|
|
|
13
|
+
### Features
|
|
13
14
|
|
|
15
|
+
- Refactor time package to support localized text + relative time formatting ([fab96a0](https://github.com/Quenty/NevermoreEngine/commit/fab96a01f941baf5e051f85ba7e0dc82bc04d1c5))
|
|
14
16
|
|
|
17
|
+
# [2.5.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.4.6...@quenty/time@2.5.0) (2026-07-14)
|
|
15
18
|
|
|
19
|
+
### Features
|
|
16
20
|
|
|
17
|
-
|
|
21
|
+
- **time:** convert package to --!strict ([f5e3586](https://github.com/Quenty/NevermoreEngine/commit/f5e3586e6a1ca22139e1717d1b32741552d9126b))
|
|
18
22
|
|
|
19
|
-
|
|
23
|
+
## [2.4.6](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.4.5...@quenty/time@2.4.6) (2026-01-12)
|
|
20
24
|
|
|
25
|
+
### Bug Fixes
|
|
21
26
|
|
|
27
|
+
- Fix dependency installation process for pnpm ([9651b09](https://github.com/Quenty/NevermoreEngine/commit/9651b09af34f252ef3f9c7c539793cc878dd8cba))
|
|
22
28
|
|
|
29
|
+
## [2.4.5](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.4.4...@quenty/time@2.4.5) (2026-01-10)
|
|
23
30
|
|
|
31
|
+
**Note:** Version bump only for package @quenty/time
|
|
24
32
|
|
|
25
33
|
## [2.4.4](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.4.3...@quenty/time@2.4.4) (2026-01-03)
|
|
26
34
|
|
|
27
35
|
**Note:** Version bump only for package @quenty/time
|
|
28
36
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
37
|
## [2.4.3](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.4.2...@quenty/time@2.4.3) (2025-05-10)
|
|
34
38
|
|
|
35
39
|
**Note:** Version bump only for package @quenty/time
|
|
36
40
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
41
|
## [2.4.2](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.4.0...@quenty/time@2.4.2) (2025-04-07)
|
|
42
42
|
|
|
43
|
-
|
|
44
43
|
### Bug Fixes
|
|
45
44
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
45
|
+
- Add types to packages ([2374fb2](https://github.com/Quenty/NevermoreEngine/commit/2374fb2b043cfbe0e9b507b3316eec46a4e353a0))
|
|
46
|
+
- Bump package versions for republishing ([ba47c62](https://github.com/Quenty/NevermoreEngine/commit/ba47c62e32170bf74377b0c658c60b84306dc294))
|
|
52
47
|
|
|
53
48
|
## [2.4.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.4.0...@quenty/time@2.4.1) (2025-04-07)
|
|
54
49
|
|
|
55
|
-
|
|
56
50
|
### Bug Fixes
|
|
57
51
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
52
|
+
- Add types to packages ([2374fb2](https://github.com/Quenty/NevermoreEngine/commit/2374fb2b043cfbe0e9b507b3316eec46a4e353a0))
|
|
63
53
|
|
|
64
54
|
# [2.4.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.3.0...@quenty/time@2.4.0) (2024-09-25)
|
|
65
55
|
|
|
66
|
-
|
|
67
56
|
### Bug Fixes
|
|
68
57
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
58
|
+
- Rename `:gsub()` to `string.gsub` ([9a730cb](https://github.com/Quenty/NevermoreEngine/commit/9a730cbd7001706787c5e5db0efe2306b2234b3a))
|
|
59
|
+
- Rename `:rep()` to `string.repeat` ([476b7c9](https://github.com/Quenty/NevermoreEngine/commit/476b7c94be8e1a68e8e281ccb9c8445c2b549812))
|
|
75
60
|
|
|
76
61
|
# [2.3.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.2.0...@quenty/time@2.3.0) (2024-05-09)
|
|
77
62
|
|
|
78
|
-
|
|
79
63
|
### Bug Fixes
|
|
80
64
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
65
|
+
- Fix .package-lock.json replicating in packages ([75d0efe](https://github.com/Quenty/NevermoreEngine/commit/75d0efeef239f221d93352af71a5b3e930ec23c5))
|
|
86
66
|
|
|
87
67
|
# [2.2.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.1.0...@quenty/time@2.2.0) (2023-08-23)
|
|
88
68
|
|
|
89
69
|
**Note:** Version bump only for package @quenty/time
|
|
90
70
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
71
|
# [2.1.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.0.1...@quenty/time@2.1.0) (2022-03-27)
|
|
96
72
|
|
|
97
73
|
**Note:** Version bump only for package @quenty/time
|
|
98
74
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
75
|
## [2.0.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@2.0.0...@quenty/time@2.0.1) (2021-12-30)
|
|
104
76
|
|
|
105
77
|
**Note:** Version bump only for package @quenty/time
|
|
106
78
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
79
|
# [2.0.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/time@1.2.0...@quenty/time@2.0.0) (2021-09-05)
|
|
112
80
|
|
|
113
|
-
|
|
114
81
|
### Bug Fixes
|
|
115
82
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
83
|
+
- Remove peer dependencies. This is because lerna doesn't really support peer dependencies being linked and getting a new version on build, which is unfortunate. ([5f5aeee](https://github.com/Quenty/NevermoreEngine/commit/5f5aeeea8de9975435309e53679f0ef7064f9dd0))
|
|
121
84
|
|
|
122
85
|
# 1.2.0 (2021-07-31)
|
|
123
86
|
|
|
124
|
-
|
|
125
87
|
### Bug Fixes
|
|
126
88
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
89
|
+
- Add CI and CD ([47513e9](https://github.com/Quenty/NevermoreEngine/commit/47513e9b568162707534af132396dd8756947dd3))
|
|
90
|
+
- Adjust CI badge to show automatic build and release state ([5a55d3f](https://github.com/Quenty/NevermoreEngine/commit/5a55d3f19bf8d66a760d67da9b56ed47fab74656))
|
|
91
|
+
- Fix selene linting ([45fc074](https://github.com/Quenty/NevermoreEngine/commit/45fc07489ee59127ac6582689f19a0e87c1e5b5a))
|
|
132
92
|
|
|
133
93
|
## 1.0.2 (2021-07-25)
|
|
134
94
|
|
|
135
|
-
|
|
136
|
-
|
|
137
95
|
## 1.0.1 (2021-07-25)
|
|
138
96
|
|
|
139
|
-
|
|
140
|
-
|
|
141
97
|
# 1.0.0 (2021-07-24)
|
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
|