for VT_FILETIME
time
should be able to be either a Time, Date, or DateTime.
# File lib/ole/types/base.rb, line 110 def self.dump time return 0.chr * SIZE unless time # convert whatever is given to be a datetime, to handle the large range case time when Date # this includes DateTime & FileTime when Time time = from_time time else raise ArgumentError, 'unknown time argument - %p' % [time] end # round to milliseconds (throwing away nanosecond precision) to # compensate for using Float-based DateTime nanoseconds = ((time - EPOCH).to_f * 864000000).round * 1000 high, low = nanoseconds.divmod 1 << 32 [low, high].pack 'V2' end
# File lib/ole/types/base.rb, line 80 def self.from_time time new(*(time.to_a[0, 6].reverse + [time.usec])) end
Create a DateTime
object from a struct FILETIME
(msdn2.microsoft.com/en-us/library/ms724284.aspx).
Converts str
to two 32 bit time values, comprising the high
and low 32 bits of the 100's of nanoseconds since 1st january 1601 (Epoch).
# File lib/ole/types/base.rb, line 97 def self.load str low, high = str.to_s.unpack 'V2' # we ignore these, without even warning about it return nil if low == 0 and high == 0 # the + 0.00001 here stinks a bit... seconds = (high * (1 << 32) + low) / 1e7 + 0.00001 obj = EPOCH + seconds / 86400 rescue return # work around home_run not preserving derived class obj = new! obj.jd + obj.day_fraction - 0.5, 0, ITALY unless FileTime === obj obj end
DateTime.new is slow... faster version for FileTime
# File lib/ole/types/base.rb, line 63 def self.new year, month, day, hour=0, min=0, sec=0, usec=0 # DateTime will remove leap and leap-leap seconds sec = 59 if sec > 59 if month <= 2 month += 12 year -= 1 end y = year + 4800 m = month - 3 jd = day + (153 * m + 2).div(5) + 365 * y + y.div(4) - y.div(100) + y.div(400) - 32045 fr = hour / 24.0 + min / 1440.0 + sec / 86400.0 # new! was actually new0 in older versions of ruby (<=1.8.4?) # see issue #4. msg = respond_to?(:new!) ? :new! : :new0 send msg, jd + fr - 0.5, 0, ITALY end
# File lib/ole/types/base.rb, line 84 def self.now from_time Time.now end
# File lib/ole/types/base.rb, line 127 def inspect "#<#{self.class} #{to_s}>" end