I have some JS and my issue is that it now just returns a time out. Please see my code below
I am not sure where I went wrong with this?
exports.handler = function(context, event, callback) {
// With timezone:
// In Functions/Configure, add NPM name: moment-timezone, version: 0.5.14
// Timezone function reference: https://momentjs.com/timezone/
let moment = require('moment-timezone');
//
// timezone needed for Daylight Saving Time adjustment
let timezone = event.timezone || 'America/New_York';
console.log(" timezone: " timezone);
//
const schedule = new Map([
[ 0, {open: [00,00], close: [00,00]} ],
[ 1, {open: [08,30], close: [17,00]} ],
[ 2, {open: [08,30], close: [17,00]} ],
[ 3, {open: [08,30], close: [17,00]} ],
[ 4, {open: [08,30], close: [17,00]} ],
[ 5, {open: [08,30], close: [17,00]} ],
[ 6, {open: [00,00], close: [00,00]} ],
])
function isOpen(weekday, hour, minute) {
const {open, close} = schedule.get(weekday)
// opening hour
if (hour == open[0])
return minute >= open[1]
// closing hour
if (hour == close[0])
return minute < close[1]
// any minute of any open hour
return hour > open[0] && hour < close[0]
}
};
CodePudding user response:
.format returns a String and you are comparing the results to Numbers. Relying on type coercion can be the source of many hard-to-find bugs. I would suggest use of .hour(), .minute(), etc, which returns a Number. Read Get/Set Docs for more info.
Another thing I would recommend is reference a single moment -
const m = moment().tz(timezone)
const hour = m.hour()
const minute = m.minute()
// ...
Repeated calls to moment().tz(...) is a code smell and can actually cause bugs if your time code depends on millisecond values.
You also (probably) have a bug with your condition -
hour >= 08 && minutes > 30 && hour < 17
| hour | minute | status |
|---|---|---|
| 8 | 29 | closed |
| 8 | 30 | closed |
| 8 | 31 | open |
| 8 | 59 | open |
| 9 | 00 | closed |
| 9 | 20 | closed |
| 9 | 30 | closed |
| 9 | 31 | open |
| ... | 20 | closed |
| ... | 30 | closed |
| ... | 31 | open |
| ... | 59 | open |
| ... | 00 | closed |
You may also consider a schedule and an isOpen function. The function takes the current weekday, and the current hour and minute. This approach also allows you to have different hours on different days, without making the isOpen function more complicated -
const schedule = new Map([
[ 0, {open: [00,00], close: [00,00]} ],
[ 1, {open: [08,30], close: [17,00]} ],
[ 2, {open: [08,30], close: [17,00]} ],
[ 3, {open: [08,30], close: [17,00]} ],
[ 4, {open: [08,30], close: [17,00]} ],
[ 5, {open: [08,30], close: [17,00]} ],
[ 6, {open: [00,00], close: [00,00]} ],
])
function isOpen(weekday, hour, minute) {
const {open, close} = schedule.get(weekday)
// opening hour
if (hour == open[0])
return minute >= open[1]
// closing hour
if (hour == close[0])
return minute < close[1]
// any minute of any open hour
return hour > open[0] && hour < close[0]
}
console.log("Sun @ 08:30", isOpen(0, 08, 30))
console.log("Mon @ 08:00", isOpen(1, 08, 00))
console.log("Mon @ 08:30", isOpen(1, 08, 30))
console.log("Mon @ 09:00", isOpen(1, 09, 00))
console.log("Mon @ 16:59", isOpen(1, 16, 59))
console.log("Mon @ 17:00", isOpen(1, 17, 00))
console.log("Tue @ 12:00", isOpen(2, 12, 00))
console.log("Wed @ 04:00", isOpen(3, 04, 00))
console.log("Thu @ 14:00", isOpen(4, 14, 00))
console.log("Fri @ 15:45", isOpen(5, 15, 45))
console.log("Sat @ 12:00", isOpen(6, 12, 00))
.as-console-wrapper {min-height: 100% !important; top: 0}
Sun @ 08:30 false
Mon @ 08:00 false
Mon @ 08:30 true
Mon @ 09:00 true
Mon @ 16:59 true
Mon @ 17:00 false
Tue @ 12:00 true
Wed @ 04:00 false
Thu @ 14:00 true
Fri @ 15:45 true
Sat @ 12:00 false
CodePudding user response:
The problem is that you check for the minutes to be always greater than 30. Thus 9:40 is open but 9:20 is closed
Try this
if (( ((hour>=8 && minutes>=30)|| (hour >= 9)) && hour < 17) && (dayOfWeek >= 1 && dayOfWeek <= 5)) {
// "open" from 8:30am to 5pm, EST.
response = "open";
} else {
response = "closed";
}
theResponse = response " : " hour " " dayOfWeek;
console.log(" Time request: " theResponse);
