Darlyncinho Bravo Darlyncinho Bravo - 2 months ago 16
Ruby Question

Convert Time.zone.now to number OLE

I need convert the DateTime to OLE, Actually I have this code

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


but this converts to DateTime now I want this solution :

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


could you please help me ?

Answer Source

Ruby's Date class subtraction returns number of days between 2 dates, so you just need to subtract your "custom epoch" (December 30, 1899 at midnight) from the other date:

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

and that should be OLE and you can also just add it to go the other way:

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