Method: MessagePack::Timestamp.to_msgpack_ext

Defined in:
lib/msgpack/timestamp.rb

.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