Skip to content

Instantly share code, notes, and snippets.

@jebai0521
Created June 20, 2020 02:10
Show Gist options
  • Save jebai0521/0c4297cce1845fa24dd2612b5a1da3be to your computer and use it in GitHub Desktop.
Save jebai0521/0c4297cce1845fa24dd2612b5a1da3be to your computer and use it in GitHub Desktop.
status2: (place, options = {}) => {
if (!place.openingHours
|| (!place.openingHours.periods && !place.openingHours.specific)
|| (place.openingHours.periods && !place.openingHours.periods.length)) {
return { status: UNKNOWN, openingTime: null };
}
const now = options.now || newDate(),
day = options.day || getDay(), // 1 - 7
yesterday = day - 1,
tomorrow = day + 1,
time = (options.time || getTime(place.timeZone, now)).replace(':', ''), // format 0900
openingHours = Opening.get(place.openingHours, { day }); // TODO: place.openingHours()
if (!openingHours || !openingHours[0]) {
return { status: UNKNOWN, openingTime: null };
}
// initial value
let status = UNKNOWN,
openingTime = null, // = openingHours[0].open.time;
allDay = false;
console.log('openingStatus.statue', JSON.stringify({ now, day, time, openingHours }));
for (let i = 0; i < openingHours.length; i += 1) {
const openingHour = openingHours[i];
if (openingHour) {
const { open, close } = openingHour,
openDay = Number(open.day);
if (openDay >= yesterday) {
if (openDay === yesterday) {
// check yesterday's opening hours
if (open.time > close.time && time <= close.time && time >= HOUR_0) {
// e.g. day = 2, time = 01:00
status = OpeningHours.openingStatus(close.time, time);
openingTime = close.time;
break;
}
} else if (openDay === day) {
if (open.time > close.time) {
// e.g. open: { time: 10:00 }, close { time: 02:00 }
if (time >= open.time && time <= HOUR_24) {
// e.g. day = 1, time = 11:00
status = OpeningHours.openingStatus(
hhmm(24 + Number(close.time.slice(0, 2)), close.time.slice(-2)),
time,
);
openingTime = close.time;
break;
} else if (time < open.time) {
openingTime = open.time;
}
} else if (open.time < close.time) {
if (open.time === HOUR_0 && close.time === HOUR_24) {
// open 24 hours
status = OPEN;
allDay = true;
break;
} else if (time < open.time) {
status = CLOSED;
openingTime = open.time;
break;
} else if (time >= open.time && time <= close.time) {
// standard opening hours
status = OpeningHours.openingStatus(close.time, time);
openingTime = close.time;
break;
} else if (openingHours[i + 1]) {
openingTime = openingHours[i + 1].open.time;
}
} else if (open.time === close.time) {
status = CLOSED;
if (openingHours[i + 1]) {
openingTime = openingHours[i + 1].open.time;
}
break;
}
} else {
// openingTime = open.time;
break;
}
}
} else {
// FIXME @wilson
}
}
return { status, openingTime, allDay };
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment