4

I am facing a bug (?) with NSDateFormatter. First of all, here's a method I use to get the current NSDate as NSString (converted in a not-so-easy-to-read format because it's sent to a web service) :

+ (NSString *) nowToUTCDate {
    NSDate *now = [NSDate date];
    NSDateFormatter* df_utc = [[NSDateFormatter alloc] init];
    [df_utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [df_utc setDateFormat:@"yyyyMMddHHmmss"];

    return [df_utc stringFromDate:now];
}

On iOS <= 7.0.4, the result gave me : 20140307203040 (even though I use the 12h format).

On my iPhone (iOS 7.0.6), the result is : 20140307083346 PM (only if I use the 12h format). Boom, a " PM" appears ! Why ? I am using the HHstring to explicitly ask for a 24h format (as said here).

As a workaround, I wrote this solution :

NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[df_utc setLocale:enUSPOSIXLocale];

Is that a bug that was introduced by Apple to the last version of iOS or am I misunderstanding something ?

Thank you for your help!

bneely
  • 9,083
  • 4
  • 38
  • 46
quantesys
  • 41
  • 2
  • 1
    That's not a bug, that's a "feature". And it's not new. It's just that the two devices you're testing on are not set up the same. You can get this symptom when you have a 12-hour locale (eg US) device set to 24-hour mode or a 24-hour locale (eg Europe) device set to 12-hour mode. Apple swears up and down that it's "by design". – Hot Licks Mar 07 '14 at 20:51
  • Thank you Hot Lips for your answer. But, how can we explain that it worked before and the "PM" appears after the update ? Thanx for the link, I didn't see it before... You solved my problem! – quantesys Mar 07 '14 at 21:00
  • Like I said, it depends on how the specific device is configured (in the device Setup) -- whether locale and 12/24 mode are consistent or in conflict. – Hot Licks Mar 07 '14 at 21:09

0 Answers0