forked from icholy/Duration.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration.ts
299 lines (223 loc) · 7.31 KB
/
duration.ts
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
import { Temporal, toTemporalInstant } from '@js-temporal/polyfill';
Date.prototype.toTemporalInstant = toTemporalInstant;
const unitMap = {
'ns': 'nanoseconds',
'us': 'microseconds',
'µs': 'microseconds',
'μs': 'microseconds',
'ms': 'milliseconds',
's': 'seconds',
'm': 'minutes',
'h': 'hours',
'd': 'days',
'w': 'weeks',
'mo': 'months',
'y': 'years',
};
/**
* Properties for setting/adding/subtracing a duration.
* @public
*/
export interface TemporalDuration {
years?: number,
months?: number,
weeks?: number,
days?: number,
hours?: number,
minutes?: number,
seconds?: number,
milliseconds?: number,
microseconds?: number,
nanoseconds?: number,
}
/**
* Contains all of the different types of timestamps that can be used in Discord.
* @public
*/
export enum DiscordTimestamp {
/**
* @example "12:00AM"
*/
ShortTime = 't',
/**
* @example "12:00:00AM"
*/
LongTime = 'T',
/**
* @example "01/01/1970"
*/
ShortDate = 'd',
/**
* @example "January 01, 1970"
*/
LongDate = 'D',
/**
* @example "January 01, 1970 at 12:00AM"
*/
ShortDateTime = 'f',
/**
* @example "Monday, January 01, 1970 at 12:00AM"
*/
LongDateTime = 'F',
/**
* @example "2 seconds ago"
*/
Relative = 'R',
}
/**
* Additional options when adding/subtracting from a duration.
* @public
*/
export interface DurationOptions {
relativeTo?: RelativeDurationOptions,
}
/**
* Options for a starting point when adding/subtracting
* @public
*/
export interface RelativeDurationOptions {
/**
* Starting date
*/
date: Date,
/**
* Time zone
* @defaultValue `UTC`
*/
timeZone?: string,
}
export class Duration {
public duration: Temporal.Duration;
/**
* @param duration - Starting duration
* @param options - Additional Options
*/
constructor(duration: string | TemporalDuration, options?: DurationOptions) {
if (typeof duration === 'string') {
this.duration = this.parseString(duration);
}
else {
this.duration = Temporal.Duration.from(duration);
}
const relative = options?.relativeTo?.date.toTemporalInstant().toString({ timeZone: options.relativeTo.timeZone ?? 'UTC' });
this.duration = this.duration.round({ largestUnit: 'years', relativeTo: relative ?? Temporal.Now.plainDateISO() });
}
/**
* Adds to the current duration
* @see {@link https://www.fleco.cloud/packages/duration/classes/duration/#add | Duration#add}
* @param duration - Total time to add
* @param options - Options for adding a duration
* @returns Current duration
*/
public add(duration: string | TemporalDuration, options?: DurationOptions): Duration {
const relative = options?.relativeTo?.date.toTemporalInstant().toString({ timeZone: options.relativeTo.timeZone ?? 'UTC' });
if (typeof duration === 'string') {
const dur = this.parseString(duration);
this.duration = this.duration.add(dur, { relativeTo: relative ?? Temporal.Now.plainDateISO() });
}
else {
this.duration = this.duration.add(duration, { relativeTo: relative ?? Temporal.Now.plainDateISO() });
}
this.duration = this.duration.round({ largestUnit: 'years', relativeTo: relative ?? Temporal.Now.plainDateISO() });
return this;
}
/**
* Subtracts from the current duration
* @see {@link https://www.fleco.cloud/packages/duration/classes/duration/#sub | Duration#sub}
* @param duration - Total time to subtract
* @param options - Options for subtracting a duration
* @returns Current duration
*/
public sub(duration: string | TemporalDuration, options?: DurationOptions): Duration {
const relative = options?.relativeTo?.date.toTemporalInstant().toString({ timeZone: options.relativeTo.timeZone ?? 'UTC' });
if (typeof duration === 'string') {
const dur = this.parseString(duration);
this.duration = this.duration.subtract(dur, { relativeTo: relative ?? Temporal.Now.plainDateISO() });
}
else {
this.duration = this.duration.subtract(duration, { relativeTo: relative ?? Temporal.Now.plainDateISO() });
}
this.duration = this.duration.round({ largestUnit: 'years', relativeTo: relative ?? Temporal.Now.plainDateISO() });
return this;
}
/**
* Outputs the end date of the duration
* @see {@link https://www.fleco.cloud/packages/duration/classes/duration/#endDate | Duration#endDate} for documentation
* @param options - Additional Options
* @returns Date
*/
public endDate(options?: DurationOptions): Date {
const relative = options?.relativeTo?.date.toTemporalInstant().toString({ timeZone: options.relativeTo.timeZone ?? 'UTC' });
const currentDate = Temporal.Now;
return new Date(currentDate.instant().epochMilliseconds + this.duration.total({ unit: 'milliseconds', relativeTo: relative ?? currentDate.plainDateTimeISO() }));
}
/**
* Shows the current duration as a string
* @see {@link https://www.fleco.cloud/packages/duration/classes/duration/#toString | Duration#toString} for documentation
* @example
* ```
* const duration = new Duration({ milliseconds: 12389348 });
*
* console.log(duration.toString());
*
* Output:
*
* "3 hours 26 minutes 29 seconds"
* ```
*/
public toString(): string {
let str = '';
if (this.duration.years > 0) {
str += `${this.duration.years} ${this.duration.years > 1 ? 'years' : 'years'} `;
}
if (this.duration.days > 0) {
str += `${this.duration.days} ${this.duration.days > 1 ? 'days' : 'day'} `;
}
if (this.duration.hours > 0) {
str += `${this.duration.hours} ${this.duration.hours > 1 ? 'hours' : 'hour'} `;
}
if (this.duration.minutes > 0) {
str += `${this.duration.minutes} ${this.duration.minutes > 1 ? 'minutes' : 'minute'} `;
}
if (this.duration.seconds > 0) {
str += `${this.duration.seconds} ${this.duration.seconds > 1 ? 'seconds' : 'second'} `;
}
return str;
}
/**
* Outputs a timestamp string for Discord.
* @see {@link https://www.fleco.cloud/packages/duration/classes/duration/#toDiscordTimestamp | Duration#toDiscordTimestamp} for documentation
* @param [type=DiscordTimestamp.ShortDateTime] - Type of timestamp
* @param options - Additional Options
*/
public toDiscordTimestamp(type: DiscordTimestamp = DiscordTimestamp.ShortDateTime, options?: DurationOptions): string {
const relative = options?.relativeTo?.date.toTemporalInstant().toString({ timeZone: options.relativeTo.timeZone ?? 'UTC' });
const end = Temporal.Now.instant().epochSeconds + this.duration.total({ unit: 'seconds', relativeTo: relative ?? Temporal.Now.plainDateISO() });
return `<t:${end}:${type}>`;
}
private parseString(duration: string): Temporal.Duration {
const unitRegex = /([-+\d.]+)([a-zµμ]+)/g;
const durations: TemporalDuration = {};
let match: RegExpExecArray | null;
if (!unitRegex.test(duration)) {
throw new Error(`invalid duration: ${duration}`);
}
unitRegex.lastIndex = 0;
while ((match = unitRegex.exec(duration)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (match.index === unitRegex.lastIndex) {
unitRegex.lastIndex++;
}
const value = parseInt(match[1]!);
const unit = match[2];
if (isNaN(value) || !unit) {
throw new Error(`invalid duration : ${duration}`);
}
if (!unitMap[unit as keyof typeof unitMap]) {
throw new Error(`invalid unit : ${unit}`);
}
durations[unitMap[unit as keyof typeof unitMap] as keyof TemporalDuration] = value;
}
return Temporal.Duration.from(durations);
}
}