-1

I have a string GTM+5:30 and i want its related time zone name example: Asia/Kolkata

Is there a possible way to get it.

NSTimeZone* localTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+5:30"];
NSLog(@"Name: %@",localTimeZone.name);

this doesn't return the time zone name(Asia/Kolkata)

Please help me finding a way to get the time zone name(Asia/Kolkata) if i have the string GMT+5:30

  • It's the reverse. You should put something there: http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations instead of `GMT+5:30`. – Larme Sep 09 '14 at 13:54
  • WHY do you want the timezone name? – Hot Licks Sep 09 '14 at 15:17
  • Because i want to set [nsdateformate datefromtimezone:[nstimezone timezonewithname:]]; so What i did now is [dateFr setTimeZone:[NSTimeZone timeZoneWithName:@"UTC+10:00"]]; and it worked for me.... – GouthamDevaraju Sep 12 '14 at 05:49

1 Answers1

1

You can enumerate the known time zones, and check each one to see if it has the same offset as your desired time zone. My test program:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT+5:30"];
        NSInteger secondsFromGMT = timeZone.secondsFromGMT;
        for (NSString *name in [NSTimeZone knownTimeZoneNames]) {
            NSTimeZone *candidate = [NSTimeZone timeZoneWithName:name];
            if (candidate.secondsFromGMT == secondsFromGMT) {
                NSLog(@"%@ is currently the same offset from GMT as %@", candidate.name, timeZone.name);
            }
        }
    }
    return 0;
}

The output:

2014-09-09 09:09:01.897 timezone[87091:303] Asia/Colombo is currently the same offset from GMT as GMT+0530
2014-09-09 09:09:01.898 timezone[87091:303] Asia/Kolkata is currently the same offset from GMT as GMT+0530

Apparently there are two time zones with the same offset as GMT+5:30. (There can be many more for other offsets; try GMT-0500 for example.) It's up to you to decide which one you want to use.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    Exactly the problem is that there can be multiple timezones with the same GMT offset on a given date. But each one of those timezones can have their own rules for DST etc – uchuugaka Sep 09 '14 at 14:49