如何在 Ruby 中的 DateTime 和 Time 对象之间进行转换?
7 回答
require 'time'
require 'date'
t = Time.now
d = DateTime.now
dd = DateTime.parse(t.to_s)
tt = Time.parse(d.to_s)
作为对 Ruby 生态系统状态的更新,Date
现在DateTime
有了Time
在各种类之间进行转换的方法。使用 Ruby 1.9.2+:
pry
[1] pry(main)> ts = 'Jan 1, 2000 12:01:01'
=> "Jan 1, 2000 12:01:01"
[2] pry(main)> require 'time'
=> true
[3] pry(main)> require 'date'
=> true
[4] pry(main)> ds = Date.parse(ts)
=> #<Date: 2000-01-01 (4903089/2,0,2299161)>
[5] pry(main)> ds.to_date
=> #<Date: 2000-01-01 (4903089/2,0,2299161)>
[6] pry(main)> ds.to_datetime
=> #<DateTime: 2000-01-01T00:00:00+00:00 (4903089/2,0,2299161)>
[7] pry(main)> ds.to_time
=> 2000-01-01 00:00:00 -0700
[8] pry(main)> ds.to_time.class
=> Time
[9] pry(main)> ds.to_datetime.class
=> DateTime
[10] pry(main)> ts = Time.parse(ts)
=> 2000-01-01 12:01:01 -0700
[11] pry(main)> ts.class
=> Time
[12] pry(main)> ts.to_date
=> #<Date: 2000-01-01 (4903089/2,0,2299161)>
[13] pry(main)> ts.to_date.class
=> Date
[14] pry(main)> ts.to_datetime
=> #<DateTime: 2000-01-01T12:01:01-07:00 (211813513261/86400,-7/24,2299161)>
[15] pry(main)> ts.to_datetime.class
=> DateTime
您将需要两次略有不同的转换。
要从转换 Time
为 DateTime
您可以修改 Time 类,如下所示:
require 'date'
class Time
def to_datetime
# Convert seconds + microseconds into a fractional number of seconds
seconds = sec + Rational(usec, 10**6)
# Convert a UTC offset measured in minutes to one measured in a
# fraction of a day.
offset = Rational(utc_offset, 60 * 60 * 24)
DateTime.new(year, month, day, hour, min, seconds, offset)
end
end
对 Date 的类似调整将让您转换 DateTime
为 Time
.
class Date
def to_gm_time
to_time(new_offset, :gm)
end
def to_local_time
to_time(new_offset(DateTime.now.offset-offset), :local)
end
private
def to_time(dest, method)
#Convert a fraction of a day to a number of microseconds
usec = (dest.sec_fraction * 60 * 60 * 24 * (10**6)).to_i
Time.send(method, dest.year, dest.month, dest.day, dest.hour, dest.min,
dest.sec, usec)
end
end
请注意,您必须在本地时间和 GM/UTC 时间之间进行选择。
上述两个代码片段均取自 O'Reilly 的Ruby Cookbook。他们的代码重用政策允许这样做。
不幸的是,DateTime.to_time, Time.to_datetime
andTime.parse
函数不保留时区信息。转换期间所有内容都转换为本地时区。日期算术仍然有效,但您将无法显示带有原始时区的日期。上下文信息通常很重要。例如,如果我想查看在纽约的营业时间内执行的交易,我可能更愿意看到它们显示在原来的时区,而不是我在澳大利亚的本地时区(比纽约早 12 小时)。
下面的转换方法确实保留了该 tz 信息。
对于 Ruby 1.8,请查看Gordon Wilson 的回答。它来自可靠的 Ruby Cookbook。
对于 Ruby 1.9,它稍微容易一些。
require 'date'
# Create a date in some foreign time zone (middle of the Atlantic)
d = DateTime.new(2010,01,01, 10,00,00, Rational(-2, 24))
puts d
# Convert DateTime to Time, keeping the original timezone
t = Time.new(d.year, d.month, d.day, d.hour, d.min, d.sec, d.zone)
puts t
# Convert Time to DateTime, keeping the original timezone
d = DateTime.new(t.year, t.month, t.day, t.hour, t.min, t.sec, Rational(t.gmt_offset / 3600, 24))
puts d
这将打印以下内容
2010-01-01T10:00:00-02:00
2010-01-01 10:00:00 -0200
2010-01-01T10:00:00-02:00
保留完整的原始日期时间信息,包括时区。
改进 Gordon Wilson 解决方案,这是我的尝试:
def to_time
#Convert a fraction of a day to a number of microseconds
usec = (sec_fraction * 60 * 60 * 24 * (10**6)).to_i
t = Time.gm(year, month, day, hour, min, sec, usec)
t - offset.abs.div(SECONDS_IN_DAY)
end
您将在 UTC 中获得相同的时间,失去时区(不幸的是)
另外,如果你有 ruby 1.9,试试这个to_time
方法
在进行此类转换时,应考虑从一个对象转换到另一个对象时的时区行为。我在这个 stackoverflow帖子中找到了一些很好的注释和示例。
您可以使用to_date
,例如
> Event.last.starts_at
=> Wed, 13 Jan 2021 16:49:36.292979000 CET +01:00
> Event.last.starts_at.to_date
=> Wed, 13 Jan 2021