Time: allow to use String representations, as produced by #to_json.

remotes/dev/pr-multi
Karchnu 2020-11-26 06:51:29 +01:00
parent df25f8269f
commit ea65c8dc32
1 changed files with 16 additions and 10 deletions

View File

@ -134,18 +134,24 @@ end
# #
# [1]: https://tools.ietf.org/html/rfc7049#section-2.4.1 # [1]: https://tools.ietf.org/html/rfc7049#section-2.4.1
def Time.new(decoder : CBOR::Decoder) def Time.new(decoder : CBOR::Decoder)
case tag = decoder.read_tag # In case Time is formatted as a JSON#to_json String.
when CBOR::Tag::RFC3339Time case decoder.current_token
when CBOR::Token::StringT
Time::Format::RFC_3339.parse(decoder.read_string) Time::Format::RFC_3339.parse(decoder.read_string)
when CBOR::Tag::EpochTime
case num = decoder.read_num
when Int
Time.unix(num)
when Float
Time.unix_ms((BigFloat.new(num) * 1_000).to_u64)
end
else else
raise CBOR::ParseError.new("Expected tag to have value 0 or 1, got #{tag.value}") case tag = decoder.read_tag
when CBOR::Tag::RFC3339Time
Time::Format::RFC_3339.parse(decoder.read_string)
when CBOR::Tag::EpochTime
case num = decoder.read_num
when Int
Time.unix(num)
when Float
Time.unix_ms((BigFloat.new(num) * 1_000).to_u64)
end
else
raise CBOR::ParseError.new("Expected tag to have value 0 or 1, got #{tag.value}")
end
end end
end end