Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time.parse and DateTime.parse returns different results

Tags:

datetime

ruby

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
like image 933
Idan Wender Avatar asked Dec 16 '22 05:12

Idan Wender


1 Answers

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"
like image 149
Jakob S Avatar answered Jan 16 '23 04:01

Jakob S