forked from msgpack/msgpack-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.rb
More file actions
29 lines (26 loc) · 951 Bytes
/
time.rb
File metadata and controls
29 lines (26 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true
# MessagePack extention packer and unpacker for built-in Time class
module MessagePack
module Time
# 3-arg Time.at is available Ruby >= 2.5
TIME_AT_3_AVAILABLE = begin
!!::Time.at(0, 0, :nanosecond)
rescue ArgumentError
false
end
Unpacker = if TIME_AT_3_AVAILABLE
lambda do |payload|
tv = MessagePack::Timestamp.from_msgpack_ext(payload)
::Time.at(tv.sec, tv.nsec, :nanosecond)
end
else
lambda do |payload|
tv = MessagePack::Timestamp.from_msgpack_ext(payload)
::Time.at(tv.sec, tv.nsec / 1000.0r)
end
end
Packer = lambda { |time|
MessagePack::Timestamp.to_msgpack_ext(time.tv_sec, time.tv_nsec)
}
end
end