-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeInterval.ts
152 lines (123 loc) · 4.04 KB
/
TimeInterval.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
/* eslint-disable no-magic-numbers */
import TimeIntervalInterface from './TimeIntervalInterface';
/**
* @description This service operates in absolutes. There is no difference taken into account between
* 2 hours ago and 2 hours from now. They are both equal, because the focus is time intervals, not linear points
* in time and their relations.
*/
class TimeInterval implements TimeIntervalInterface
{
// Millisecond Measures of Common Time Units
public static readonly ONE_SECOND = 1000;
public static readonly ONE_MINUTE = 60000;
public static readonly ONE_DAY = 86400000;
public static readonly ONE_HOUR = 3600000;
public static readonly ONE_WEEK = 604800000;
private readonly milliseconds: number;
private constructor(milliseconds: number)
{
this.milliseconds = milliseconds;
}
public static fromTimeBetweenTwoDates(
date1: Date | number,
date2: Date | number,
): TimeInterval
{
const firstDateInMilliseconds = date1 instanceof Date ? date1.getTime() : date1;
const secondDateInMilliseconds = date2 instanceof Date ? date2.getTime() : date2;
return new this(Math.abs(firstDateInMilliseconds - secondDateInMilliseconds));
}
public static forSpecifiedMilliseconds(milliseconds: number): TimeIntervalInterface
{
return new this(milliseconds);
}
public static forSpecifiedSeconds(seconds: number): TimeIntervalInterface
{
return new this(this.ONE_SECOND * seconds);
}
public static forOneMinute(): TimeIntervalInterface
{
return new this(this.ONE_MINUTE);
}
public static forSpecifiedMinutes(minutes: number): TimeIntervalInterface
{
return new this(this.ONE_MINUTE * minutes);
}
public static forOneHour(): TimeInterval
{
return new this(this.ONE_HOUR);
}
public static forSpecifiedHours(hours: number): TimeIntervalInterface
{
return new this(this.ONE_HOUR * hours);
}
public static forOneDay(): TimeIntervalInterface
{
return new this(this.ONE_DAY);
}
public static forSpecifiedDays(days: number): TimeIntervalInterface
{
return new this(this.ONE_DAY * days);
}
public static forOneWeek(): TimeIntervalInterface
{
return new this(this.ONE_WEEK);
}
public static forSpecifiedWeeks(weeks: number): TimeIntervalInterface
{
return new this(this.ONE_WEEK * weeks);
}
public static absoluteTimeFromNow(date: Date): TimeIntervalInterface
{
return TimeInterval.fromTimeBetweenTwoDates(Date.now(), date.getTime());
}
public inMilliseconds(): number
{
return this.milliseconds;
}
public inSeconds(): number
{
return this.milliseconds / TimeInterval.ONE_SECOND;
}
public inMinutes(): number
{
return this.milliseconds / TimeInterval.ONE_MINUTE;
}
public inHours(): number
{
return this.milliseconds / TimeInterval.ONE_HOUR;
}
public inDays(): number
{
return this.milliseconds / TimeInterval.ONE_DAY;
}
public inWeeks(): number
{
return this.milliseconds / TimeInterval.ONE_WEEK;
}
/**
* @description This should only ever be approximate, or not entirely accurate, as some years have more or less days
* according to the Gregorian Calendar.
*/
public inApproximateYears(): number
{
return this.milliseconds / (TimeInterval.ONE_DAY * 365);
}
public addToDate(date: Date): Date
{
return new Date(this.milliseconds + date.getTime());
}
public subtractFromDate(date: Date): Date
{
return new Date(date.getTime() - this.milliseconds);
}
public isLongerThan(interval: TimeIntervalInterface): boolean
{
return Math.abs(this.inMilliseconds()) > Math.abs(interval.inMilliseconds());
}
public isShorterThan(interval: TimeIntervalInterface): boolean
{
return Math.abs(this.inMilliseconds()) < Math.abs(interval.inMilliseconds());
}
}
export default TimeInterval;