generated from TBD54566975/tbd-project-template
/
time.ts
78 lines (72 loc) · 2.6 KB
/
time.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
import { Temporal } from '@js-temporal/polyfill';
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
/**
* Time related utilities.
*/
export class Time {
/**
* sleeps for the desired duration
* @param durationInMillisecond the desired amount of sleep time
* @returns when the provided duration has passed
*/
public static async sleep(durationInMillisecond: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, durationInMillisecond));
}
/**
* We must sleep for at least 2ms to avoid timestamp collisions during testing.
* https://github.com/TBD54566975/dwn-sdk-js/issues/481
*/
public static async minimalSleep(): Promise<void> {
await Time.sleep(2);
}
/**
* Returns an UTC ISO-8601 timestamp with microsecond precision accepted by DWN.
* using @js-temporal/polyfill
*/
public static getCurrentTimestamp(): string {
return Temporal.Now.instant().toString({ smallestUnit: 'microseconds' });
}
/**
* Creates a UTC ISO-8601 timestamp in microsecond precision accepted by DWN.
* @param options - Options for creating the timestamp.
* @returns string
*/
public static createTimestamp(options: {
year?: number, month?: number, day?: number, hour?: number, minute?: number, second?: number, millisecond?: number, microsecond?: number
}): string {
const { year, month, day, hour, minute, second, millisecond, microsecond } = options;
return Temporal.ZonedDateTime.from({
timeZone: 'UTC',
year,
month,
day,
hour,
minute,
second,
millisecond,
microsecond
}).toInstant().toString({ smallestUnit: 'microseconds' });
}
/**
* Creates a UTC ISO-8601 timestamp offset from now or given timestamp accepted by DWN.
* @param offset Negative number means offset into the past.
*/
public static createOffsetTimestamp(offset: { seconds: number }, timestamp?: string): string {
const timestampInstant = timestamp ? Temporal.Instant.from(timestamp) : Temporal.Now.instant();
const offsetDuration = Temporal.Duration.from(offset);
const offsetInstant = timestampInstant.add(offsetDuration);
return offsetInstant.toString({ smallestUnit: 'microseconds' });
}
/**
* Validates that the provided timestamp is a valid number
* @param timestamp the timestamp to validate
* @throws DwnError if timestamp is not a valid number
*/
public static validateTimestamp(timestamp: string): void {
try {
Temporal.Instant.from(timestamp);
} catch {
throw new DwnError(DwnErrorCode.TimestampInvalid, `Invalid timestamp: ${timestamp}`);
}
}
}