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.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ole/types/base.rb', line 141

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



111
112
113
# File 'lib/ole/types/base.rb', line 111

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



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ole/types/base.rb', line 128

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

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

DateTime.new is slow… faster version for FileTime



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ole/types/base.rb', line 94

def self.new year, month, day, hour=0, min=0, sec=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

.nowObject



115
116
117
# File 'lib/ole/types/base.rb', line 115

def self.now
	from_time Time.now
end

Instance Method Details

#inspectObject



158
159
160
# File 'lib/ole/types/base.rb', line 158

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