# Handling week numbers according to US Standard and ISO 8601

**URL:** <https://www.ruby-forum.com/t/handling-week-numbers-according-to-us-standard-and-iso-8601/187762>\
**Category:** Ruby\
**Created:** [April 28, 2010, 10:58pm UTC](https://www.ruby-forum.com/t/handling-week-numbers-according-to-us-standard-and-iso-8601/187762 "2010-04-28T22:58:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kasper\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/48db29/32.png) [@Kasper\_T](https://www.ruby-forum.com/u/Kasper_T)\
**Post date:** [April 28, 2010, 10:58pm UTC](https://www.ruby-forum.com/t/handling-week-numbers-according-to-us-standard-and-iso-8601/187762/1 "2010-04-28T22:58:33Z")

</div>

Hi ruby-lang list,

Thank you all for reading this mail.

I am struggling to handle week numbers properly on a web platform made  
in  
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  
far.

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  
time  
zone.

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  
(and  
the rest of Europe, pretty much).

------- USA:

1. Now we are using the system in USA, but still communicating with the  
same  
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  
returns  
52, which is WRONG according to European standards.

However, executing Time.zone.parse(“2009-12-31”).strftime("%U") to get  
the  
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.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Kasper\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/48db29/32.png) [@Kasper\_T](https://www.ruby-forum.com/u/Kasper_T)\
**Post date:** [April 29, 2010, 5:54pm UTC](https://www.ruby-forum.com/t/handling-week-numbers-according-to-us-standard-and-iso-8601/187762/2 "2010-04-29T17:54:11Z")

</div>

On Wed, Apr 28, 2010 at 4:58 PM, Kasper T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## ------- DENMARK: the rest of Europe, pretty much).
> 
> So, I have Date.civil that tells me one thing - 53 - and Time.zone that  
> Kasper T.  
> removed\_email\_address@domain.invalid

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)

## [GitHub - rubyredrick/ri\_cal: New Rfc 2445 (iCalendar) gem for Ruby](http://github.com/rubyredrick/ri_cal)

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
