-2

I'm storing values in GMT format on a server. I download them on the device and convert it to the users local time via the following function:

- (void)convertTimeZone
{
    //Converts the match timing from GMT to the users local time
    NSString *serverDateString = [NSString stringWithFormat:@"%@-%@-%@ %@:%@ GMT", year, month, day, hour, min];

    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    dateFormatter.dateFormat       = @"yyyy-MM-dd HH:mm zzz";

    NSDate *serverDate             = [dateFormatter dateFromString:serverDateString];

    NSString *localDateString      = [dateFormatter stringFromDate:serverDate];
    NSDate *localDate              = [dateFormatter dateFromString:localDateString];


    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit |NSMinuteCalendarUnit |NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:localDate];

    min   = [NSString stringWithFormat:@"%d",[components minute]];
    hour  = [NSString stringWithFormat:@"%d",[components hour]];
    day   = [NSString stringWithFormat:@"%d",[components day]];
    month = [NSString stringWithFormat:@"%d",[components month]];
    year  = [NSString stringWithFormat:@"%d",[components year]];

}

Almost everybody gets the proper time after conversion. However few users have written in to say that the date they receive is 1st January 2001, which is the initialization date on iOS and OSX. And the date is the time difference between GMT and their local date.

They values year, month, day, hour and min are properly set and received by the server. I know that because some of the other values are displayed properly.

However this date-time conversion process goes wrong in few cases.

Why does it happen?

Since it has never happened on my device but has happened to users in other countries, I don't know how to reproduce it. They tried to restart it but they still get the wrong date. Thanks.

Bilbo Baggins
  • 3,644
  • 8
  • 40
  • 64
  • Without seeing your inputs and outputs it's impossible to locate all the places where you might have screwed up. (BTW, -1 for declaring an "iOS bug" when the odds of that are vanishingly small.) – Hot Licks Aug 21 '13 at 16:04
  • Your code makes absolutely no sense -- you convert a string to NSDate, convert that back to string, then convert to NSDate again, all with the same date formatter, and with no changes to the date formatter setup. What do you expect to change? – Hot Licks Aug 21 '13 at 16:07
  • (And how do you expect the above to convert from GMT to local time?) – Hot Licks Aug 21 '13 at 16:10
  • @HotLicks I've checked things to isolate the problem. Getting JSON values to the device and storing it is working fine. I know this because few users are facing problem with the time only and not other text data. – Bilbo Baggins Aug 21 '13 at 16:14
  • @HotLicks The above code does convert the date-time from GMT to users local time. 1 in a 100 people found that it did not work. I thought it was a bug. Hopefully bdesham's answer will work. – Bilbo Baggins Aug 21 '13 at 16:16
  • Then maybe you're right -- maybe iOS *is* broken. Because I can't seen how the above does a timezone conversion. – Hot Licks Aug 21 '13 at 16:24
  • stringWithFormat:@"%@-%@-%@ %@:%@ GMT. Notice the GMT part. – Bilbo Baggins Aug 21 '13 at 16:29
  • Ah, yes. But why convert back and forth twice? Once you've got it into an NSDate there's no need for further conversion. – Hot Licks Aug 21 '13 at 17:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35934/discussion-between-pritesh-desai-and-hot-licks) – Bilbo Baggins Aug 21 '13 at 17:12

2 Answers2

1

You're failing to set the locale, as required per QA1480. Hence the date format you've set may be modified as per the user's settings.

Set the locale explicitly to resolve the problem.

Tommy
  • 99,986
  • 12
  • 185
  • 204
1

Any time you’re using an NSDateFormatter to read or emit dates that need to be in a certain specific format, you’ll want to do something like this:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];

This will prevent the problem shown e.g. here.

Community
  • 1
  • 1
bdesham
  • 15,430
  • 13
  • 79
  • 123
  • So, will it work everywhere? My app isn't only US centric, so this way will all of them be able to use it? Thanks. – Bilbo Baggins Aug 21 '13 at 16:07
  • Yes, this will work everywhere, and setting the locale manually will not interfere with the localization of your app. The locale is separate from the time zone; setting it manually is necessary to ensure that your users’ devices behave as you expect regardless of their language or region settings. – bdesham Aug 21 '13 at 16:09