NSDateFormatter bug?
NSDateFormatter bug?
- Subject: NSDateFormatter bug?
- From: Alex Rice <email@hidden>
- Date: Tue, 18 Dec 2001 23:00:56 -0700
I am going to file a bug on NSDateFormatter and NSShortDateFormatString,
unless someone can please correct me. I am sending to both cocoa-dev and
macosx-dev because I asked this a week ago on cocoa-dev and got no
responses.
NSShortDateFormatString is what the user sees in System Preferences |
International | Date | Region and Short Date.
NSDateFormatter sometimes correctly responds to NSShortDateFormatString,
and sometimes does NOT. I know it has problems with British and Finnish
although I have not tried all of the formats. I discovered this because
some British users were complaining that they couldn't enter dates
correctly and sure enough it works fine for U.S. and German, but
apparently not British, Finnish, and probably others.
Test case:
1) Create a Cocoa app with an editable TextField and an uneditable
TextField label.
2) Create the controller class below, and connect the outlets and
actions.
3) Run the app and using your System preferences, try various settings
for your NSShortDateFormatString and try entering dates into the
textfield. For example, with "British" for your Date format, 25/12/01
should be valid (xmas day) but is not accepted by the formatter.
However, you can get to 25/12/01 by entering "next week", which it
parses correctly. Similarly, in "Finnish" 25.12.01 is not accepted, but
is valid according to the NSShortDateFormatString.
// Controller.h
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject {
IBOutlet NSTextField *date;
IBOutlet NSTextField *label;
}
-(IBAction) acceptDate:(id) sender;
@end
---
// Controller.m
#import "Controller.h"
@implementation Controller
-(void) awakeFromNib
{
NSString *shortDateFormat = [[NSUserDefaults standardUserDefaults]
objectForKey:NSShortDateFormatString];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]
initWithDateFormat:shortDateFormat
allowNaturalLanguage:YES] autorelease];
[date setFormatter:dateFormatter];
[label setStringValue:[NSString stringWithFormat:
@"NSShortDateFormatString = %@", shortDateFormat]];
}
-(void) acceptDate:(id) sender
{
NSLog(@"accepted date");
NSLog(@"%@", [date objectValue]);
}
@end
Alex Rice <email@hidden>
Mindlube Software
http://www.mindlube.com/