【问题标题】:GMT+5:30 to NSTimeZoneName String [duplicate]GMT+5:30 到 NSTimeZoneName 字符串 [重复]
【发布时间】:2014-09-09 13:50:41
【问题描述】:

我有一个字符串 GTM+5:30,我想要它的相关时区名称示例:Asia/Kolkata

有没有办法得到它。

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

这不会返回时区名称(亚洲/加尔各答)

如果我有字符串 GMT+5:30,请帮助我找到获取时区名称(亚洲/加尔各答)的方法

【问题讨论】:

  • 恰恰相反。你应该在那里放一些东西:en.wikipedia.org/wiki/List_of_time_zone_abbreviations 而不是GMT+5:30
  • 为什么要时区名称?
  • 因为我想设置 [nsdateformate datefromtimezone:[nstimezone timezonewithname:]];所以我现在做的是 [dateFr setTimeZone:[NSTimeZone timeZoneWithName:@"UTC+10:00"]];它对我有用....

标签: ios objective-c nstimezone


【解决方案1】:

您可以枚举已知时区,并检查每个时区以查看它是否与您所需的时区具有相同的偏移量。我的测试程序:

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

输出:

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

显然有两个时区与GMT+5:30 具有相同的偏移量。 (其他偏移量可能更多;例如,尝试GMT-0500。)由您决定要使用哪一个。

【讨论】:

  • 确切的问题是在给定日期可能有多个时区具有相同的 GMT 偏移量。但是这些时区中的每一个都可以有自己的 DST 等规则
猜你喜欢
  • 2021-05-26
  • 2020-09-22
  • 2015-07-21
  • 1970-01-01
  • 2014-08-06
  • 2020-04-20
  • 2023-03-09
  • 2013-08-04
  • 1970-01-01
相关资源
最近更新 更多