forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime-interval.ts
More file actions
46 lines (39 loc) · 1.31 KB
/
time-interval.ts
File metadata and controls
46 lines (39 loc) · 1.31 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
import { logger } from '../utils/logger';
export class TimeInterval implements ISerializable<TimeInterval> {
domain: string = '';
intervals: string[] = [];
day: string = '';
init(day: string, domain: string) {
this.domain = domain;
this.intervals = [];
this.day = day;
}
addInterval() {
const stringDate = this.getCurrentStringDate();
this.intervals.push(stringDate + '-' + stringDate);
logger.log(`Add interval ${this.domain} - ${stringDate} - ${stringDate}`);
}
closeInterval() {
const stringDate = this.getCurrentStringDate();
const currentInterval = this.intervals[this.intervals.length - 1];
if (currentInterval != null) {
if (currentInterval.split('-')[0] == currentInterval.split('-')[1]) {
this.intervals.pop();
this.intervals.push(currentInterval.split('-')[0] + '-' + stringDate);
logger.log(
`Close interval ${this.domain} - ${currentInterval.split('-')[0]} - ${stringDate}`,
);
}
}
}
deserialize(input: TimeInterval): TimeInterval {
this.domain = input.domain;
this.day = input.day;
this.intervals = input.intervals;
return this;
}
private getCurrentStringDate(): string {
const date = new Date();
return date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
}
}