Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 使用DateTime.strTime时不考虑Rails夏令时_Ruby On Rails 3_Date_Timezone - Fatal编程技术网

Ruby on rails 3 使用DateTime.strTime时不考虑Rails夏令时

Ruby on rails 3 使用DateTime.strTime时不考虑Rails夏令时,ruby-on-rails-3,date,timezone,Ruby On Rails 3,Date,Timezone,我一直在分析字符串,我有一个测试用例给我带来了问题。使用strtime解析日期/时间字符串时,不考虑夏令时。据我所知,这是一个错误。我找不到关于这个bug的任何文档。下面是Rails控制台中的一个测试用例。这是ruby 1.9.3-p215和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")

我一直在分析字符串,我有一个测试用例给我带来了问题。使用strtime解析日期/时间字符串时,不考虑夏令时。据我所知,这是一个错误。我找不到关于这个bug的任何文档。下面是Rails控制台中的一个测试用例。这是ruby 1.9.3-p215和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,但是时间也会移动一个小时,因此这不是我从字符串中解析出来的。有人有没有解决这个错误的方法,或者有没有更可靠的方法将日期+时间+时区解析成一个DateTime对象,在这个对象中夏时制是正确表示的?多谢各位

编辑: 好的,我找到了一个解决方法,尽管我不确定它有多强大

以下是一个例子:

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

这会将日期/时间字符串解析为正确的时区。我不确定解析方法处理这个问题的健壮性,所以我想看看是否有更好的解决方法,但这是我目前为止的方法。

好的,这是我迄今为止发现的处理这个问题的最佳方法。我在lib文件中创建了一个实用程序方法

# Returns a DateTime object in the specified timezone
def self.parse_to_date(date_string, num_hours, timezone)
  if timezone.is_a? String
    timezone = ActiveSupport::TimeZone[timezone]
  end

  result = nil
  #Chronic.time_class = timezone # Trying out chronic time zone support - so far it doesn't work
  the_date = Chronic.parse date_string
  if the_date
    # Format the date into something that TimeZone can definitely parse
    date_string = the_date.strftime("%Y-%m-%d")
    result = timezone.parse(date_string) + num_hours.to_f.hours
  end

  result
end
请注意,我手动将小时添加到时间上,因为Chronic.parse在解析时间上没有我喜欢的那么健壮-当没有向时间添加尾随零时,它失败了,例如,在8:0而不是8:00时。

我希望这对某人有用。将日期/时间/时区字符串解析为有效日期似乎是一件非常常见的事情,但我找不到任何将这三者结合在一起的解析代码。

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

> 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)。

您可以像这样设置慢性的时区。Time\u class=ActiveSupport::Timezone['Mexico City']