【发布时间】:2012-08-29 15:30:12
【问题描述】:
简单的问题,但我找不到好的或明确的答案。将 Ruby 日期和时间对象(对象,而不是字符串)组合成单个 DateTime 对象的最佳和最有效的方法是什么?
【问题讨论】:
标签: ruby
简单的问题,但我找不到好的或明确的答案。将 Ruby 日期和时间对象(对象,而不是字符串)组合成单个 DateTime 对象的最佳和最有效的方法是什么?
【问题讨论】:
标签: ruby
使用seconds_since_midnight 时,夏令时的更改可能会导致意外结果。
Time.zone = 'America/Chicago'
t = Time.zone.parse('07:00').seconds_since_midnight.seconds
d1 = Time.zone.parse('2016-11-06').to_date # Fall back
d2 = Time.zone.parse('2016-11-07').to_date # Normal day
d3 = Time.zone.parse('2017-03-12').to_date # Spring forward
d1 + t
#=> Sun, 06 Nov 2016 06:00:00 CST -06:00
d2 + t
#=> Mon, 07 Nov 2016 07:00:00 CST -06:00
d3 + t
#=> Sun, 12 Mar 2017 08:00:00 CDT -05:00
这是一个替代方案,类似于上面@selva-raj 的答案,使用字符串插值、strftime 和parse。 %F 等于 %Y-%m-%d 和 %T 等于 %H:%M:%S。
Time.zone = 'America/Chicago'
t = Time.zone.parse('07:00')
d1 = Time.zone.parse('2016-11-06').to_date # Fall back
d2 = Time.zone.parse('2016-11-07').to_date # Normal day
d3 = Time.zone.parse('2017-03-12').to_date # Spring forward
Time.zone.parse("#{d1.strftime('%F')} #{t.strftime('%T')}")
#=> Sun, 06 Nov 2016 07:00:00 CST -06:00
Time.zone.parse("#{d2.strftime('%F')} #{t.strftime('%T')}")
#=> Sun, 07 Nov 2016 07:00:00 CST -06:00
Time.zone.parse("#{d3.strftime('%F')} #{t.strftime('%T')}")
#=> Sun, 12 Mar 2017 07:00:00 CDT -05:00
【讨论】:
Time.zone.parse("#{d1.strftime('%F')} #{t.strftime('%T')}")。谢谢!
我找到了这个,但它没有你希望的那么优雅:
d = Date.new(2012, 8, 29)
t = Time.now
dt = DateTime.new(d.year, d.month, d.day, t.hour, t.min, t.sec, t.zone)
顺便说一句,ruby Time 对象还存储年、月和日,因此在创建 DateTime 时会丢弃它。
【讨论】:
Utils.datetime_from_date_and_time(date, time) 或更短的东西。您的实用程序方法将包含一个不优雅的代码,但代码的用户将拥有一个方便且易读的工具。
简单:
Date.new(2015, 2, 10).to_datetime + Time.parse("16:30").seconds_since_midnight.seconds
# => Object: Tue, 10 Feb 2015 16:30:00 +0000
你一定会爱上 Ruby!
【讨论】:
Date.new(2015, 2, 10).to_time + Time.parse("16:30").seconds_since_midnight.seconds
seconds_since_midnight 在使用时区和夏令时可能会导致意外结果。请参阅下面的答案。
如果您使用的是 Ruby on Rails,则效果很好。
我构建了一个方法来扩展 DateTime 类以组合日期和时间。 它从日期开始计算区域,这样它就不会在夏令时结束一个小时。
另外,为了方便起见,我也喜欢能够传入字符串。
class DateTime
def self.combine(d, t)
# pass in a date and time or strings
d = Date.parse(d) if d.is_a? String
t = Time.zone.parse(t) if t.is_a? String
# + 12 hours to make sure we are in the right zone
# (eg. PST and PDT switch at 2am)
zone = (Time.zone.parse(d.strftime("%Y-%m-%d")) + 12.hours ).zone
new(d.year, d.month, d.day, t.hour, t.min, t.sec, zone)
end
end
所以你可以这样做:
DateTime.combine(3.weeks.ago, "9am")
或
DateTime.combine("2015-3-26", Time.current)
等等……
【讨论】:
如果使用 Rails,请尝试以下任一方法:
d = Date.new(2014, 3, 1)
t = Time.parse("16:30")
dt = d + t.seconds_since_midnight.seconds
# => ActiveSupport::TimeWithZone
dt = (d + t.seconds_since_midnight.seconds).to_datetime
# => DateTime
dt = DateTime.new(d.year, d.month, d.day, t.hour, t.min, t.sec)
# => DateTime
【讨论】:
dt = d + t.seconds_since_midnight.seconds 解决了我遇到的问题。
我找到了另一种方法,我希望这是正确的。
datetojoin=Time.parse(datetime).strftime("%Y-%m-%d")
timetojoin=Time.parse(time).strftime("%T")
joined_datetime = Time.parse(datetojoin +" "+ timetojoin).strftime("%F %T")
有什么想法吗?请分享。
【讨论】: