Class: Zip::ExtraField::UniversalTime

Inherits:
Generic
  • Object
show all
Defined in:
lib/zip/extra_field/universal_time.rb

Overview

Info-ZIP Additional timestamp field

Constant Summary collapse

HEADER_ID =
'UT'
ATIME_MASK =
0b010
CTIME_MASK =
0b100
MTIME_MASK =
0b001

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#initial_parse, name, register_map, #to_c_dir_bin, #to_local_bin

Constructor Details

#initialize(binstr = nil) ⇒ UniversalTime

Returns a new instance of UniversalTime.



11
12
13
14
15
16
17
18
# File 'lib/zip/extra_field/universal_time.rb', line 11

def initialize(binstr = nil)
  @ctime = nil
  @mtime = nil
  @atime = nil
  @flag  = 0

  merge(binstr) unless binstr.nil?
end

Instance Attribute Details

#atimeObject

Returns the value of attribute atime.



20
21
22
# File 'lib/zip/extra_field/universal_time.rb', line 20

def atime
  @atime
end

#ctimeObject

Returns the value of attribute ctime.



20
21
22
# File 'lib/zip/extra_field/universal_time.rb', line 20

def ctime
  @ctime
end

#flagObject (readonly)

Returns the value of attribute flag.



20
21
22
# File 'lib/zip/extra_field/universal_time.rb', line 20

def flag
  @flag
end

#mtimeObject

Returns the value of attribute mtime.



20
21
22
# File 'lib/zip/extra_field/universal_time.rb', line 20

def mtime
  @mtime
end

Instance Method Details

#==(other) ⇒ Object



57
58
59
60
61
# File 'lib/zip/extra_field/universal_time.rb', line 57

def ==(other)
  @mtime == other.mtime &&
    @atime == other.atime &&
    @ctime == other.ctime
end

#merge(binstr) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zip/extra_field/universal_time.rb', line 37

def merge(binstr)
  return if binstr.empty?

  size, content = initial_parse(binstr)
  return if !size || size <= 0

  @flag, *times = content.unpack('Cl<l<l<')

  # Parse the timestamps, in order, based on which flags are set.
  return if times[0].nil?

  @mtime ||= ::Zip::DOSTime.at(times.shift) unless @flag & MTIME_MASK == 0
  return if times[0].nil?

  @atime ||= ::Zip::DOSTime.at(times.shift) unless @flag & ATIME_MASK == 0
  return if times[0].nil?

  @ctime ||= ::Zip::DOSTime.at(times.shift) unless @flag & CTIME_MASK == 0
end

#pack_for_c_dirObject



71
72
73
74
75
# File 'lib/zip/extra_field/universal_time.rb', line 71

def pack_for_c_dir
  s = [@flag].pack('C')
  s << [@mtime.to_i].pack('l<') unless @flag & MTIME_MASK == 0
  s
end

#pack_for_localObject



63
64
65
66
67
68
69
# File 'lib/zip/extra_field/universal_time.rb', line 63

def pack_for_local
  s = [@flag].pack('C')
  s << [@mtime.to_i].pack('l<') unless @flag & MTIME_MASK == 0
  s << [@atime.to_i].pack('l<') unless @flag & ATIME_MASK == 0
  s << [@ctime.to_i].pack('l<') unless @flag & CTIME_MASK == 0
  s
end