Yes, someone actually did this and I found it running on our server
weekend = dayOfWeek > 5
Sunday is 1 0 and Saturday is 7 6. You just made Friday part of the weekend You forgot Sunday
I was off by one, but Sunday is 0 in javascript
Both Monday and Sunday are used as the first day of the week with quite some regularity. It’s a completely arbitrary standard no different to "the tenth month is the one called “October”. Or dividing a day into 24 segments which are each broken into 60 smaller segments of 60 even smaller segments. You can’t say either is “wrong” per se.
Personally, I was brought up learning Sunday is the first day of the week, but at some point decided that was bullshit partly because it’s the week end. But also just from a practical standpoint when looking at a calendar, it’s useful to have the weekend days grouped together.
Why would you call it weekend and the start the week with half of it?!
Honestly the first one is the only one that works when people define the first day of the week differently. On the other hand, it does make you wonder. If Sunday is the first day of the week (as it is in many places) then how is it also part of the weekend?
But if you’re worried about locale, you can’t assume people use the string “Saturday” to describe Saturday either. That solution only works in English.
Depending on whether this code is in a hotpath (and considering how “elementary” it is, I figure that’s a possibility), this could very well be a significant speed improvement.
Though I’d say that only excuses it if it’s truly an elementary function (and not one line as part of a larger function), as otherwise it’s unreadable garbage. But on its own it:
- has a clear purpose
- (presumably) isn’t reimplementing functionality
- is easily tested
- can be modified with no side effects (besides breaking your calendar, but that’s beside the point)
weekend = day_of_week in (“sat”, “sun”)
As a bonus this completely sidesteps the issue of what day is 0 or 1.