-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
99 lines (80 loc) · 3.35 KB
/
util.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
const { bestPossibleTime } = require('./model');
function findEarliestIdleTime(schedule, personIndex, duration) {
let earliestIdleTime = new bestPossibleTime();
if (!schedule || !schedule.length) {
console.error(`Schedule of person ${personIndex} is either undefined or not an array`);
return null;
}
const allStartTime = schedule.map(x => x[0]);
const allEndTime = schedule.map(x => x[1]);
for (let i = 1; i < schedule.length; i++) {
if (!allStartTime[i] || !allStartTime[i-1]) {
return null;
}
const startingHour = allStartTime[i].split(':')[0];
const startingMinute = allStartTime[i].split(':')[1];
const endingHour = allEndTime[i - 1].split(':')[0];
const endingMinute = allEndTime[i - 1].split(':')[1];
const startingTime = new Date();
startingTime.setHours(startingHour);
startingTime.setMinutes(startingMinute);
const endingTime = new Date();
endingTime.setHours(endingHour);
endingTime.setMinutes(endingMinute);
const diffHours = Math.abs(endingTime - startingTime) / (60 * 1000); //change to minute
if (!earliestIdleTime.startTime || !earliestIdleTime.endTime) {
if (diffHours > duration) {
earliestIdleTime.startTime = allEndTime[i - 1];
earliestIdleTime.endTime = allStartTime[i];
break;
}
}
}
return earliestIdleTime;
}
function getMaximumEndTime(idleTimeList) {
let maximumEndTime;
for (const idleTime of idleTimeList) {
const idleEndHour = idleTime.endTime.split(':')[0];
const idleEndMinute = idleTime.endTime.split(':')[1];
const idleEndHourFloat = parseFloat(idleEndHour) + parseFloat(idleEndMinute / 60);
if (!maximumEndTime) {
maximumEndTime = idleEndHourFloat;
}
else if (maximumEndTime > idleEndHourFloat) {
maximumEndTime = idleEndHourFloat;
}
}
return maximumEndTime;
}
function getEarliestIdleTime(idleTimeList, duration) {
let earliestIdleTime = new bestPossibleTime();
let earliestIdleTimeFloat;
const maximumEndTime = getMaximumEndTime(idleTimeList);
for (const idleTime of idleTimeList) {
const idleStartHour = idleTime.startTime.split(':')[0];
const idleStartMinute = idleTime.startTime.split(':')[1];
const idleStartHourFloat = parseFloat(idleStartHour) + parseFloat(idleStartMinute / 60);
if (!earliestIdleTimeFloat) {
earliestIdleTimeFloat = {
startTime: idleStartHourFloat,
endTime: idleStartHourFloat + parseFloat(duration / 60)
};
}
else if (earliestIdleTimeFloat.startTime < idleStartHourFloat) {
earliestIdleTimeFloat.startTime = idleStartHourFloat;
earliestIdleTimeFloat.endTime = idleStartHourFloat + parseFloat(duration / 60);
}
if (earliestIdleTimeFloat.endTime > maximumEndTime) {
return null;
}
}
earliestIdleTimeFloat.startTime = earliestIdleTimeFloat.startTime.toFixed(2);
earliestIdleTimeFloat.endTime = earliestIdleTimeFloat.endTime.toFixed(2);
earliestIdleTime.startTime = earliestIdleTimeFloat.startTime.toString().split('.')[0] + ':'
+ Math.round((earliestIdleTimeFloat.startTime.toString().split('.')[1] * 60) / 100);
earliestIdleTime.endTime = earliestIdleTimeFloat.endTime.toString().split('.')[0] + ':'
+ Math.round((earliestIdleTimeFloat.endTime.toString().split('.')[1] * 60) / 100);
return earliestIdleTime;
}
module.exports = { findEarliestIdleTime, getEarliestIdleTime };