1

I have a question concerning week of the year output in ruby on rails:

I want to get the correct week number of this week. I'm located in Germany, so my week starts from Monday. Right now I am in week 42!

There are 3 different possibilities to print the week number accordingly to the documentation (APIDock):

The documentation says:

irb(main):023:0> Time.now.beginning_of_week.strftime("%Y-%U")
=> "2014-41"
irb(main):024:0> Time.now.beginning_of_week.strftime("%Y-%W")
=> "2014-41"
irb(main):025:0> Time.now.beginning_of_week.strftime("%Y-%V")
=> "2014-42"
irb(main):029:0> Time.zone
=> #<ActiveSupport::TimeZone:0x007f600a82c320 @name="Berlin", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Europe/Berlin>, @current_period=nil>

The documentation says:

ISO 8601 week-based year and week number:
The week 1 of YYYY starts with a Monday and includes YYYY-01-04.
The days in the year before the first week are in the last week of
the previous year.
  %G - The week-based year
  %g - The last 2 digits of the week-based year (00..99)
  %V - Week number of the week-based year (01..53)

Week number:
The week 1 of YYYY starts with a Sunday or Monday (according to %U
or %W).  The days in the year before the first week are in week 0.
  %U - Week number of the year.  The week starts with Sunday.  (00..53)
  %W - Week number of the year.  The week starts with Monday.  (00..53)

My Question

Why are the first (%Uand %W) outputs wrong? I do not really get the point of that!

Thanks a lot!

Sebastian
  • 1,873
  • 4
  • 25
  • 57
  • I just never use %U and %W. See same question http://stackoverflow.com/questions/20678784/ruby-on-rails-week-number-is-not-the-correct-one-1 –  Oct 14 '14 at 09:20
  • Germany (and generally most of the world) uses ISO 8601 weeks (i.e. week 1 is that week containing the first thursday of the year). Other countries use other systems. In the US, week 1 is the one with the first Sunday of the year. In Islamic countries, it's the week with the first Monday. So just pick the system you need. And always remember: dates (and time) are hard, as in *really* hard. – Holger Just Oct 14 '14 at 10:06

1 Answers1

2

Here's the difference:

  • %U - week 1 starts at 2014-01-05 (first Sunday in 2014)
  • %W - week 1 starts at 2014-01-06 (first Monday in 2014)
  • %V - week 1 starts at 2013-12-30 (first Monday on or before January 4th)
Stefan
  • 109,145
  • 14
  • 143
  • 218