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

Convert Time.zone.now to number OLE

我需要将 DateTime 转换为 OLE,实际上我有这段代码

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

但这现在转换为 DateTime 我想要这个解决方案:

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

你能帮帮我吗?

Ruby的Dateclass减returns两个日期之间的天数,所以你只需要减去你的"custom epoch"(12月30日, 1899 午夜) 来自另一个日期:

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