Class: Ole::Types::FileTime

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

Overview

for VT_FILETIME

Constant Summary collapse

SIZE =
8
EPOCH =
new 1601, 1, 1

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dump(time) ⇒ Object

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ole/types/base.rb', line 108

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

.from_time(time) ⇒ Object



81
82
83
# File 'lib/ole/types/base.rb', line 81

def self.from_time time
	new(*(time.to_a[0, 6].reverse + [time.usec]))
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).



98
99
100
101
102
103
104
105
# File 'lib/ole/types/base.rb', line 98

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
	EPOCH + seconds / 86400 rescue return
end

.new(year, month, day, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object

DateTime.new is slow… faster version for FileTime



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

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) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045
	fr  = hour / 24.0 + min / 1440.0 + sec / 86400.0
	# avoid new!, as it was actually new0 in older versions of ruby (<=1.8.4?)
	# see issue #4. this is equivalent, but doesn't rely on the aliasing used
	return new!(jd + fr - 0.5, 0, ITALY)
	obj = allocate
	obj.send :initialize, jd + fr - 0.5, 0, ITALY
	obj
end

.nowObject



85
86
87
# File 'lib/ole/types/base.rb', line 85

def self.now
	from_time Time.now
end

Instance Method Details

#inspectObject



125
126
127
# File 'lib/ole/types/base.rb', line 125

def inspect
	"#<#{self.class} #{to_s}>"
end