Fix fractional unix timestamps handling

dev
Alberto Restifo 2020-04-24 22:46:07 +02:00
parent f4ad61665c
commit 307da9658d
3 changed files with 5 additions and 5 deletions

View File

@ -16,13 +16,13 @@ describe "CBOR helpers on basic types" do
Bytes[0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33, 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x30, 0x5a], Bytes[0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33, 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x30, 0x5a],
Time::Format::RFC_3339.parse("2013-03-21T20:04:00Z")}, Time::Format::RFC_3339.parse("2013-03-21T20:04:00Z")},
{Time, Bytes[0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0], Time.unix(1363896240)}, {Time, Bytes[0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0], Time.unix(1363896240)},
{Time, Bytes[0xc1, 0xfb, 0x41, 0xd4, 0x52, 0xd9, 0xec, 0x20, 0x00, 0x00], Time.unix_ms((1363896240.5 * 1000).to_i)}, {Time, Bytes[0xc1, 0xfb, 0x41, 0xd4, 0x52, 0xd9, 0xec, 0x20, 0x00, 0x00], Time.unix_ms((BigFloat.new(1363896240.5) * 1000).to_u64)},
] ]
tests.each do |tt| tests.each do |tt|
type, bytes, want = tt type, bytes, want = tt
it "decodes #{type.class}" do it "decodes #{type}" do
res = type.from_cbor(bytes) res = type.from_cbor(bytes)
res.should eq(want) res.should eq(want)
end end

View File

@ -42,7 +42,7 @@ class CBOR::Decoder
end end
def read_tag : Tag def read_tag : Tag
read_type(Token::TagT) { |token| token.value } read_type(Token::TagT, ignore_tag: false) { |token| token.value }
end end
def read_bool : Bool def read_bool : Bool
@ -64,7 +64,7 @@ class CBOR::Decoder
private macro read_type(type, finish_token = true, ignore_tag = true, &block) private macro read_type(type, finish_token = true, ignore_tag = true, &block)
# Skip the tag unless the token we want to read is a tag # Skip the tag unless the token we want to read is a tag
{% if type != Token::TagT && ignore_tag %} {% if ignore_tag %}
if @current_token.is_a?(Token::TagT) if @current_token.is_a?(Token::TagT)
finish_token! finish_token!
end end

View File

@ -40,7 +40,7 @@ def Time.new(decoder : CBOR::Decoder)
when Int when Int
Time.unix(num) Time.unix(num)
when Float when Float
Time.unix_ms((num * 1000).to_i) Time.unix_ms((BigFloat.new(num) * 1_000).to_u64)
end end
else else
raise CBOR::ParseError.new("Expected tag to have value 0 or 1, got #{tag.value.to_s}") raise CBOR::ParseError.new("Expected tag to have value 0 or 1, got #{tag.value.to_s}")