Deal with different ways of counting weeks and weekdays in Ruby
Depending on where you live, different rules are used to determine the number of the week and a weekday. You have no chance whatsoever to get this right globally unless you make it your life’s purpose. However, when you work for clients from Europe or the US, there are two dominantish standards you should know about. Each of these has subtle differences.
- This is adhered to by most European countries.
- Weeks start on Monday and end on Sunday.
- To get the number of the weekday in Ruby as defined by ISO 8601, use Date#cwday, which returns values in
1..7(mind the range difference to Date#wday).
- The first week of the year is the week with January 4th in it. All days in this week belong to the year. This means that January 1st, 2nd and 3rd may or may not be part of the previous year’s last calendar week. Also December 29th, 30th and 31st may or may not be part of the upcoming year’s first calendar week.
- To get the calendar week in Ruby as defined by ISO 8601, use Date#cweek, which returns values in
USA and Canada
- Not defined in a standard.
- Weeks start on Sunday and end on Saturday.
- To get the number of the weekday in Ruby as used in the USA, use Date#wday, which returns values in
0..6(mind the range difference to Date#cwday)
- January 1st is part of the first week of the year. I am not sure which year December days in the same week belong to, I believe the week actually gets split into two calendar weeks in two different years (please change this note if you know).
- AFAIK there is no Ruby method to get the calendar week of a
Dateas used in the USA.
Author of this card:
- About this deck:
- We are makandra and do test-driven, agile Ruby on Rails software development.
- License for source code
License for source code
All source code included in the card Deal with different ways of counting weeks and weekdays in Ruby is licensed under the license stated below. This includes both code snippets embedded in the card text and code that is included as a file attachment. Excepted from this license are code snippets that are explicitely marked as citations from another source.
The MIT License (MIT) Copyright (c) 2011-2013 makandra GmbH Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.