Idan Wender Idan Wender - 5 months ago 10
Ruby Question

Time.parse and DateTime.parse returns different results

Why do these two

parse
statements return different results?

time = "13:30:0"

DateTime.parse(time).to_time.utc
#=> 2013-10-13 13:30:00 UTC

Time.parse(time).utc
#=> 2013-10-13 11:30:00 UTC

Answer

There is no timezone information in the input String. DateTime.parse therefore assumes UTC. Time.parse assumes local time, and I guess you're in UTC+2.

>> time = "13:30:0"
=> "13:30:0"
>> DateTime.parse(time).to_s
=> "2013-10-13T13:30:00+00:00"
>> Time.parse(time).to_s
=> "2013-10-13 13:30:00 +0200"
Comments