views:

462

answers:

1

hey!

we are currently working on a rails project that uses i18n and we have small problem with the dates:

# on my mac
"10.06.2008".to_date 
# produces => Tue, 10 Jun 2008

# on a friends mac and on the production server
"10.06.2008".to_date
# produces => ..., 06 Oct 2008

As you can see the dates aren't right. We checked everything. The locale.yml (de.yml) is fine on both systems and the default_locale is set, too. The only difference between our systems is the ruby version. I'm running ruby 1.8.7 and he is running 1.8.6. Can this be the reason for this behaviour?

Any help would be appreciated.

+8  A: 

Yes, the Ruby version is the difference. String#to_date calls Date._parse internally, and that returns different results for Ruby 1.8.6 and 1.8.7.

1.8.7:

require "date"
Date._parse("10.06.2008")
#=> {:year=>2008, :mday=>10, :mon=>6}

1.8.6:

Date._parse("10.06.2008")
#=> {:mday=>6, :mon=>10, :year=>2008}

The change log for 1.8.7 mentions a change to the way dates are being parsed:

Date.parse: '##.##.##' (where each '#' is a digit) is now taken as 'YY.MM.DD' instead of 'MM.DD.YY'. While the change may confuse you, you can always use Date.strptime() when you know what you are dealing with.

Even though the four-digit version from your example is not explicitly mentioned, the same change probably causes the difference. The workaround would be to use Date.strptime.

molf
Very thorough, with both a clear explanation and a clear suggestion. Well done, molf. +1
James A. Rosen