Search code examples
iostimezonensdatenstimezone

Wrong systemTimeZone


I'm trying to get systemTimeZone, but it gives me wrong data:

NSTimeZone * currentDateTimeZone = [NSTimeZone systemTimeZone];
NSString* name = [currentDateTimeZone name];
int myGMT = (int)roundf([currentDateTimeZone secondsFromGMT]/60.f/60.f);

I'm living in Budapest,Hungary. It's in GMT+1, but I'm getting myGMT = 2. But name is ok : name = Europe/Budapest Why?


Solution

  • The current GMT offset for the Europe/Budapest timezone is GMT+2, because the Daylight Saving Time started at Sunday, 30 March 2014, and the clocks were advanced by one hour (see http://www.timeanddate.com/worldclock/city.html?n=50).

    You can verify that with

    BOOL isDst = [currentDateTimeZone isDaylightSavingTime];
    // --> YES
    NSTimeInterval dstOffset = [currentDateTimeZone daylightSavingTimeOffset];
    // --> 3600
    

    If necessary, you can compute

    [currentDateTimeZone secondsFromGMT] - [currentDateTimeZone daylightSavingTimeOffset]
    

    to get the "default" GMT offset for your timezone.