Why is this iso8601 date parsing with an offset of +2?

737 Views Asked by At

I have the following JSON:

{"dtime" : "2020-08-26T15:30:00+03:00"}

Parsing it, yields the following date:

=> 2020-08-26 14:30:00 +0200

Why does it have a +2 offset and not a +3 one?

EDIT: I'm using Rails 3.2.13, with ActiveSupport 3.2.13. I'm trying to parse the string by using:

Time.parse(string)

I've noticed, however, that if I parse it with:

DateTime.parse(string)

I get a correct offset.

1

There are 1 best solutions below

0
On

I have no business answering this question, because I don't use Ruby. That said, just looking at the standard library, it seems the reason is Time.parse includes a call to localtime, which converts the parse result into your local time zone.

I suspect you're actually executing this code in GMT +02. You can check with Time.new. I'm running in GMT -07.

>> Time.new
=> 2014-05-16 18:51:00 -0700

You can convert the result into any time zone by passing it to localtime.

>> require 'Time'
=> true
>> t = Time.parse("2020-08-26T15:30:00+03:00")
=> 2020-08-26 05:30:00 -0700
>> t.localtime("+03:00")
=> 2020-08-26 15:30:00 +0300