-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathduration.d.lua
More file actions
325 lines (300 loc) · 9.65 KB
/
Copy pathduration.d.lua
File metadata and controls
325 lines (300 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
---@meta _
---Representation of duration parts.
---@class mods.DurationParts
---@field milliseconds? number The millisecond component (1000 ms = 1 second).
---@field seconds? number The second component (60 seconds = 1 minute).
---@field minutes? number The minute component (60 minutes = 1 hour).
---@field hours? number The hour component (24 hours = 1 day).
---@field days? number The day component (7 days = 1 week).
---@field weeks? number The week component.
---@field months? number The month component (12 months = 1 year).
---@field quarters? number The quarter component (3 months = 1 quarter).
---@field years? number The year component.
---Rounding mode to use when humanizing durations.
---@alias mods.durationHumanizeRoundMode
---| boolean Whether to round (true) or not (false).
---| 'round' Round to the nearest integer.
---| 'floor' Round down (floor).
---| 'ceil' Round up (ceil).
---Supported units of time for duration representation and calculations.
---@alias mods.durationUnit
---| 'ms' Milliseconds
---| 'milliseconds' Milliseconds
---| 'millisecond' Milliseconds
---| 's' Seconds
---| 'secs' Seconds
---| 'sec' Seconds
---| 'seconds' Seconds
---| 'second' Seconds
---| 'm' Minutes
---| 'mins' Minutes
---| 'min' Minutes
---| 'minutes' Minutes
---| 'minute' Minutes
---| 'h' Hours
---| 'hours' Hours
---| 'hour' Hours
---| 'd' Days
---| 'days' Days
---| 'day' Days
---| 'w' Weeks
---| 'weeks' Weeks
---| 'week' Weeks
---| 'M' Months
---| 'months' Months
---| 'month' Months
---| 'q' Quarters
---| 'quarters' Quarters
---| 'quarter' Quarters
---| 'y' Years
---| 'years' Years
---| 'year' Years
---Configuration options for humanizing durations into relative-style strings.
---@class mods.DurationHumanizeOptions
---@field with_suffix? boolean Whether to include `ago` / `in` style wording.
---@field short? boolean Whether to use abbreviated unit labels like `2h`.
---@field round? mods.durationHumanizeRoundMode Rounding mode for custom unit output.
---@field max_unit? mods.durationUnit Largest unit allowed when choosing the displayed unit.
---@field min_unit? mods.durationUnit Smallest unit allowed when choosing the displayed unit.
---
---Represent, calculate, and humanize time spans.
---
---## Usage
---
---```lua
---local mods = require "mods"
---local Duration = mods.duration
---
---local shift = Duration({ day = 2, hour = 3 })
---print(shift:format("D [days] HH:mm")) --> 2 days 03:00
---```
---
---@class mods.duration
local M = {}
---
---Create a duration from numeric parts, an ISO 8601 string, or another duration.
---
---```lua
---local a = Duration({ day = 2, hour = 3 })
---local b = Duration("PT1H30M")
---local c = Duration(a)
---```
---
---@param input? string|mods.DurationParts|mods.Duration Duration parts, an ISO 8601 string, or another duration.
---@return mods.Duration duration
---@nodiscard
function M.new(input) end
---
---Create a duration from a numeric amount and unit.
---
---```lua
---local d = Duration(90, "minute")
---```
---
---@param input number Numeric amount to convert into a duration.
---@param unit? mods.durationUnit Unit used with the numeric amount. Defaults to `"ms"`.
---@return mods.Duration duration
---@nodiscard
function M.new(input, unit) end
---
---Return `true` when the value is a duration created by `mods.duration(...)`.
---
---```lua
---print(Duration.is_duration(Duration({ day = 2 }))) --> true
---print(Duration.is_duration({ day = 2 })) --> false
---```
---
---@param value any
---@return boolean isDuration
---@nodiscard
function M.is_duration(value) end
---
---Reusable immutable duration value for date arithmetic and formatting.
---
---@class mods.Duration
---@field private __index fun(t:mods.Duration,k:any):any
---@field private __eq fun(self: mods.Duration, other: mods.Duration): boolean
---@field private __tostring fun(self: mods.Duration): string
---@field milliseconds number Stored millisecond component.
---@field seconds number Stored second component.
---@field minutes number Stored minute component.
---@field hours number Stored hour component.
---@field days number Stored day component.
---@field months number Stored month component.
---@field years number Stored year component.
local Duration = {}
---
---Return a shallow copy of the duration value.
---
---```lua
---local d = Duration({ month = 1, day = 2 })
---local copy = d:clone()
---print(copy == d, rawequal(copy, d)) --> true false
---```
---
---@return mods.Duration duration
function Duration:clone() end
---
---Compare this duration to another duration-like value.
---
---Returns `-1` when smaller, `0` when equal, and `1` when larger.
---
---```lua
---print(Duration({ day = 1 }):compare({ hour = 24 })) --> 0
---```
---
---@param other number|string|mods.DurationParts|mods.Duration
---@return integer ordering
function Duration:compare(other) end
---
---Return the duration expressed in the requested unit.
---
---```lua
---local d = Duration({ day = 1, hour = 12 })
---print(d:as("hour")) --> 36
---```
---
---@param unit mods.durationUnit
---@return number amount
function Duration:as(unit) end
---
---Return a compacted duration using the module's canonical carry rules.
---
---```lua
---print(Duration({ minute = 90 }):normalize()) --> duration(hours=1, minutes=30)
---```
---
---@return mods.Duration duration
function Duration:normalize() end
---
---Return a new duration with another duration or unit amount added.
---
---```lua
---local a = Duration({ day = 2 })
---local b = a:add(3, "hour")
---print(b:format("D [days] HH:mm:ss")) --> 2 days 03:00:00
---```
---
---@param value number|mods.DurationParts|mods.Duration Signed amount to add, or another duration value.
---@param unit? mods.durationUnit Unit used when `value` is a number.
---@return mods.Duration duration
function Duration:add(value, unit) end
---
---Return a new duration with another duration or unit amount subtracted.
---
---```lua
---local a = Duration({ day = 2, hour = 3 })
---local b = a:subtract(3, "hour")
---print(b:format("D [days] HH:mm:ss")) --> 2 days 00:00:00
---```
---
---@param value number|mods.DurationParts|mods.Duration Signed amount to subtract, or another duration value.
---@param unit? mods.durationUnit Unit used when `value` is a number.
---@return mods.Duration duration
function Duration:subtract(value, unit) end
---
---Format the duration using duration tokens like `Y`, `MM`, `DD`, and `HH`.
---
---```lua
---local d = Duration({ day = 2, hour = 3, minute = 4 })
---print(d:format("D [days] HH:mm")) --> 2 days 03:04
---```
---
---@param pattern string Format pattern using supported duration tokens.
---@return string formatted
function Duration:format(pattern) end
---
---Return a human-readable relative-style phrase for the duration.
---
---By default this returns the bare phrase without `ago` / `in`. Pass `true`
---to include relative wording. You can also pass an options table for
---abbreviated output or explicit unit clamping.
---
---```lua
---local d = Duration({ day = 3 })
---print(d:humanize()) --> 3 days
---print(d:humanize(true)) --> in 3 days
---print(d:humanize({ short = true })) --> 3d
---```
---
---@param with_suffix_or_options? boolean|mods.DurationHumanizeOptions Whether to include `ago` / `in` style wording, or an options table.
---@param options? mods.DurationHumanizeOptions Additional options when the first argument is a boolean.
---@return string humanized
function Duration:humanize(with_suffix_or_options, options) end
---
---Return `true` when both duration values have identical components.
---
---```lua
---local a = Duration({ day = 2 })
---local b = Duration({ day = 2 })
---print(a:equals(b)) --> true
---```
---
---@param other any Value to compare against.
---@return boolean isEqual
function Duration:equals(other) end
---
---Return an ISO 8601 duration string.
---
---```lua
---print(Duration({ hour = 1, minute = 30 }):to_iso()) --> PT1H30M
---```
---
---@return string iso
function Duration:to_iso() end
---
---Return a debug-friendly string representation of the duration.
---
---```lua
---print(Duration({ day = 2, hour = 3 })) --> duration(days=2, hours=3)
---```
---
---@return string s
function Duration:tostring() end
---
---Return the same result as `tostring()` when coerced to a string.
---
---```lua
---print(Duration({ day = 2 })) --> duration(days=2)
---```
---
---@section Metamethods
---@return string s
function Duration:__tostring() end
---
---Return `true` when both duration values have identical components.
---
---```lua
---print(Duration({ day = 2 }) == Duration({ day = 2 })) --> true
---```
---
---@section Metamethods
---@param duration mods.Duration Duration to compare against.
---@return boolean isEqual
function Duration:__eq(duration) end
---
---Create a duration from numeric parts, an ISO 8601 string, or another duration.
---
---```lua
---local a = Duration({ day = 2, hour = 3 })
---local b = Duration("PT1H30M")
---local c = Duration(a)
---```
---
---@section Metamethods
---@param input? string|mods.DurationParts|mods.Duration Duration parts, an ISO 8601 string, or another duration.
---@return mods.Duration duration
function M:__call(input) end
---
---Create a duration from a numeric amount and unit.
---
---```lua
---local d = Duration(90, "minute")
---```
---
---@section Metamethods
---@param input number Numeric amount to convert into a duration.
---@param unit? mods.durationUnit Unit used with the numeric amount. Defaults to `"ms"`.
---@return mods.Duration duration
function M:__call(input, unit) end
return M