Skip to content

Commit

Permalink
Fixed edge cases with store hours (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
thumn authored Apr 30, 2020
1 parent 8acaf36 commit 899ca38
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/mapUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,34 +155,40 @@ function computeStoreOpen(hours) {
const openingTime = hoursSplit[0]; // e.g. "8:30am"
const closingTime = hoursSplit[1]; // e.g. "8:30pm"

const openingSuffix = openingTime.slice(-2);
const openingSuffix = openingTime.slice(-2).toLowerCase();
const openingHourMin = openingTime.slice(0, -2);
const openingHourMinSplit = openingHourMin.split(':');
let openingHour = parseInt(openingHourMinSplit[0], 10);
const openingMin =
openingHourMinSplit.length > 1 ? parseInt(openingHourMinSplit[1], 10) : 0;

const closingSuffix = closingTime.slice(-2);
const closingSuffix = closingTime.slice(-2).toLowerCase();
const closingHourMin = closingTime.slice(0, -2);
const closingHourMinSplit = closingHourMin.split(':'); // e.g. ["8", "30"]
let closingHour = parseInt(closingHourMinSplit[0], 10);
const closingMin =
closingHourMinSplit.length > 1 ? parseInt(closingHourMinSplit[1], 10) : 0;

// Edge case: 11pm-5am Daily (pm before am)
// Also consider: Current time is before opening time but same day
if (openingHour === 12 && openingSuffix === 'am') {
openingHour -= 12;
}

// Convert to 24 hour time
if (openingSuffix.toLowerCase() === 'pm') {
// e.g. 11pm = 23
if (openingSuffix === 'pm') {
openingHour += 12;
}

if (
closingSuffix.toLowerCase() === 'pm' ||
closingSuffix === 'pm' ||
(closingHour === 12 && closingSuffix === 'am')
) {
closingHour += 12;
} else if (closingSuffix === 'am') {
// e.g. 9am-3am Daily (closes the following day)
closingHour += 24;
}

const convertedOpeningTime = openingHour * 60 + openingMin;
Expand All @@ -196,6 +202,11 @@ function computeStoreOpen(hours) {
return `Open now until ${closingTime}`.replace('12am', 'Midnight');
}

// Store is opening later
if (currentTime < convertedOpeningTime) {
return `Closed until today ${openingTime}`;
}

// The store is closed at this time, so we need the next day and its opening time
return computeNextOpenDay(todaysDayIndex, hoursPerDayDict);
} catch (err) {
Expand Down

0 comments on commit 899ca38

Please sign in to comment.