Question

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.)

Was it helpful?

Solution

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.

OTHER TIPS

I know this is pretty old, but I just ran across it.

I'll bet that something somewhere is interpreting 010 as an octal number with the value 8. Perhaps it's a bug in the implementation of Time.parse()?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top