Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MessagePack Time extension unpacker in Ruby
require "msgpack"
class DateTimeExtension
def self.unpack(data)
case data.size
when 4
seconds = data.unpack("N")[0]
Time.at(seconds).utc
when 8
nanoseconds, seconds =
data.unpack("B30B34").map do |bitstring|
bitstring.to_i(2)
end
Time.at(seconds, nanoseconds / 1_000).utc
when 16
nanoseconds, seconds = data.unpack("NQ")
Time.at(seconds, nanoseconds / 1_000).utc
end
end
end
MessagePack::DefaultFactory.register_type(-1, DateTimeExtension, unpacker: :unpack)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment