const toMilliseconds = hour => { return hour * 60 * 60 * 1000 } const leftPad = num => { return num < 10 ? '0' : '' } const msToString = ms => { const minFromMS = ms / 60 / 1000 const min = Math.round(minFromMS % 60) const h = Math.floor(minFromMS / 60) return `${leftPad(h)}${h}:${leftPad(min)}${min}` } const resetDate = date => { date.setHours(0) date.setMinutes(0) date.setSeconds(0) date.setMilliseconds(0) return date } // starts with sunday const atomicOpeningTimes = [ { from: 0, to: 0, nextOpen: toMilliseconds(24) + toMilliseconds(7), }, { from: toMilliseconds(7), to: toMilliseconds(20), nextOpen: toMilliseconds(24) + toMilliseconds(7), }, { from: toMilliseconds(7), to: toMilliseconds(20), nextOpen: toMilliseconds(24) + toMilliseconds(7), }, { from: toMilliseconds(7), to: toMilliseconds(20), nextOpen: toMilliseconds(24) + toMilliseconds(7), }, { from: toMilliseconds(7), to: toMilliseconds(20), nextOpen: toMilliseconds(24) + toMilliseconds(7), }, { from: toMilliseconds(7), to: toMilliseconds(20), nextOpen: toMilliseconds(24) + toMilliseconds(7), }, { from: toMilliseconds(7), to: toMilliseconds(20), nextOpen: toMilliseconds(24) + toMilliseconds(24) + toMilliseconds(7), }, ] const checkOpenningTimes = date => { const nowInMs = date.getTime() const data = atomicOpeningTimes[date.getDay()] const morning = resetDate(date) const morningInMS = morning.getTime() const diff = nowInMs - morningInMS // currently closed, opening same day if (diff < data.from) { return `öffnet in ${msToString(data.from - diff)}` } // currently open if (diff > data.from && diff < data.to) { return `noch ${msToString(data.to - diff)} geöffnet` } // already closed, opening next time if(diff > data.to){ return `zur zeit geschlossen, öffnet wieder in ${msToString(data.nextOpen - diff)}` } return 'wtf...' } module.exports = checkOpenningTimes