NSDateFormatter problem with %p
NSDateFormatter problem with %p
- Subject: NSDateFormatter problem with %p
- From: Rainer Brockerhoff <email@hidden>
- Date: Sat, 27 Jul 2002 19:43:43 -0300
Hi folks.
I've run into some problems with NSDateFormatter which have been mentioned a few times here on the list, but I couldn't find a posted solution anywhere.
I need to format an editable NSTextField so that it looks like:
09:55:30 PM, Thursday, July 18, 2002
or the equivalent:
21:55:30, Thursday, July 18, 2002
when the user has opted for 24-hour time display (which is what I use normally).
Originally I was generating the formatter as follows:
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSDateFormatter* timeAndDateFormatter = [[XRayDateFormatter alloc]
initWithDateFormat:[NSString stringWithFormat:@"%@, %@",
[userDefaults stringForKey:@"NSTimeFormatString"],
[userDefaults stringForKey:@"NSDateFormatString"]]
allowNaturalLanguage:YES];
and this worked perfectly for 24-hour display. Naturally, I neglected to test the 12-hour display case :-)...
For 24-hour display, NSTimeFormatString returns @"%H:%M:%S" which is OK. But for 12-hour display, NSTimeFormatString returns @"%I:%M:%S", which does NOT display the AM/PM strings. Actually, the correct formats should be @"%H:%M:%S%p" and @"%I:%M:%S%p" - the AM/PM strings are empty for 24-hour.
So my first question is, is %p's absence a bug or should I really insert it by hand into the format?
I did insert %p into the format and this indeed gives me the desired displayed format. However, when editing in 12-hour mode (or just when tabbing over the NSTextField), the PM designation is always ignored, and changed to AM.
I've traced through the code and it seems that the only available option would be for me to subclass NSDateFormatter and reimplement
- (BOOL)getObjectValue:(id *)anObject forString:(NSString *)string errorDescription:(NSString **)error
from scratch.
I'd rather spend my time killing my own bugs, so my second question is, does anyone know if this bug has been fixed in 10.2? (I've had to let my Select membership lapse for a few months, so I don't have any access to it.)
TIA,
--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"I love deadlines. I love the whooshing noise they make as they go by" (Douglas Adams)
http://www.brockerhoff.net/ (updated July 2002)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.