Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with how I'm using NSDateFormatter?

   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
   [dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzz"];
   NSString *dateString = @"Tue, 08 Jun 2010 17:00:00 EDT";
   NSDate *eventDate = [dateFormatter dateFromString:dateString];

In this case the eventDate object is nil. Can somebody clue me in? This code used to work.

UPDATE: Can't talk about why this doesn't work due to NDA. Suffice it to say, when iOS 4 is out I will post the answer to my own question.

like image 785
Thom Mahoney Avatar asked Jun 07 '10 22:06

Thom Mahoney


2 Answers

/*
    x           number
    xx          two digit number
    xxx         abbreviated name
    xxxx        full name

    a           AM/PM
    A           millisecond of day
    c           day of week (c,cc,ccc,cccc)
    d           day of month
    e           day of week (e,EEE,EEEE)
    F           week of month
    g           julian day (since 1/1/4713 BC)
    G           era designator (G=GGG,GGGG)
    h           hour (1-12, zero padded)
    H           hour (0-23, zero padded)
    L           month of year (L,LL,LLL,LLLL)
    m           minute of hour (0-59, zero padded)
    M           month of year (M,MM,MMM,MMMM)
    Q           quarter of year (Q,QQ,QQQ,QQQQ)
    s           seconds of minute (0-59, zero padded)
    S           fraction of second
    u           zero padded year
    v           general timezone (v=vvv,vvvv)
    w           week of year (0-53, zero padded)
    y           year (y,yy,yyyy)
    z           specific timezone (z=zzz,zzzz)
    Z           timezone offset +0000

    sql         y-M-d H:m:s
    rss         [E, ]d MMM y[y] H:m:s Z|z[zzz]
*/

This is my comment for date parsing. I use the following, where toDateUsingFormat uses an NSDateFormatter with the passed in string. I do not use a locale, because rss dates are not localized.

    if ( 0 == [string rangeOfString:@","].length ) {
        result = [string toDateUsingFormat:@"d MMM y H:m:s z"];
    } else {
        result = [string toDateUsingFormat:@"E, d MMM y H:m:s z"];
    }

Edit:

I use getObjectValue: instead of dateFromString.

NSDate *result = nil;
NSError *error = nil;
[dataFormatter getObjectValue:&result forString:dateString errorDescription:&error];
like image 122
drawnonward Avatar answered Nov 04 '22 20:11

drawnonward


The answer to this question is the following: I was using the wrong date format string:

@"EEE, d MMM yyyy HH:mm:ss zzz"

when it should have been:

@"EEE, dd MMM y HH:mm:ss zzz"

The part about iOS 4 and NDA was that I thought I had to use the NSDateFormatter method dateFormatFromTemplate:options:locale: which would have looked like this:

NSString *format = [NSDateFormatter dateFormatFromTemplate:@"EEE, d MMM yyyy HH:mm:ss zzz" options:0 locale:[NSLocale currentLocale]];

However, that method should only be used when you want to DISPLAY the date to a user of unknown locale. In my case, I knew exactly what the date format was going to look like and I was trying to PARSE the date string so that I could store it in CoreData. Therefore, that method wasn't useful.

Bonus bookmark: Read this table very carefully and you will definitely figure out what the problem is... Unicode date formats should follow these specifications: http://unicode.org/reports/tr35/tr35-6.html#Date_Field_Symbol_Table

TL;DR The format string was wrong. D'oh!

like image 4
Thom Mahoney Avatar answered Nov 04 '22 20:11

Thom Mahoney