/
universal_time.rb
79 lines (65 loc) · 1.77 KB
/
universal_time.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module Zip
# Info-ZIP Additional timestamp field
class ExtraField::UniversalTime < ExtraField::Generic
HEADER_ID = 'UT'
register_map
def initialize(binstr = nil)
@ctime = nil
@mtime = nil
@atime = nil
@flag = 0
merge(binstr) unless binstr.nil?
end
attr_reader :atime, :ctime, :mtime, :flag
def atime=(time)
time.nil? ? @flag &= 0b101 : @flag |= 0b010
@atime = time
end
def ctime=(time)
time.nil? ? @flag &= 0b011 : @flag |= 0b100
@ctime = time
end
def mtime=(time)
time.nil? ? @flag &= 0b110 : @flag |= 0b001
@mtime = time
end
def merge(binstr)
return if binstr.empty?
size, content = initial_parse(binstr)
return if size.nil? || size <= 0
flags, *times = content.unpack('Cl<l<l<')
@flag = flags
# Parse the timestamps, in order, based on which flag is set.
times.each do |time|
break if time.nil? || flags == 0
if flags & 1 != 0
flags &= 0b110
@mtime ||= ::Zip::DOSTime.at(time)
elsif flags & 2 != 0
flags &= 0b101
@atime ||= ::Zip::DOSTime.at(time)
elsif flags & 4 != 0
flags &= 0b011
@ctime ||= ::Zip::DOSTime.at(time)
end
end
end
def ==(other)
@mtime == other.mtime &&
@atime == other.atime &&
@ctime == other.ctime
end
def pack_for_local
s = [@flag].pack('C')
s << [@mtime.to_i].pack('l<') unless @flag & 1 == 0
s << [@atime.to_i].pack('l<') unless @flag & 2 == 0
s << [@ctime.to_i].pack('l<') unless @flag & 4 == 0
s
end
def pack_for_c_dir
s = [@flag].pack('C')
s << [@mtime.to_i].pack('l<') unless @flag & 1 == 0
s
end
end
end