Created
May 20, 2019 09:03
-
-
Save qcam/f4ac9d0e58409940669bd0d87a383d78 to your computer and use it in GitHub Desktop.
MessagePack Time extension unpacker in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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