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