Re: NSShortDateFormatString etc. deprecated in Leopard - what should I use instead?
Re: NSShortDateFormatString etc. deprecated in Leopard - what should I use instead?
- Subject: Re: NSShortDateFormatString etc. deprecated in Leopard - what should I use instead?
- From: Chris Kane <email@hidden>
- Date: Fri, 7 Dec 2007 08:04:31 -0800
On Dec 7, 2007, at 6:15 AM, Manfred Schwind wrote:
I have the following line of code:
NSString *shortDateFormatString = [[NSUserDefaults
standardUserDefaults] objectForKey:NSShortDateFormatString];
When compiling with 10.5 SDK, I got the following warning:
warning: 'NSShortDateFormatString' is deprecated (declared at /
Developer/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/
Foundation.framework/Headers/NSUserDefaults.h:98)
[...]
All I want to do is: get the format string for the short date
format that you e.g. can see in System Preferences - International
- Formats (the last line in the Dates examples). How can I get this
value?
Really? All you want to do is get the string? You're not going to
use it for any sort of date formatting later? You're not going look
at the characters inside the string?
Your code can be simplified down to:
NSString *getShortDateFormat(void) {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]
autorelease];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle]; // short date
[dateFormatter setTimeStyle:kCFDateFormatterNoStyle]; // no time
return [dateFormatter dateFormat];
}
The bit you're missing is that the -setLocale:, setDateStyle: etc.
methods are new methods in 10.4 and only apply to 10_4-style
formatters. When you set the formatter to 10_0 behavior, those
methods became no-ops, and so you end up getting the factory-default
string value out of the date formatter.
Of course, given your comment "// to get e.g. "%d" instead of "dd"",
you probably do want to look at the goodies inside and do something
nefarious with them. ;-) There's no provided mechanism to convert
from one format string style to the other, though such conversion
code can be written. But that would be somewhat lossy as well; the
value you get from NSShortDateFormatString is a lossy conversion of
the 10_4-style format. For more accurate truth in 10.4 and forward,
stick to the new-style string and update any parsing code (if you
really must look at the string contents) to understand the new formats.
I tried the following code:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]
autorelease];
[dateFormatter
setFormatterBehavior:NSDateFormatterBehavior10_0]; // to get e.g.
"%d" instead of "dd"
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle]; // short date
[dateFormatter setTimeStyle:kCFDateFormatterNoStyle]; // no time
NSString *shortDateFormatString = [dateFormatter dateFormat];
But I do not get the correct result. I always get back an english
date format string "%m/%d/%y".
But my system is configured in german and therefore the correct
format string is "%d.%m.%y" and I get that back correctly with the
deprecated NSShortDateFormatString key. But what's the Leopard way
to get that format string?
Chris Kane
Cocoa Frameworks, Apple
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden