From 307da9658d56e2f9b512b05f9e45b99af0420ca8 Mon Sep 17 00:00:00 2001 From: Alberto Restifo Date: Fri, 24 Apr 2020 22:46:07 +0200 Subject: [PATCH] Fix fractional unix timestamps handling --- spec/cbor/from_cbor_spec.cr | 4 ++-- src/cbor/decoder.cr | 4 ++-- src/cbor/from_cbor.cr | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/cbor/from_cbor_spec.cr b/spec/cbor/from_cbor_spec.cr index fc818ca..fa81ac2 100644 --- a/spec/cbor/from_cbor_spec.cr +++ b/spec/cbor/from_cbor_spec.cr @@ -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], Time::Format::RFC_3339.parse("2013-03-21T20:04:00Z")}, {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| type, bytes, want = tt - it "decodes #{type.class}" do + it "decodes #{type}" do res = type.from_cbor(bytes) res.should eq(want) end diff --git a/src/cbor/decoder.cr b/src/cbor/decoder.cr index 7c97a13..f33bd4a 100644 --- a/src/cbor/decoder.cr +++ b/src/cbor/decoder.cr @@ -42,7 +42,7 @@ class CBOR::Decoder end def read_tag : Tag - read_type(Token::TagT) { |token| token.value } + read_type(Token::TagT, ignore_tag: false) { |token| token.value } end def read_bool : Bool @@ -64,7 +64,7 @@ class CBOR::Decoder 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 - {% if type != Token::TagT && ignore_tag %} + {% if ignore_tag %} if @current_token.is_a?(Token::TagT) finish_token! end diff --git a/src/cbor/from_cbor.cr b/src/cbor/from_cbor.cr index 7a88aee..b598786 100644 --- a/src/cbor/from_cbor.cr +++ b/src/cbor/from_cbor.cr @@ -40,7 +40,7 @@ def Time.new(decoder : CBOR::Decoder) when Int Time.unix(num) when Float - Time.unix_ms((num * 1000).to_i) + 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.to_s}")