master
Christian Dreier 2017-02-22 22:26:23 +01:00
commit 220406f730
5 changed files with 2139 additions and 0 deletions

2
.gitignore vendored 100644
View File

@ -0,0 +1,2 @@
node_modules
.vscode

View File

@ -0,0 +1,47 @@
const openingTimes = require('../index.js')
describe('calculating opening times', () => {
it('is early morning, waiting in front of the door', () => {
const date = new Date(2017, 1, 23, 6, 0, 0, 0)
const resultStr = openingTimes(date)
expect(resultStr).toBe('öffnet in 01:00')
})
it('is early morning, waiting in front of the door, hoping someone opens early', () => {
const date = new Date(2017, 1, 23, 6, 58, 59, 0)
const resultStr = openingTimes(date)
expect(resultStr).toBe('öffnet in 00:01')
})
it('is sunday...', () => {
const date = new Date(2017, 1, 19, 16, 0, 0, 0)
const resultStr = openingTimes(date)
expect(resultStr).toBe('zur zeit geschlossen, öffnet wieder in 15:00')
})
it('is late night friday', () => {
const date = new Date(2017, 1, 18, 23, 30, 0, 0)
const resultStr = openingTimes(date)
expect(resultStr).toBe('zur zeit geschlossen, öffnet wieder in 31:30')
})
it('should open in 9 h', () => {
const date = new Date(2017, 1, 22, 22, 0, 0, 0)
const resultStr = openingTimes(date)
expect(resultStr).toBe('zur zeit geschlossen, öffnet wieder in 09:00')
})
it('is opened', () => {
const date = new Date(2017, 1, 22, 14, 5, 0, 0)
const resultStr = openingTimes(date)
expect(resultStr).toBe('noch 05:55 geöffnet')
})
it('is still opened, but hurry up', () => {
const date = new Date(2017, 1, 22, 19, 59, 55, 0)
const resultStr = openingTimes(date)
expect(resultStr).toBe('noch 00:00 geöffnet')
})
})

92
index.js 100644
View File

@ -0,0 +1,92 @@
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

14
package.json 100644
View File

@ -0,0 +1,14 @@
{
"name": "datetest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"author": "",
"license": "ISC",
"dependencies": {
"jest": "^19.0.1"
}
}

1984
yarn.lock 100644

File diff suppressed because it is too large Load Diff