Today is the 14.6.2007. If you enter "today" or "heute" into a NSTextField that is formatted with @"%d.%h.%Y you get a NSCalendarDate object with the time of noon today. If you enter 14.6.2007 you get a date for midnight.
These two aren't equal.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool;
NSDateFormatter *formatter;
NSString *today;
NSCalendarDate *date1;
NSCalendarDate *date2;
pool = [NSAutoreleasePool new];
formatter = [[[NSDateFormatter alloc] initWithDateFormat:@"%d.%m.%Y"
allowNaturalLanguage:YES] autorelease];
today = [formatter stringFromDate:[NSCalendarDate date]];
date1 = [formatter dateFromString:today];
date2 = [formatter dateFromString:@"today"];
NSLog( @"A1: %@ (%d:%d)", date1, [date1 hourOfDay], [date1 minuteOfHour]);
NSLog( @"A2: %@ (%d:%d)", date2, [date2 hourOfDay], [date2 minuteOfHour]);
[pool release];
return 0;
}
Output:
2007-06-14 16:34:19.460 CalendarFormatterTest[10708] A1: 14.06.2007 (0:0) 2007-06-14 16:34:19.461 CalendarFormatterTest[10708] A2: 2007-06-14 12:00:00 +0200 (12:0)Why does that faze me ? Because I replaced the formatters in a few windows, where the hour and minute information should not be shown anymore. Now suddenly my customers don't find their records in the database anymore.
The solution for me was to write a subclass of NSDateFormatter that posed as it and hacks the dates to 12:00, if the formatter is indeed @"%d.%m.%Y".
I read on a mailing list, that poseAs: isn't supported anymore in Objective-C 2.0. The tendency to make language more noob friendly, but less powerful is not something I like to see.
Comments (2)
Don't fret. While we lose poseAs:, we get a better replacement in ObjC 2. I just googled for it again and found nothing, so Apple's doing a good job keeping it under wraps until 10.5 is out :-)
Posted by rentzsch | June 28, 2007 8:18 PM
Posted on June 28, 2007 20:18
Sounds good, though I wonder if better for you will also mean better for me. :)
Posted by Nat! | June 29, 2007 6:09 PM
Posted on June 29, 2007 18:09