Class: MessagePack::Timestamp
- Inherits:
-
Object
- Object
- MessagePack::Timestamp
- Defined in:
- lib/msgpack/timestamp.rb
Overview
a.k.a. “TimeSpec”
Constant Summary collapse
- TYPE =
The timestamp extension type defined in the MessagePack spec. See github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type for details.
-1
- TIMESTAMP32_MAX_SEC =
(1 << 32) - 1
- TIMESTAMP64_MAX_SEC =
(1 << 34) - 1
Instance Attribute Summary collapse
- #nsec ⇒ Integer readonly
- #sec ⇒ Integer readonly
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(sec, nsec) ⇒ Timestamp
constructor
A new instance of Timestamp.
- #to_msgpack_ext ⇒ Object
Constructor Details
#initialize(sec, nsec) ⇒ Timestamp
Returns a new instance of Timestamp.
24 25 26 27 |
# File 'lib/msgpack/timestamp.rb', line 24 def initialize(sec, nsec) @sec = sec @nsec = nsec end |
Instance Attribute Details
Class Method Details
.from_msgpack_ext(data) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/msgpack/timestamp.rb', line 29 def self.from_msgpack_ext(data) case data.length when 4 # timestamp32 (sec: uint32be) sec, = data.unpack('L>') new(sec, 0) when 8 # timestamp64 (nsec: uint30be, sec: uint34be) n, s = data.unpack('L>2') sec = ((n & 0b11) << 32) | s nsec = n >> 2 new(sec, nsec) when 12 # timestam96 (nsec: uint32be, sec: int64be) nsec, sec = data.unpack('L>q>') new(sec, nsec) else raise MalformedFormatError, "Invalid timestamp data size: #{data.length}" end end |
.to_msgpack_ext(sec, nsec) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/msgpack/timestamp.rb', line 50 def self.to_msgpack_ext(sec, nsec) if sec >= 0 && nsec >= 0 && sec <= TIMESTAMP64_MAX_SEC if nsec === 0 && sec <= TIMESTAMP32_MAX_SEC # timestamp32 = (sec: uint32be) [sec].pack('L>') else # timestamp64 (nsec: uint30be, sec: uint34be) nsec30 = nsec << 2 sec_high2 = sec >> 32 # high 2 bits (`x & 0b11` is redandunt) sec_low32 = sec & 0xffffffff # low 32 bits [nsec30 | sec_high2, sec_low32].pack('L>2') end else # timestamp96 (nsec: uint32be, sec: int64be) [nsec, sec].pack('L>q>') end end |
Instance Method Details
#==(other) ⇒ Object
72 73 74 |
# File 'lib/msgpack/timestamp.rb', line 72 def ==(other) other.class == self.class && sec == other.sec && nsec == other.nsec end |
#to_msgpack_ext ⇒ Object
68 69 70 |
# File 'lib/msgpack/timestamp.rb', line 68 def to_msgpack_ext self.class.to_msgpack_ext(sec, nsec) end |