-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.ts
342 lines (307 loc) · 8.96 KB
/
conversions.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* DatePlus A simple program to assist with date manipulation
*
* @license MIT
* @version 4.0.0-beta1
* @author Luke Zhang luke-zhang-04.github.io
* @copyright Copyright (C) 2020 - 2021 Luke Zhang
*/
import {Values} from "."
/**
* Object that stores milliseconds with the ms key
*/
export interface MsObj {
[index: string]: number
ms: number
}
/**
* Object that stores seconds and milliseconds with the seconds and ms keys
*/
export interface SecondsObj extends MsObj {
seconds: number
}
/**
* Object that stores minutes, seconds, and milliseconds with the minutes, seconds, and ms keys
*/
export interface MinutesObj extends SecondsObj {
minutes: number
}
/**
* Object that stores hours, minutes, seconds, and milliseconds with the hours, minutes, seconds, and ms keys
*/
export interface HoursObj extends MinutesObj {
hours: number
}
/**
* Object that stores days, hours, minutes, seconds, and milliseconds with the days, hours,
* minutes, seconds, and ms keys
*/
export interface DaysObj {
[index: string]: number | undefined
ms?: number
seconds?: number
minutes?: number
hours?: number
days: number
}
/**
* Object that stores weeks, days, hours, minutes, seconds, and milliseconds with the weeks, days,
* hours, minutes, seconds, and ms keys
*/
export interface WeeksObj extends DaysObj {
weeks: number
}
/**
* Object that stores months, weeks, days, hours, minutes, seconds, and milliseconds with the
* months, weeks, days, hours, minutes, seconds, and ms keys
*/
export interface MonthsObj extends DaysObj {
weeks?: number
months: number
}
/**
* Object that stores years, months, weeks, days, hours, minutes, seconds, and milliseconds with
* the years, months, weeks, days, hours, minutes, seconds, and ms keys
*/
export interface YearsObj extends MonthsObj {
years: number
}
/**
* Object that stores day, with the day key
*/
export interface DayObj {
[index: string]: number
day: number
}
/**
* Object that stores month and day, with the month and day keys
*/
export interface MonthObj extends DayObj {
month: number
}
/**
* Object that stores year, month, and day, with the year, month, and day keys
*/
export interface YearObj extends MonthObj {
year: number
}
/**
* Converts milliseconds to seconds with remainders
*
* @param ms - Milliseconds to convert
* @returns Object with seconds and milliseconds
*/
export const msToSecs = (ms: number): SecondsObj => ({
ms: ms % Values.MsPerSec,
seconds: (ms - (ms % Values.MsPerSec)) / Values.MsPerSec,
})
export const msToSeconds = msToSecs
/**
* Converts milliseconds to minutes with remainders
*
* @param ms - Milliseconds to convert
* @returns Object with minutes, seconds, and milliseconds
*/
export const msToMins = (ms: number): MinutesObj => {
const milliseconds = (ms % Values.MsPerSec) % Values.MsPerSec
const seconds = ((ms - (ms % Values.MsPerSec)) / Values.MsPerSec) % Values.SecsPerMin
const minutes =
(ms - seconds * Values.MsPerSec - milliseconds) / (Values.MsPerSec * Values.SecsPerMin)
return {
ms: milliseconds,
seconds,
minutes,
}
}
export const msToMinutes = msToMins
/**
* Converts milliseconds to hours with remainders
*
* @param ms - Milliseconds to convert
* @returns Object with hours, minutes, seconds, and milliseconds
*/
export const msToHrs = (ms: number): HoursObj => {
const milliseconds = (ms % Values.MsPerSec) % Values.MsPerSec
const seconds = ((ms - (ms % Values.MsPerSec)) / Values.MsPerSec) % Values.SecsPerMin
const minutes =
((ms - seconds * Values.MsPerSec - milliseconds) / (Values.MsPerSec * Values.SecsPerMin)) %
Values.MinsPerHr
const hours =
(ms -
minutes * Values.MsPerSec * Values.SecsPerMin -
seconds * Values.MsPerSec -
milliseconds) /
(Values.MsPerSec * Values.SecsPerMin * Values.MinsPerHr)
return {
ms: milliseconds,
seconds,
minutes,
hours,
}
}
export const msToHours = msToHrs
/**
* Converts milliseconds to days with remainders
*
* @param ms - Milliseconds to convert
* @returns Object with days, hours, minutes, seconds, and milliseconds
*/
export const msToDays = (ms: number): DaysObj => {
const milliseconds = (ms % Values.MsPerSec) % Values.MsPerSec
const seconds = ((ms - (ms % Values.MsPerSec)) / Values.MsPerSec) % Values.SecsPerMin
const minutes =
((ms - seconds * Values.MsPerSec - milliseconds) / (Values.MsPerSec * Values.SecsPerMin)) %
Values.MinsPerHr
const hours =
((ms -
minutes * Values.MsPerSec * Values.SecsPerMin -
seconds * Values.MsPerSec -
milliseconds) /
(Values.MsPerSec * Values.SecsPerMin * Values.MinsPerHr)) %
Values.HrsPerDay
const days =
(ms -
hours * Values.MsPerSec * Values.SecsPerMin * Values.MinsPerHr -
minutes * Values.MsPerSec * Values.SecsPerMin -
seconds * Values.MsPerSec -
milliseconds) /
(Values.MsPerSec * Values.SecsPerMin * Values.MinsPerHr * Values.HrsPerDay)
return {
ms: milliseconds,
seconds,
minutes,
hours,
days,
}
}
/* eslint-disable max-len */
/**
* Converts seconds to milliseconds
*
* @param secs - Seconds to convert
* @returns Converted milliseconds
*/
export const secsToMs = (secs: number): number => secs * Values.MsPerSec
export const secondsToMs = secsToMs
/**
* Converts seconds to minutes with remainders
*
* @param secs - Seconds to convert
* @returns Converted minutes
*/
export const secsToMins = (secs: number): MinutesObj => msToMins(secsToMs(secs))
export const secondsToMinutes = secsToMins
/**
* Converts seconds to hours with remainders
*
* @param secs - Seconds to convert
* @returns Converted hours
*/
export const secsToHrs = (secs: number): HoursObj => msToHrs(secsToMs(secs))
export const secondsToHours = secsToHrs
/**
* Converts seconds to days with remainders
*
* @param secs - Seconds to convert
* @returns Converted days
*/
export const secsToDays = (secs: number): DaysObj => msToDays(secsToMs(secs))
export const secondsToDays = secsToDays
/**
* Converts hours to milliseconds
*
* @param mins - Minutes to convert
* @returns Converted milliseconds
*/
export const minsToMs = (mins: number): number => mins * Values.SecsPerMin * Values.MsPerSec
export const minutesToMs = secsToDays
/**
* Converts hours to seconds
*
* @param mins - Minutes to convert
* @returns Converted seconds
*/
export const minsToSecs = (mins: number): number => mins * Values.SecsPerMin
export const minutesToSeconds = minsToSecs
/**
* Converts minutes to hours with remainders
*
* @param mins - Minutes to convert
* @returns Converted hours
*/
export const minsToHrs = (mins: number): HoursObj => msToHrs(minsToMs(mins))
export const minutesToHours = minsToHrs
/**
* Converts minutes to days with remainders
*
* @param mins - Minutes to convert
* @returns Converted days
*/
export const minsToDays = (mins: number): DaysObj => msToDays(minsToMs(mins))
export const minutesToDays = minsToDays
/**
* Converts hours to milliseconds
*
* @param hours - Hours to convert
* @returns Converted milliseconds
*/
export const hrsToMs = (hours: number): number =>
hours * Values.MinsPerHr * Values.SecsPerMin * Values.MsPerSec
export const hoursToMs = hrsToMs
/**
* Converts hours to seconds
*
* @param hours - Hours to convert
* @returns Converted seconds
*/
export const hrsToSecs = (hours: number): number => hours * Values.MinsPerHr * Values.SecsPerMin
export const hoursToSeconds = hrsToSecs
/**
* Converts hours to minutes
*
* @param hours - Hours to convert
* @returns Converted minutes
*/
export const hrsToMins = (hours: number): number => hours * Values.MinsPerHr
export const hoursToMinutes = hrsToMins
/**
* Converts hours to days with remainders
*
* @param hrs - Hours to convert
* @returns Converted days
*/
export const hrsToDays = (hrs: number): DaysObj => msToDays(hrsToMs(hrs))
export const hoursToDays = hrsToDays
/**
* Converts days to milliseconds
*
* @param days - Days to convert
* @returns Converted milliseconds
*/
export const daysToMs = (days: number): number =>
days * Values.HrsPerDay * Values.MinsPerHr * Values.SecsPerMin * Values.MsPerSec
/**
* Converts days to seconds
*
* @param days - Days to convert
* @returns Converted seconds
*/
export const daysToSecs = (days: number): number =>
days * Values.HrsPerDay * Values.MinsPerHr * Values.SecsPerMin
export const daysToSeconds = daysToSecs
/**
* Converts days to minutes
*
* @param days - Days to convert
* @returns Converted minutes
*/
export const daysToMins = (days: number): number => days * Values.HrsPerDay * Values.MinsPerHr
export const daysToMinutes = daysToMins
/**
* Converts days to hours
*
* @param days - Days to convert
* @returns Converted hours
*/
export const daysToHrs = (days: number): number => days * Values.HrsPerDay
export const daysToHours = daysToHrs