将 Time.zone.now 转换为数字 OLE



我需要将日期时间转换为 OLE,实际上我有这个代码

def self.convert_time(t=42941.6102054745)
Time.at((t - 25569) * 86400).utc
end

但现在这转换为日期时间,我想要这个解决方案:

def self.date_time_ole(dt= Time.zone.now)
# her convert to number ole from datetime
end

你能帮帮我吗?

Ruby 的Date类减法返回 2 个日期之间的天数,因此您只需从另一个日期减去您的"自定义纪元"(1899 年 12 月 30 日午夜(:

ole_epoch = DateTime.new(1899, 12, 30, 0, 0, 0)
now = DateTime.now # => 2017-08-18 21:03:28 UTC
(now - ole_epoch).to_f # => 42965.87741847492

这应该是 OLE,您也可以将其添加到另一种方式:

ole_epoch + 42965.87741847492 # => Fri, 18 Aug 2017 21:03:28 +0000

最新更新