function work_hours(On, Off){
var hour = new Date().getHours(),
a = Number(On),
b = Number(Off),
c = Number(hour);
if(a == b){
return {"status": true, "next_time": 0};
}
if(a < b){
if(c >= a && c < b){
return {"status": true, "next_time": 0};
}
}
if(a > b){
if(c >= a && c > b){
return {"status": true, "next_time": 0};
}
if (c <= a && c < b){
return {"status": true, "next_time": 0};
}
}
return {"status": false, "next_time": 3600000 - new Date().getTime() % 3600000};
}
function workHours(on, off, testNow = null) {
// TODO: validate input
const now = testNow ?? new Date().getHours();
const a = Number(on);
let b = Number(off);
const format = (status, nextTime) => ({ status, next_time: nextTime });
if (a === b) {
return format(true, 0);
}
if (a > b) {
b += 24;
}
if (now >= a && now < b) {
return format(true, 0);
}
if (now < a) {
return format(false, a - now);
}
return format(false, a + 24 - now);
}
// Тесты тесты
const tests = [
[9, 17, 10, { status: true, next_time: 0 }],
[9, 17, 8, { status: false, next_time: 1 }],
[9, 17, 17, { status: false, next_time: 16 }],
[19, 17, 17, { status: false, next_time: 2 }],
[19, 19, 17, { status: true, next_time: 0 }],
];
const eq = (a, b) => Object.entries(a).every(([k, v]) => b[k] === v);
tests.forEach(test => {
const [on, off, now, expected] = test;
const result = workHours(on, off, now);
const testResult = eq(result, expected);
if (testResult) {
console.log('Passed', {on, off, now, result});
}
console.assert(testResult, test);
});