-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hour.js
168 lines (160 loc) · 3.93 KB
/
Hour.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
function Hour(timeInString) {
if (!timeInString) {
this.hour = 0;
this.minute = 0;
this.sencond = 0;
}
else {
let times = timeInString.split(':');
this.hour = parseInt(times[0]);
this.minute = parseInt(times[1]);
this.sencond = parseInt(times[2]);
}
}
Hour.prototype = {
toSeconds: function () {
return this.hour * 60 * 60 + this.minute * 60 + this.sencond;;
},
toMinutes: function () {
return this.hour * 60 + this.minute + this.sencond / 60;
},
toHours: function () {
return this.hour + this.minute / 60 + this.sencond / (60 * 60);
},
copy: function (toCopy) {
this.hour = parseInt(toCopy.hour);
this.minute = parseInt(toCopy.minute);
this.sencond = parseInt(toCopy.sencond);
}
}
/**
* a >= b ?
* @param {Hour} a
* @param {Hour} b
*/
Hour.greaterOrEqual = function (a, b) {
if (a == null && b == null) {
console.warn('both of them are null value returning true');
return true;
}
else if (a == null || b == null) {
if (a == null) {
console.warn('a is null value returning false');
return false;
}
if (b == null) {
console.warn('b is null value returning true');
return true;
}
}
return a.toSeconds() >= b.toSeconds()
}
/**
* a > b ?
* @param {Hour} a
* @param {Hour} b
*/
Hour.greater = function (a, b) {
if (a == null && b == null) {
console.warn('both of them are null value returning false');
return false;
}
else if (a == null || b == null) {
if (a == null) {
console.warn('a is null value returning false');
return false;
}
if (b == null) {
console.warn('b is null value returning true');
return true;
}
}
return a.toSeconds() > b.toSeconds()
}
/**
* a == b ?
* @param {Hour} a
* @param {Hour} b
*/
Hour.equals = function (a, b) {
if (a == null && b == null) {
console.warn('both of them are null value returning true');
return true;
}
else if (a == null || b == null) {
if (a == null) {
console.warn('a is null value returning false');
return false;
}
if (b == null) {
console.warn('b is null value returning false');
return false;
}
}
return a.toSeconds() == b.toSeconds();
}
/**
* Construct Hour from seconds
* @param {Number} _seconds
*/
Hour.fromSeconds = function (_seconds) {
_seconds = parseInt(_seconds);
let toReturn = new Hour();
if (_seconds < 0) {
console.warn('seconds are negative value. underflowing')
_seconds = 24 * 60 * 60 + _seconds;
}
let hour = parseInt(Math.floor(_seconds / (60 * 60)));
_seconds -= hour * 60 * 60;
let minute = parseInt(Math.floor(_seconds / (60)));
_seconds -= minute * 60;
let second = _seconds;
toReturn.hour = hour;
toReturn.minute = minute;
toReturn.sencond = second;
return toReturn;
}
/**
* a+b
* @param {Hour} a
* @param {Hour} b
*/
Hour.add = function (a, b) {
return Hour.fromSeconds(a.toSeconds + b.toSeconds);
}
/**
* a-b
* @param {Hour} a
* @param {Hour} b
*/
Hour.sub = function (a, b) {
return Hour.fromSeconds(a.toSeconds + b.toSeconds);
}
Hour.prototype.toString = function () {
let numToString = function (num) {
if (num == 0)
return '00';
else if (num < 10)
return '0' + num
else
return num;
}
return `${numToString(this.hour)}:${numToString(this.minute)}:${numToString(this.sencond)}`
}
/**
* max(a,b)
* @param {Hour} a
* @param {Hour} b
*/
Hour.max = function(a, b){
return Hour.greaterOrEqual(a,b)? a : b;
}
/**
* min(a,b)
* @param {Hour} a
* @param {Hour} b
*/
Hour.min = function(a, b){
return Hour.greaterOrEqual(a,b)? b : a;
}
export {Hour}