forked from Menturan/MMM-OpeningHours
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpeningHoursLogic.js
51 lines (47 loc) · 1.63 KB
/
OpeningHoursLogic.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
function calculateOpeningHours(opening_hours, currentTime) {
let weekdayNumber = currentTime.weekday()+1;
let closingTime = undefined
let openingTime = undefined
let placeIsOpen = false
const openingHoursYesterday = opening_hours[weekdayNumber - 1]
// Closed yesterday?
if (openingHoursYesterday !== undefined) {
// Yesterday time still valid?
openingHoursYesterday.forEach(period => {
closingTime = period.close
openingTime = period.open
placeIsOpen = currentTime.isBetween(openingTime, closingTime)
if (placeIsOpen) {
return [openingTime, closingTime, placeIsOpen]
}
})
}
// Closed today?
if (opening_hours[weekdayNumber] === undefined) {
const nextOpenDay = getNextOpenDay(opening_hours, weekdayNumber)
closingTime = nextOpenDay.close
openingTime = nextOpenDay.open
placeIsOpen = currentTime.isBetween(openingTime, closingTime)
return [openingTime, closingTime, placeIsOpen]
}
const openingHoursToday = opening_hours[weekdayNumber]
openingHoursToday.forEach(period => {
closingTime = period.close
openingTime = period.open
placeIsOpen = currentTime.isBetween(openingTime, closingTime)
if (placeIsOpen) {
return [openingTime, closingTime, placeIsOpen];
}
})
return [openingTime, closingTime, placeIsOpen];
}
var isServerSide = true
try{
var dummy = window !== undefined
isServerSide = false
}catch (e) {
// Do nothing...
}
if (isServerSide){
module.exports = { calculateOpeningHours };
}