-
Notifications
You must be signed in to change notification settings - Fork 7
/
timeHandler.js
203 lines (109 loc) · 4.71 KB
/
timeHandler.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/****************************************************************************
* timeHandler.js
* openacousticdevices.info
* November 2019
*****************************************************************************/
const {ipcRenderer} = require('electron');
const constants = require('./constants.js');
const ui = require('./ui.js');
let storedTimeZoneOffset = 0;
function sortPeriods (periods) {
const sortedPeriods = periods.sort((a, b) => {
return a.startMins - b.startMins;
});
return sortedPeriods;
}
exports.sortPeriods = sortPeriods;
function storeTimeZoneOffset () {
storedTimeZoneOffset = 0;
if (ui.getTimeZoneMode() === constants.TIME_ZONE_MODE_LOCAL) {
const currentDate = new Date();
storedTimeZoneOffset = -currentDate.getTimezoneOffset();
}
if (ui.getTimeZoneMode() === constants.TIME_ZONE_MODE_CUSTOM) {
storedTimeZoneOffset = ipcRenderer.sendSync('request-custom-time-zone');
}
}
exports.storeTimeZoneOffset = storeTimeZoneOffset;
function getTimeZoneOffset () {
return storedTimeZoneOffset;
}
exports.getTimeZoneOffset = getTimeZoneOffset;
/* ------------------------------------------------- Time zone conversion functions ------------------------------------------------- */
function shiftTime (time, toUTC) {
/* Offset is given as UTC - local time in minutes */
let timeZoneOffset = storedTimeZoneOffset;
timeZoneOffset = toUTC ? timeZoneOffset * -1 : timeZoneOffset;
time = (time + timeZoneOffset) % constants.MINUTES_IN_DAY;
/* If time zone offset move time over midnight */
if (time < 0) {
time += constants.MINUTES_IN_DAY;
}
return time;
}
function shiftTimePeriod (timePeriod, toUTC) {
const startMins = shiftTime(timePeriod.startMins, toUTC);
const endMins = shiftTime(timePeriod.endMins, toUTC);
return {
startMins,
endMins
};
}
exports.shiftTimePeriod = shiftTimePeriod;
/* Convert a list of time periods from UTC to local */
function shiftTimePeriods (tps, toUTC) {
let shiftedTimePeriods = [];
for (let i = 0; i < tps.length; i++) {
const timePeriod = tps[i];
const shiftedTimePeriod = shiftTimePeriod(timePeriod, toUTC);
shiftedTimePeriods.push({
startMins: shiftedTimePeriod.startMins,
endMins: shiftedTimePeriod.endMins
});
}
shiftedTimePeriods = sortPeriods(shiftedTimePeriods);
shiftedTimePeriods = checkTimePeriodsForOverlaps(shiftedTimePeriods);
return shiftedTimePeriods;
}
exports.shiftTimePeriods = shiftTimePeriods;
/* ------------------------------------------------- Time period checks ------------------------------------------------- */
/* See if any newly created time periods overlap and can be merged */
function checkTimePeriodsForOverlaps (timePeriods) {
for (let i = 0; i < timePeriods.length; i++) {
for (let j = 0; j < timePeriods.length; j++) {
if (timePeriods[i].startMins === timePeriods[j].startMins && timePeriods[i].endMins === timePeriods[j].endMins) {
continue;
}
if (timePeriods[i].endMins === timePeriods[j].startMins) {
timePeriods[i].endMins = timePeriods[j].endMins;
timePeriods.splice(j, 1);
return checkTimePeriodsForOverlaps(timePeriods);
}
}
}
return timePeriods;
}
exports.checkTimePeriodsForOverlaps = checkTimePeriodsForOverlaps;
/* ------------------------------------------------- Other functions ------------------------------------------------- */
/* Get the text representation of the current timeZone */
function getTimeZoneText () {
let timeZoneText = 'UTC';
if (storedTimeZoneOffset === 0) return timeZoneText;
const timeZoneOffsetHours = storedTimeZoneOffset < 0 ? Math.ceil(storedTimeZoneOffset / constants.MINUTES_IN_HOUR) : Math.floor(storedTimeZoneOffset / constants.MINUTES_IN_HOUR);
const timeZoneOffsetMins = Math.abs(storedTimeZoneOffset % constants.MINUTES_IN_HOUR);
timeZoneText += storedTimeZoneOffset > 0 ? '+' : '-';
timeZoneText += Math.abs(timeZoneOffsetHours);
if (timeZoneOffsetMins > 0) timeZoneText += ':' + ('00' + timeZoneOffsetMins).slice(-2);
return timeZoneText;
}
exports.getTimeZoneText = getTimeZoneText;
/* Pad the left of each time with zeroes */
function pad (n) {
return (n < 10) ? ('0' + n) : n;
}
/* Convert the number of minutes through a day to a HH:MM formatted string */
function minsToTimeString (mins) {
const timeHours = Math.floor(mins / constants.MINUTES_IN_HOUR);
return pad(timeHours) + ':' + pad((mins - (timeHours * constants.MINUTES_IN_HOUR)));
}
exports.minsToTimeString = minsToTimeString;