Class: DOSTime

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/archive/support/time.rb

Overview

A representation of the DOS time structure which can be converted into instances of Time.

Instance Method Summary collapse

Constructor Details

#initialize(dos_time = nil) ⇒ DOSTime

Creates a new instance of DOSTime. dos_time is a 4 byte String or unsigned number (Integer) representing an MS-DOS time structure where:

Bits 0-4

2 second increments (0-29)

Bits 5-10

minutes (0-59)

Bits 11-15

hours (0-24)

Bits 16-20

day (1-31)

Bits 21-24

month (1-12)

Bits 25-31

four digit year minus 1980 (0-119)

If dos_time is ommitted or nil, a new instance is created based on the current time.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/archive/support/time.rb', line 44

def initialize(dos_time = nil)
  case dos_time
  when nil
    @dos_time = Time.now.to_dos_time.dos_time
  when Integer
    @dos_time = dos_time
  else
    unless dos_time.length == 4 then
      raise ArgumentError, 'length of DOS time structure is not 4'
    end
    @dos_time = dos_time.unpack('V')[0]
  end
end

Instance Method Details

#cmp(other) ⇒ Object Also known as: <=>

Returns -1 if other is a time earlier than this one, 0 if other is the same time, and 1 if other is a later time.



60
61
62
# File 'lib/archive/support/time.rb', line 60

def cmp(other)
  @dos_time <=> other.dos_time
end

#to_iObject

Returns the time value of this object as an integer representing the DOS time structure.



67
68
69
# File 'lib/archive/support/time.rb', line 67

def to_i
  @dos_time
end

#to_timeObject

Returns a Time instance which is equivalent to the time represented by this object.



73
74
75
76
77
78
79
80
81
# File 'lib/archive/support/time.rb', line 73

def to_time
  second = ((0b11111         & @dos_time)      ) * 2
  minute = ((0b111111  << 5  & @dos_time) >>  5)
  hour   = ((0b11111   << 11 & @dos_time) >> 11)
  day    = ((0b11111   << 16 & @dos_time) >> 16)
  month  = ((0b1111    << 21 & @dos_time) >> 21)
  year   = ((0b1111111 << 25 & @dos_time) >> 25) + 1980
  return Time.local(year, month, day, hour, minute, second)
end