Handling week numbers according to US Standard and ISO 8601

Hi ruby-lang list,

Thank you all for reading this mail.

I am struggling to handle week numbers properly on a web platform made
Ruby that is used across several countries. I have googled this for days
trying to find a solution, but I have not had any success whatsoever so

The problem is the following:

------- DENMARK:

  1. Say we are using the system in Denmark and we want to know the week
    number of the date 2009-12-31.

  2. Denmark follows the ISO 8601 standard for calendar handling, and the
    server running the system is initialized using the Europe/Copenhagen

  3. Executing Date.civil(2009, 12, 31).cweek returns “53”, which is the
    correct answer, since December 31st 2009 is part of week 53 in Denmark
    the rest of Europe, pretty much).

------- USA:

  1. Now we are using the system in USA, but still communicating with the
    server here in Denmark, the UI itself just speaks English.

  2. When I execute Date.civil(2009, 12, 31).cweek, it still - obviously -
    returns “53”. But in the case of USA, this is wrong, since December 31st
    2009 would be week 52 according to the US Standard.

Now, I have tried to use TZInfo for Ruby, doing a
Time.zone.parse(“2009-12-31”).strftime("%W") to get the week number of
December 31st 2009 - with weeks beginning on Monday (ISO 8601). This
52, which is WRONG according to European standards.

However, executing Time.zone.parse(“2009-12-31”).strftime("%U") to get
week number of December 31st 2009 - with weeks beginning on Sunday (US
Standard) - returns 52. Which is right.

So, I have Date.civil that tells me one thing - 53 - and Time.zone that
tells me another - 52. Either is wrong in one of the cases.

Do you guys have any ideas regarding handling the week numbers correctly
according to the country of the user? Is there a way to tell
Date.civil() to
follow a specific standard, or does it just inherit from the machine
settings no matter what? What can I do to fix this?

Thank you for your help.

All the best,
Kasper T.
[email protected]

On Wed, Apr 28, 2010 at 4:58 PM, Kasper T. [email protected]

------- DENMARK:
the rest of Europe, pretty much).

So, I have Date.civil that tells me one thing - 53 - and Time.zone that
Kasper T.
[email protected]

My ri_cal gem (whose purpose is to support rfc2445/5545 icalendar
data) extends Time and DateTime with this method

    # return the iso year of the receiver
    # == parameter
    # wkst:: an integer representing the day of the week on which

weeks are deemed to start. This uses
# the ruby convention where 0 represents Sunday.
def iso_year(wkst)


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs