Reputation: 3764
Here is the code, that brought me some problems:
NSDate * d1 = [NSDate dateWithTimeIntervalSinceReferenceDate:444555883.659000];
NSTimeInterval since1970 = [d1 timeIntervalSince1970];
NSDate * d2 = [NSDate dateWithTimeIntervalSince1970:since1970];
NSLog(@"%@\n%@", d1, d2);
NSLog(@"%d", [d1 compare: d2] == NSOrderedSame);
The problem I can't understand is that the test fails with following logs:
2015-02-02 07:44:43 +0000
2015-02-02 07:44:43 +0000
0 // test failed
What can be the reason of test fails?
Upvotes: 0
Views: 143
Reputation: 5409
You're comparing double's, so you can't simply check for equality. The fact that both instances print out the same output doesn't mean anything because the precision of the default output format is 1 second.
Compare them by doing something like this:
if (fabs(t1 - t2) < delta)
where delta is very small amount such as 0.005
Or you can set the higher precision components of the date you don't need to 0 using NSDateComponents
before comparing them
Upvotes: 0
Reputation: 47729
if (interval1 + delta > interval2 && interval1 - delta < interval2)
(You can do it directly with NSDates by adding/subtracting delta on the dates, but it's messier and I'm too lazy to write it out.)
Upvotes: 1