-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
97 lines (95 loc) · 1.77 KB
/
mod.ts
File metadata and controls
97 lines (95 loc) · 1.77 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
/**
* Shared functions to manage time in seconds instead of milliseconds
* @module
*/
/**
* The timestamp (in seconds) when the current process started.
*
* @example
* ```ts
* import { startTime } from './time.ts';
*
* console.log(`Process started at: ${new Date(startTime * 1000)}`);
* ```
*/
export const startTime = performance.timeOrigin / 1000
let lastTime = startTime
/**
* Return the current timestamp in seconds since the epoch.
* Uses the performance API for high precision timing.
* Ensured to be unique per process.
*
* @example
* ```ts
* import { now } from './time.ts';
*
* const timestamp = now();
* console.log(timestamp);
* ```
*/
export const now = (): number => {
const time = startTime + performance.now() / 1000
if (time === lastTime) return now()
lastTime = time
return time
}
/** One second in seconds.
*
* @example
* ```ts
* import { SEC } from './time.ts';
*
* const tenSeconds = 10 * SEC;
* ```
*/
export const SEC = 1
/** One minute in seconds.
*
* @example
* ```ts
* import { MIN } from './time.ts';
*
* const fiveMinutes = 5 * MIN;
* ```
*/
export const MIN = 60
/** One hour in seconds.
*
* @example
* ```ts
* import { HOUR } from './time.ts';
*
* const twoHours = 2 * HOUR;
* ```
*/
export const HOUR = 60 * MIN
/** One day in seconds.
*
* @example
* ```ts
* import { DAY } from './time.ts';
*
* const threeDays = 3 * DAY;
* ```
*/
export const DAY = 24 * HOUR
/** One week in seconds.
*
* @example
* ```ts
* import { WEEK } from './time.ts';
*
* const twoWeeks = 2 * WEEK;
* ```
*/
export const WEEK = 7 * DAY
/** One year in seconds (accounting for leap years).
*
* @example
* ```ts
* import { YEAR } from './time.ts';
*
* const fiveYears = 5 * YEAR;
* ```
*/
export const YEAR = 365.2422 * DAY