Re: Trouble comparing NSCalendarDates
Re: Trouble comparing NSCalendarDates
- Subject: Re: Trouble comparing NSCalendarDates
- From: j o a r <email@hidden>
- Date: Mon, 15 Nov 2004 13:29:31 +0100
On 2004-11-15, at 12.31, Tito Ciuro wrote:
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:
But you didn't answer my question - does the string that you restore
contain the sub-second information? If the information is not in the
string, it can't be restored - do you see what I'm saying?
I tried your example (simplified), and got the following results:
NSCalendarDate *calendarDate = [NSCalendarDate calendarDate];
NSString *dateStr = [calendarDate description];
NSCalendarDate *restoredCalendarDate = [NSCalendarDate dateWithString:
dateStr];
NSTimeInterval interval = [calendarDate timeIntervalSinceDate:
restoredCalendarDate];
NSLog(@"calendarDate: %@\n dateStr: %@\n restoredCalendarDate: %@\n
interval: %f", calendarDate, dateStr, restoredCalendarDate, interval);
calendarDate: 2004-11-15 13:19:11 +0100
dateStr: 2004-11-15 13:19:11 +0100
restoredCalendarDate: 2004-11-15 13:19:11 +0100
interval: 0.344116
Note that "dateStr" only have second precision. A modified example
gives "better" results:
#define C_FORMAT @"%Y-%m-%d %H:%M:%S.%F"
NSCalendarDate *calendarDate = [NSCalendarDate calendarDate];
NSString *dateStr = [calendarDate descriptionWithCalendarFormat:
C_FORMAT];
NSCalendarDate *restoredCalendarDate = [NSCalendarDate dateWithString:
dateStr calendarFormat: C_FORMAT];
NSTimeInterval interval = [calendarDate timeIntervalSinceDate:
restoredCalendarDate];
NSLog(@"calendarDate: %@\n dateStr: %@\n restoredCalendarDate: %@\n
interval: %f", calendarDate, dateStr, restoredCalendarDate, interval);
calendarDate: 2004-11-15 13:24:42 +0100
dateStr: 2004-11-15 13:24:42.328
restoredCalendarDate: 2004-11-15 13:24:42.328
interval: 0.000873
Note that the difference in time now is less than the precision
provided by the string format.
As it is a floating point value you can't rely on getting exactly zero.
j o a r
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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