diff --git a/lib/mapUtils.js b/lib/mapUtils.js index 68c4ab6b..7ff390c2 100644 --- a/lib/mapUtils.js +++ b/lib/mapUtils.js @@ -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; @@ -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) {