Trouble comparing NSCalendarDates
Trouble comparing NSCalendarDates
- Subject: Trouble comparing NSCalendarDates
- From: Tito Ciuro <email@hidden>
- Date: Mon, 15 Nov 2004 09:27:07 +0100
Hello,
Working with NSCalendarDates I have found one issue where a date stored
on disk and later retrieved doesn't pass the isEqualToDate test. Here's
what I do:
1) When I save the NSCalendarDate to disk, I call [value
descriptionWithCalendarFormat:nil timeZone:nil locale:nil];
2) Later, when I need to retrieve it from disk, I call returnedValue =
[[[NSCalendarDate alloc] dateWithCalendarFormat:dateData]
timeZone:nil]autorelease];
The issue here is that if you compare both dates (the one used to store
and the one used to retrieve) with isEqualToDate, it fails! If you look
in the debugger, you see that the dates are the same, that is, printing
both values to the console shows that at least at the description
level, both dates are the same. Then why does it fail? The docs state:
isEqualToDate:
- (BOOL)isEqualToDate:(NSDate *)anotherDate
Returns YES if the two objects compared are NSDate objects and are
exactly equal to each other, NO if one of the objects is not of the
NSDate class or their date and time values differ. This method detects
subsecond differences between dates. If you want to compare dates with
a less fine granularity, either use timeIntervalSinceDate: to compare
the two dates or use NSCalendarDate objects instead.
I wanted to see if there was a subsecond difference, so I called:
2004-11-15 09:13:37.783 QuickLiteTestRun[1235] oneDate: 2004-11-15
09:13:37 +0100
2004-11-15 09:13:37.783 QuickLiteTestRun[1235] twoDate: 2004-11-15
09:13:37 +0100
2004-11-15 09:13:37.783 QuickLiteTestRun[1235] interval between the
two: 122199217.755116
So, indeed, there is a difference between the two. The question is...
what can I do about it? I tried doing this:
[[oneDate description] isEqualToString: [twoDate description]];
This comparison works and evaluates to true, so as far I can tell, this
seems the only way to make sure we can compare both dates at the
second-level. I'm not sure what the docs mean by "If you want to
compare dates with a less fine granularity, either use
timeIntervalSinceDate: to compare the two dates or use NSCalendarDate
objects instead."
Any ideas?
Thanks,
-- Tito
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden