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 =

:nodoc:

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



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

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.



22
23
24
# File 'lib/zip/extra_field/universal_time.rb', line 22

def atime
  @atime
end

#ctimeObject

Returns the value of attribute ctime.



22
23
24
# File 'lib/zip/extra_field/universal_time.rb', line 22

def ctime
  @ctime
end

#flagObject (readonly)

Returns the value of attribute flag.



22
23
24
# File 'lib/zip/extra_field/universal_time.rb', line 22

def flag
  @flag
end

#mtimeObject

Returns the value of attribute mtime.



22
23
24
# File 'lib/zip/extra_field/universal_time.rb', line 22

def mtime
  @mtime
end

Instance Method Details

#==(other) ⇒ Object



59
60
61
62
63
# File 'lib/zip/extra_field/universal_time.rb', line 59

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

#merge(binstr) ⇒ Object



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

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



73
74
75
76
77
# File 'lib/zip/extra_field/universal_time.rb', line 73

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



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

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