NSShortDateFormatString etc. deprecated in Leopard - what should I use instead?

  • Hi,

    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)

    In the header file, I can find:
    /* The following keys and their values are deprecated in Mac OS X 10.5
    "Leopard". Developers should use NSLocale, NSDateFormatter and
    NSNumberFormatter to retrieve the values formerly returned by these
    keys.
      */

    My problem is: I can not figure out how I can replace the above line
    of code with code of NSLocale, NSDateFormatter or NSNumberFormatter!

    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?

    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?

    Regards,
    Mani
    --
    http://www.mani.de
    iVolume - Loudness adjustment for iTunes.
    LittleSecrets - The encrypted notepad.
  • 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
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today