Class: Ole::Types::FileTime

Inherits:
DateTime
  • Object
show all
Defined in:
lib/ole/types.rb

Overview

for VT_FILETIME

Constant Summary collapse

SIZE =
8
EPOCH =
new 1601, 1, 1

Class Method Summary collapse

Class Method Details

.dump(time) ⇒ Object

time should be able to be either a Time, Date, or DateTime.



80
81
82
83
84
85
86
87
88
89
# File 'lib/ole/types.rb', line 80

def self.dump time
	# i think i'll convert whatever i get to be a datetime, because of
	# the covered range.
	return 0.chr * SIZE unless time
	time = time.send(:to_datetime) if Time === time
	# don't bother to use const_get here
	bignum = (time - EPOCH) * 86400 * 1e7.to_i
	high, low = bignum.divmod 1 << 32
	[low, high].pack 'V2'
end

.load(str) ⇒ Object

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).



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ole/types.rb', line 63

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
	# switched to rational, and fixed the off by 1 second error i sometimes got.
	# time = EPOCH + (high * (1 << 32) + low) / 1e7 / 86400 rescue return
	# use const_get to ensure we can return anything which subclasses this (VT_DATE?)
	const_get('EPOCH') + Rational(high * (1 << 32) + low, 1e7.to_i * 86400) rescue return
	# extra sanity check...
	#unless (1800...2100) === time.year
	#	Log.warn "ignoring unlikely time value #{time.to_s}"
	#	return nil
	#end
	#time
end