class MessagePack::Timestamp

Constants

TIMESTAMP32_MAX_SEC
TIMESTAMP64_MAX_SEC
TYPE

The timestamp extension type defined in the MessagePack spec. See github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type for details.

Attributes

nsec[R]

@return [Integer]

sec[R]

@return [Integer]

Public Class Methods

from_msgpack_ext(data) click to toggle source
# 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
new(sec, nsec) click to toggle source

@param [Integer] sec @param [Integer] nsec

# File lib/msgpack/timestamp.rb, line 24
def initialize(sec, nsec)
  @sec = sec
  @nsec = nsec
end
to_msgpack_ext(sec, nsec) click to toggle source
# 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

Public Instance Methods

==(other) click to toggle source
# File lib/msgpack/timestamp.rb, line 72
def ==(other)
  other.class == self.class && sec == other.sec && nsec == other.nsec
end
to_msgpack_ext() click to toggle source
# File lib/msgpack/timestamp.rb, line 68
def to_msgpack_ext
  self.class.to_msgpack_ext(sec, nsec)
end