views:

514

answers:

1

I am refactoring some code for a Ruby library. This code includes a Date parser. One of the tests was to parse this string "2008-02-20T8:05:00-010:00" which is supposed to be ISO 8601.

The previous code would actually output: "Wed Feb 20 18:05:00 UTC 2008". My new code outputs that: "Wed Feb 20 16:05:00 UTC 2008".

My question is: which one is the right one?

Time.parse in Ruby gives the second one. But again, I want to be 100% sure that the previous code AND test were buggy.

Which one is right? (By maybe parsing the string with a library in another language? - I only know Ruby.)

+2  A: 

The correct UTC time is 1805. The time group indicates 0805 in zone -10, so to get UTC add the 10 to the given time. Thus 1805. Since 1805 is less than 2400 it's the same day.

If your code is giving 1605, then you almost certainly have the timezone set incorrectly to zone -8, which happens to be Pacific Standard Time.


Aha, looks like your input format is messed up. Observe:

irb(main):003:0> Time.parse("2008-02-20T8:05:00-010:00") 
=> Wed Feb 20 08:05:00 -0700 2008

I happen to be in zone -7 so it's suiting that to my locale. But

irb(main):004:0> t=Time.parse("2008-02-20T8:05:00-010:00")
=> Wed Feb 20 08:05:00 -0700 2008
irb(main):005:0> t
=> Wed Feb 20 08:05:00 -0700 2008
irb(main):006:0> t.getutc
=> Wed Feb 20 15:05:00 UTC 2008

I'm getting an unexpected result. Now observe:

irb(main):007:0> t=Time.parse("2008-02-20T8:05:00-10:00")
=> Wed Feb 20 11:05:00 -0700 2008
irb(main):008:0> t.getutc
=> Wed Feb 20 18:05:00 UTC 2008

There's the expected result. See the difference? First example vs second:

irb(main):004:0> t=Time.parse("2008-02-20T8:05:00-010:00")
irb(main):007:0> t=Time.parse("2008-02-20T8:05:00-10:00")

I took the spurious extra 0 out (which I certainly didn't notice either) and whoosh, it works.

Charlie Martin
Yes, I am actually in PST (San Francisco). Does that mean that Ruby's Time.parse fails me?
Julien Genestoux
No, it means you are getting the time set correctly, but you're outputting it in your local timezone. Notice that 1805 in zone -10 *is* 1605 in zone -8.
Charlie Martin
well, the output is show in UTC both times:"Wed Feb 20 18:05:00 UTC 2008" "Wed Feb 20 16:05:00 UTC 2008"They can't be right both, can they?
Julien Genestoux
Try using Time#getutc and see what the Zulu time you're saving is. Also have a look to make sure you're not calling Time@local somewhere.
Charlie Martin
(Thank you for your help... )I am not sure what you mean by [email protected] yes, time.getutc.to_s actually returns "Wed Feb 20 16:05:00 UTC 2008"
Julien Genestoux
it means I can't type: I meant Time#local.
Charlie Martin
So I guess that means the test was actually wrong, since this it was not testing a valid iso8601 format, right?
Julien Genestoux
That's how it looks. If you want to actually confirm, get an earlier version of Ruby and try the same experiment. I'm guessing that a bug in the Ruby stuff was fixed — it got more strict about the ISO format — thereby revealing a misformatted string in your test.
Charlie Martin
Awesome, thanks a bunch for the great help! Have a wonderful evening!
Julien Genestoux