Re: Trouble comparing NSCalendarDates
Re: Trouble comparing NSCalendarDates
- Subject: Re: Trouble comparing NSCalendarDates
- From: Tito Ciuro <email@hidden>
- Date: Mon, 15 Nov 2004 12:31:38 +0100
On Nov 15, 2004, at 11:30, j o a r wrote:
On 2004-11-15, at 11.14, Tito Ciuro wrote:
If I call this:
returnedValue = [NSCalendarDate dateWithString: [NSString stringWithUTF8String: data]];
The proper date is returned... *however*: if you compare both dates, they're still *not* the same. It seems to me that the original date created using [NSCalendarDate date] stores sub-
second information. This extra info is not stored to disk, so when we instantiate a new NSCalendarDate object with the above statement, the sub-second information has been lost:
If you check the string that you pass to NSCalendarDate, does it contain the subsecond data that you're interested in?
If it does, perhaps you could retain the sub-second precision if you use a format string that explicitly includes sub-second precision?
I've tried storing the date with the sub-second info like this:
NSCalendarDate *today = [NSCalendarDate date];
NSString *dateStr = [today descriptionWithCalendarFormat:nil timeZone:nil locale:nil];
NSCalendarDate *newDate = [NSCalendarDate dateWithString: dateStr];
NSTimeInterval interval = [today timeIntervalSinceDate:newDate];
stringValue = [NSString stringWithFormat:@"%@:%g", dateStr, interval];
Then, I retrieve it later on and add the stored interval:
NSString *dateStr = [NSString stringWithUTF8String: data];
NSRange range = [dateStr rangeOfString:@":" options:NSBackwardsSearch];
NSCalendarDate *newDate = [NSCalendarDate dateWithString:[dateStr substringToIndex:range.location]];
[newDate addTimeInterval:[[dateStr substringFromIndex:range.location + 1]doubleValue]];
When I obtain the interval between the two dates:
NSTimeInterval interval = [today timeIntervalSinceDate:newDate];
NSLog(@"interval between the two: %f", interval);
I get (it should be zero, right?):
2004-11-15 12:19:24.817 QuickLiteTestRun[2830] interval between the two: 0.789428
I have no idea what's going on...
-- 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