使用DateTime.strptime时,Rails夏令时不考虑 [英] Rails daylight savings time not accounted for when using DateTime.strptime

查看:164
本文介绍了使用DateTime.strptime时,Rails夏令时不考虑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在解析字符串,我有一个测试用例给我带来了问题。当用strptime解析日期/时间字符串时,不考虑夏令时。据我所知,这是一个bug。我找不到任何这个bug的文档。这是Rails控制台中的一个测试用例。这是ruby 1.9.3-p215和Rails 3.2.2。

I've been working on parsing strings and I have a test case that has been causing problems for me. When parsing a date/time string with strptime, Daylight Savings Time is NOT accounted for. This is a bug as far as I can tell. I can't find any docs on this bug. Here is a test case in the Rails console. This is ruby 1.9.3-p215 and Rails 3.2.2.

1.9.3-p125 :049 >   dt = DateTime.strptime("2012-04-15 10:00 Central Time (US & Canada)", "%Y-%m-%d %H:%M %Z")  
=> Sun, 15 Apr 2012 10:00:00 -0600  
1.9.3-p125 :050 > dt = DateTime.strptime("2012-04-15 10:00 Central Time (US & Canada)", "%Y-%m-%d %H:%M %Z").utc  
=> Sun, 15 Apr 2012 16:00:00 +0000  
1.9.3-p125 :051 > dt = DateTime.strptime("2012-04-15 10:00 Central Time (US & Canada)", "%Y-%m-%d %H:%M %Z").utc.in_time_zone("Central Time (US & Canada)")  
=> Sun, 15 Apr 2012 11:00:00 CDT -05:00  

如你所见,我必须转换为utc然后返回到时区以使DST正确解释,但是时间也改变了一个小时,所以这不是我从字符串中解析出来的。有人有这个错误的解决方法或更强大的方式将日期+时间+时区可靠地分析到日期时间对象,其中夏令时被正确地表示?谢谢。

As you can see, I have to convert to utc and then back to the timezone to get DST to be properly interpreted, but then the time is shifted one hour as well, so it's not what I parsed out of the string. Does someone have a workaround to this bug or a more robust way of parsing a date + time + timezone reliably into a DateTime object where daylight savings time is properly represented? Thank you.

编辑:
好​​的,我找到了一个解决方法,虽然我不知道它的强大程度。

Ok, I found a workaround, although I'm not sure how robust it is.

这是一个例子:

ActiveSupport::TimeZone["Central Time (US & Canada)"].parse "2012-04-15 10:00"  

将日期/时间字符串解析为正确时区。我不知道解析方法有多强大的处理方式,所以我想看看是否有更好的解决方法,但这是我迄今为止的方法。

This parses the date/time string into the correct timezone. I'm not sure how robust the parse method is for handling this so I'd like to see if there is a better workaround, but this is my method so far.

推荐答案

这是一个令人沮丧的问题。您要查找的Rails方法是 Time.zone.parse 。首先使用 DateTime.strptime 来解析字符串,然后通过 Time.zone.parse 运行它来设置区域。查看以下控制台输出:

This is a frustrating problem. The Rails method you're looking for is Time.zone.parse. First use DateTime.strptime to parse the string, then run it through Time.zone.parse to set the zone. Check out the following console output:

> Time.zone
 => (GMT-06:00) Central Time (US & Canada) 
> input_string = "10/12/12 00:00"
> input_format = "%m/%d/%y %H:%M"
> date_with_wrong_zone = DateTime.strptime(input_string, input_format)
 => Fri, 12 Oct 2012 00:00:00 +0000 
> correct_date = Time.zone.parse(date_with_wrong_zone.strftime('%Y-%m-%d %H:%M:%S'))
 => Fri, 12 Oct 2012 00:00:00 CDT -05:00 

请注意,即使 Time.zone 的偏移量为-6(CST),最终结果的偏移量为-5(CDT)。

Notice that even though Time.zone's offset is -6 (CST), the end result's offset is -5 (CDT).

这篇关于使用DateTime.strptime时,Rails夏令时不考虑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆