1

I have an app where i want to provide a list of UTC timezone so that the user could select destination time. I have all the country abbreviations in picker view. But i want UTC abbreviations. Can anyone please suggest me how could i achieve this.

Thanks

Tajinder
  • 13
  • 6
  • There's no such thing as a "UTC timezone" or a "UTC abbreviation". I think you are looking for the term "time zone offset" - which is not the same as a "time zone". Please review [the timezone tag wiki](http://stackoverflow.com/tags/timezone/info). – Matt Johnson-Pint Dec 17 '13 at 16:40

2 Answers2

4

You can use the knownTimeZoneNames property of NSTimeZone for all timezones.

[NSTimeZone knownTimeZoneNames]

Or you can use abbreviationDictionary for getting all abbreviations

[[NSTimeZone abbreviationDictionary] allKeys]

If you want time of these timezones, the use the below code

NSArray *abbs = [[NSTimeZone abbreviationDictionary] allKeys];

    for (id eachObj in abbs) {

        NSString *dateStr = [self dateFromTimeZoneAbbreviation:eachObj];
        NSLog(@"%@",dateStr);

    }

define the method as

-(NSString *)dateFromTimeZoneAbbreviation:(NSString *)abb   {

    NSString *dateStr;

    NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
    NSTimeZone* timeZoneFromAbbreviation = [NSTimeZone timeZoneWithAbbreviation:abb];

    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:[NSDate date]];
    NSInteger gmtOffset = [timeZoneFromAbbreviation secondsFromGMTForDate:[NSDate date]];
    NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;

    NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:[NSDate date]] ;

    NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
    [dateFormatters setDateFormat:@"dd-MMM-yyyy hh:mm"];
    /*[dateFormatters setDateStyle:NSDateFormatterShortStyle];
     [dateFormatters setTimeStyle:NSDateFormatterShortStyle];
     [dateFormatters setDoesRelativeDateFormatting:YES];*/
    [dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];
    dateStr = [dateFormatters stringFromDate: destinationDate];
    NSLog(@"DateString : %@, TimeZone : %@", dateStr , timeZoneFromAbbreviation.abbreviation);

    return dateStr;
}
manujmv
  • 6,450
  • 1
  • 22
  • 35
  • It returns all the list of countries. But i need like this UTC+01:00 to set the time zone. – Tajinder Dec 17 '13 at 08:33
  • Thanks. Can you also suggest me that how could i pass UTC+01:00 to get time accordingly. – Tajinder Dec 17 '13 at 08:36
  • you mean current time of all timezones? – manujmv Dec 17 '13 at 08:49
  • or u mean u need time difference from GMT? – manujmv Dec 17 '13 at 09:00
  • I want to provide a list of all UTC+ and UTC-- time. Suppose i am in india and want to know what will the time at America or anyother selected countries. – Tajinder Dec 17 '13 at 09:04
  • @Tajinder - There are way more time zones than just one per country. Many countries have multiple time zones. And because of daylight saving time, you can't express them with just an offset from UTC. See also [this question](http://stackoverflow.com/q/19695439/634824) and its answers. – Matt Johnson-Pint Dec 17 '13 at 16:43
1
NSLog(@"%@", [NSTimeZone knownTimeZoneNames]);//viewDidLoad
for(id timezone in [NSTimeZone knownTimeZoneNames]){
     NSLog(@"%@",[self getTimeZoneStringForAbbriviation:timezone]);
    NSLog(@"%@", timezone);
}

-(NSString*)getTimeZoneStringForAbbriviation:(NSString*)abbr{
    NSTimeZone *atimezone=[NSTimeZone timeZoneWithName:abbr];
    int minutes = (atimezone.secondsFromGMT / 60) % 60;
    int hours = atimezone.secondsFromGMT / 3600;
    NSString *aStrOffset=[NSString stringWithFormat:@"%02d:%02d",hours, minutes];
    return [NSString stringWithFormat:@"GMT%@",aStrOffset];
}
Mrug
  • 4,963
  • 2
  • 31
  • 53