Weebly/Cereal

NSDate losing accuracy

henrinormak opened this issue · 2 comments

Unclear whether this is due to a bug in Cereal of not, but encoding an NSDate and then decoding it seems to result in a slightly less accurate date, which then in turn causes isEqualToDate() to return false for the two dates. Here's a minimal test case to highlight the problem:

let date = NSDate()
let data = try! CerealEncoder.dataWithRootItem(date)
let comparison: NSDate = try! CerealDecoder.rootItemWithData(data)
XCTAssertTrue(date.isEqualToDate(comparison))

The final line is not true, while logically it should be. When logging out info about the dates, they seem correct, but there is actually a problem with the sub-second part of the dates, Cereal seems to be dropping some accuracy:

Original: 471798680.624343 After encode/decode: 471798680.62434

After investigating a bit further, I suspect these lines are to blame. It seems that timeIntervalSince1970 is not as accurate as whatever is used by the equality function (maybe timeIntervalSinceReferenceDate)

Thanks a ton for this report. I've pushed a new version, 1.2.1, which addresses this and uses timeIntervalSinceReferenceDate. Encoding will always use this new format, and decoding supports both timeIntervalSince1970 and timeIntervalSinceReferenceDate for backwards compatibility reasons.