-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
71 lines (56 loc) · 1.78 KB
/
utils.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
function updateTimes(schedule) {
let { duration_per_zone, zones, iterations, interval } = schedule;
let startTime = formatTime(new Date());
schedule.start_time = startTime;
schedule.end_time = calculateEndTime(schedule);
return schedule;
}
function calculateEndTime({ start_time, interval, iterations, zones, duration_per_zone }) {
let session = zones.length * duration_per_zone;
let totalTime = session * iterations + (iterations - 1) * interval;
let startSeconds = timeStringToSeconds(start_time);
return secondsToTimeString(startSeconds + totalTime * 60);
}
function timeStringToSeconds(string) {
if (!string) {
return;
}
let array = string.split(':');
let hoursInSeconds = parseInt(array[0]) * 60 * 60;
let minutesInSeconds = parseInt(array[1]) * 60;
return hoursInSeconds + minutesInSeconds + parseInt(array[2]);
}
function secondsToTimeString(totalSeconds) {
if (totalSeconds < 60) {
return '00:00:' + totalSeconds.toString();
}
if (totalSeconds > 24 * 60 * 60){
totalSeconds = totalSeconds - (24 * 60 * 60);
}
let minutesAndSeconds = totalSeconds % (60 * 60);
let seconds = minutesAndSeconds % 60;
let minutes = (minutesAndSeconds - seconds) / 60;
let hours = (totalSeconds - minutesAndSeconds) / (60 * 60);
if (seconds < 10) {
seconds = '0' + seconds;
}
if (minutes < 10) {
minutes = '0' + minutes;
}
if (hours < 10) {
hours = '0' + hours;
}
return hours.toString() + ':' + minutes.toString() + ':' + seconds.toString();
}
const formatTime = (rawDate) => {
let h = rawDate.getHours();
let m = rawDate.getMinutes();
let s = rawDate.getSeconds();
h = (h < 10) ? "0" + h : h;
m = (m < 10) ? "0" + m : m;
s = (s < 10) ? "0" + s : s;
return `${h}:${m}:${s}`;
}
module.exports = {
updateTimes
}