-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopwatch.ts
56 lines (51 loc) · 1.26 KB
/
Stopwatch.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
function dateToISO8601(date: Date) {
const tzo = -date.getTimezoneOffset(),
dif = tzo >= 0 ? "+" : "-",
pad = function(num: number) {
const norm = Math.floor(Math.abs(num));
return (norm < 10 ? "0" : "") + norm;
};
return (
date.getFullYear() +
"-" +
pad(date.getMonth() + 1) +
"-" +
pad(date.getDate()) +
"T" +
pad(date.getHours()) +
":" +
pad(date.getMinutes()) +
":" +
pad(date.getSeconds()) +
dif +
pad(tzo / 60) +
":" +
pad(tzo % 60)
);
}
export interface IStopwatchOutput {
pluginDuration: number,
pluginStartDateTime: string,
pluginStopDateTime: string,
pluginStartTime: number,
pluginStopTime: number
}
export class Stopwatch {
private _startDateTime: Date;
private _startTimeStamp: number;
start(): void {
this._startTimeStamp = performance.now();
this._startDateTime = new Date(Date.now());
}
stop(): IStopwatchOutput {
const now = performance.now()
const pluginDuration = now - this._startTimeStamp;
return {
pluginDuration,
pluginStartDateTime: dateToISO8601(this._startDateTime),
pluginStopDateTime: dateToISO8601(new Date(Date.now())),
pluginStartTime: this._startTimeStamp,
pluginStopTime: now
};
}
}