Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compare() func return wrong result comparing using .nanosecond as granularity #346

Closed
carmelogallo opened this issue Nov 29, 2016 · 4 comments
Assignees
Labels
Milestone

Comments

@carmelogallo
Copy link

As the title, I'm trying the follow code:
date_1.isAfter(NSCalendarUnit.Nanosecond, ofDate: date_2)

between the following dates:
2016-11-18T16:20:12.311Z - 2016-11-18T16:20:12.212Z

I got a false such as result. Any clue?

malcommac added a commit that referenced this issue Nov 30, 2016
@malcommac
Copy link
Owner

Just for completeness that's the code for SwiftDate:

let date1 = try! "2016-11-18T16:20:12.311Z".date(format: .iso8601(options: .withInternetDateTimeExtended))
let date2 = try! "2016-11-18T16:20:12.212Z".date(format: .iso8601(options: .withInternetDateTimeExtended))
let is1After2 = date1.isAfter(date: date2, granularity: .nanosecond) // return false
(lldb) po date.absoluteDate
▿ 2016-11-18 16:20:12 +0000
  - timeIntervalSinceReferenceDate : 501178812.21199989


(lldb) po self.absoluteDate
▿ 2016-11-18 16:20:12 +0000
  - timeIntervalSinceReferenceDate : 501178812.31100011

According to NSDate documentation:
"[NSDate] ... makes possible a wide and fine-grained range of date and time values, giving precision within milliseconds for dates 10,000 years apart [...]"

So I think it's a rounding error (even if comparing two dates using Calendar and nanosecond seems a bit weird).
I've just added a workaround (planned for 4.0.8) you can found here:
7edca03

@malcommac malcommac added the bug label Nov 30, 2016
@malcommac malcommac added this to the 4.0.8 milestone Nov 30, 2016
@malcommac malcommac self-assigned this Nov 30, 2016
@malcommac malcommac changed the title isAfter(_:, _:, _:) give wrong result if the difference is only in the milliseconds compare() func return wrong result comparing using .nanosecond as granularity Nov 30, 2016
@carmelogallo
Copy link
Author

Cool! Thanks man!

@carmelogallo
Copy link
Author

BTW, do you know already the date for 4.0.8?

@malcommac
Copy link
Owner

If it's all okay I plan to release it later tomorrow (dec. 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants