Closed ghts closed 6 years ago
cbor spec defines 2 modes of encoding time:
By default, seconds since epoch is used. CborHandle allows you switch to RFC3339 format using the flag: TimeRFC3339=true (see https://godoc.org/github.com/ugorji/go/codec#CborHandle).
Because you inadvertently used the former model (seconds since epoch), all milliseconds etc are ignored, or a float is used which is guaranteed to not be exact. Thus the discrepancy in equals.
This is a limitation of cbor format leveraging floats, and the inability to exactly represent large fractional floats in computing.
To have a better calculation, use exact milliseconds (not nanoseconds) and compare using UTC on both sides, or use RFC 3339 format.
Closing this.
P.S. Please update if you use exact milliseconds (or 0 milliseconds) and UTC on both sides and still get a difference. I have tests that exact this code, and worked hard to get it to some semblance of correctness. So would want to ensure that this works as designed.
Please explicitly tag me (ie use @ugorji in your response) if you still see an issue, so I can re-check.
@ugorji
This is a limitation of cbor format.
When I looked the cbor code, I could not understand why using Float type for time.
Now, I am clear that it is limitation of CBOR format.
Based on your detailed explanation,
I chose to use (updated) MessagePack format, (rather than CBOR format).
Thank you for generous & detailed reply.
Cheers.~
TestCborTimeIssue.go.txt
This simple test case check the correctness of 'encoding & decoding' of time value in CBOR.
(For the source code, please refer to the attachment.)
A random time value is 'encoded & decoded' and compared with original value.
Expected 2 time values are equal, but, test result shows that it is not the case.
Test Environment:
I ran "go test github.com/ugorji/go/codec -run ^TestCborTimeIssue$"
and received output below
but expected output below.