简体   繁体   中英

Ruby date parse inconsistency

I can't understand why Date.utc is making this difference:

Please not that the date is different:

1.9.3-p327 :009 > time = Date.parse("2013-07-04 15:39:45 -0700").to_time
 => 2013-07-04 00:00:00 +0300    

and:

1.9.3-p327 :010 > time = Date.parse("2013-07-04 15:39:45 -0700").to_time.utc
 => 2013-07-03 21:00:00 UTC 

UPDATE: Even if I provide the UTC within the date string, still 4th of July becomes 3rd.

1.9.3-p327 :017 > time = Date.parse("2013-07-04 1:00:00 UTC").to_time.utc
 => 2013-07-03 21:00:00 UTC 
1.9.3-p327 :018 > time = Date.parse("2013-07-04 21:00:00 UTC").to_time.utc
 => 2013-07-03 21:00:00 UTC 
1.9.3-p327 :019 > time = Date.parse("2013-07-04 23:00:00 UTC").to_time.utc
 => 2013-07-03 21:00:00 UTC 

My guess is this is because Date.parse doesn't accept an indication about UTC as a parameter. How would you make it work? Thanks.

Can you please try DateTime.parse("2013-07-04 1:00:00 UTC").to_time.utc

I got

irb(main):003:0>require 'date'
irb(main):005:0> DateTime.parse("2013-07-04 1:00:00 UTC").to_time.utc
=> 2013-07-04 01:00:00 UTC

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM