【问题标题】:How to get list of all UTC abbreviation in ios?如何获取ios中所有UTC缩写的列表?
【发布时间】:2014-01-04 22:10:40
【问题描述】:

我有一个应用程序,我想在其中提供 UTC 时区列表,以便用户可以选择目的地时间。我在选择器视图中有所有国家/地区的缩写。但我想要 UTC 缩写。 谁能建议我如何实现这一目标。

谢谢

【问题讨论】:

  • 没有“UTC 时区”或“UTC 缩写”之类的东西。我认为您正在寻找“时区偏移”一词-与“时区”不同。请查看the timezone tag wiki

标签: ios timezone


【解决方案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];
}

【讨论】:

    【解决方案2】:

    您可以将 NSTimeZoneknownTimeZoneNames 属性用于所有时区。

    [NSTimeZone knownTimeZoneNames]
    

    或者您可以使用 abbreviationDictionary 获取所有缩写词

    [[NSTimeZone abbreviationDictionary] allKeys]
    

    如果你想要这些时区的时间,使用下面的代码

    NSArray *abbs = [[NSTimeZone abbreviationDictionary] allKeys];
    
        for (id eachObj in abbs) {
    
            NSString *dateStr = [self dateFromTimeZoneAbbreviation:eachObj];
            NSLog(@"%@",dateStr);
    
        }
    

    将方法定义为

    -(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;
    }
    

    【讨论】:

    • 返回所有国家列表。但我需要像这样 UTC+01:00 来设置时区。
    • 谢谢。您还可以建议我如何通过 UTC+01:00 来获得相应的时间。
    • 你是指所有时区的当前时间?
    • 或者你的意思是你需要与 GMT 的时差?
    • 我想提供所有 UTC+ 和 UTC-- 时间的列表。假设我在印度,想知道在美国或任何其他选定国家/地区的时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2013-10-15
    • 2016-05-31
    • 2017-04-02
    • 2011-08-04
    相关资源
    最近更新 更多