Class: Archive::DOSTime
- Inherits:
-
Object
- Object
- Archive::DOSTime
- 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
-
#cmp(other) ⇒ Object
(also: #<=>)
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.
-
#initialize(dos_time = nil) ⇒ DOSTime
constructor
Creates a new instance of DOSTime.
-
#pack ⇒ Object
Returns the 32 bit integer that backs this object packed into a String in little endian format.
-
#to_i ⇒ Object
Returns the time value of this object as an integer representing the DOS time structure.
-
#to_time ⇒ Object
Returns a Time instance which is equivalent to the time represented by this object.
- #validate ⇒ Object private
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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/archive/support/time.rb', line 45 def initialize(dos_time = nil) case dos_time when nil @dos_time = Time.now.to_dos_time.to_i 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 validate 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.
63 64 65 |
# File 'lib/archive/support/time.rb', line 63 def cmp(other) to_i <=> other.to_i end |
#pack ⇒ Object
Returns the 32 bit integer that backs this object packed into a String in little endian format. This is suitable for use with #new.
76 77 78 |
# File 'lib/archive/support/time.rb', line 76 def pack [to_i].pack('V') end |
#to_i ⇒ Object
Returns the time value of this object as an integer representing the DOS time structure.
70 71 72 |
# File 'lib/archive/support/time.rb', line 70 def to_i @dos_time end |
#to_time ⇒ Object
Returns a Time instance which is equivalent to the time represented by this object.
82 83 84 85 86 87 88 89 90 |
# File 'lib/archive/support/time.rb', line 82 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 |
#validate ⇒ Object (private)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/archive/support/time.rb', line 94 def validate second = (0b11111 & @dos_time) 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 if second > 29 raise ArgumentError, 'second must not be greater than 29' elsif minute > 59 raise ArgumentError, 'minute must not be greater than 59' elsif hour > 24 raise ArgumentError, 'hour must not be greater than 24' elsif day < 1 raise ArgumentError, 'day must not be less than 1' elsif month < 1 raise ArgumentError, 'month must not be less than 1' elsif month > 12 raise ArgumentError, 'month must not be greater than 12' elsif year > 119 raise ArgumentError, 'year must not be greater than 119' end end |