目标c中的GMT时区转换

时间:2012-01-15 17:27:32

标签: objective-c nsdate nstimezone

我正在尝试将nsstring转换为nsdate,然后转换为systemtimezone。我的代码是对的吗?任何帮助赞赏。

NSString *str=@"2012-01-15 06:27:42";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [dateFormatter dateFromString:str];

NSDate* sourceDate = dateFromString;

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT-07:00"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];

bottomLabel2.text=[NSString stringWithFormat:@"- %@ -",destinationDate];

代码结果是2012-01-15 06:27:42 +0000,与源代码相同!

2 个答案:

答案 0 :(得分:16)

我可以看到问题在这里 - 就是这一行:

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT-07:00"];

GMT-07:00不是此方法的有效缩写。如果您致电[NSTimeZone abbreviationDictionary],您将获得包含所有可用缩写的字典。您需要使用实际缩写(例如,太平洋标准时间的'PST'),而不是格式“GMT-07:00”。

为了节省您的时间,以下是使用NSTimeZone支持的缩写的完整列表。

    ADT = "America/Halifax";
    AKDT = "America/Juneau";
    AKST = "America/Juneau";
    ART = "America/Argentina/Buenos_Aires";
    AST = "America/Halifax";
    BDT = "Asia/Dhaka";
    BRST = "America/Sao_Paulo";
    BRT = "America/Sao_Paulo";
    BST = "Europe/London";
    CAT = "Africa/Harare";
    CDT = "America/Chicago";
    CEST = "Europe/Paris";
    CET = "Europe/Paris";
    CLST = "America/Santiago";
    CLT = "America/Santiago";
    COT = "America/Bogota";
    CST = "America/Chicago";
    EAT = "Africa/Addis_Ababa";
    EDT = "America/New_York";
    EEST = "Europe/Istanbul";
    EET = "Europe/Istanbul";
    EST = "America/New_York";
    GMT = GMT;
    GST = "Asia/Dubai";
    HKT = "Asia/Hong_Kong";
    HST = "Pacific/Honolulu";
    ICT = "Asia/Bangkok";
    IRST = "Asia/Tehran";
    IST = "Asia/Calcutta";
    JST = "Asia/Tokyo";
    KST = "Asia/Seoul";
    MDT = "America/Denver";
    MSD = "Europe/Moscow";
    MSK = "Europe/Moscow";
    MST = "America/Denver";
    NZDT = "Pacific/Auckland";
    NZST = "Pacific/Auckland";
    PDT = "America/Los_Angeles";
    PET = "America/Lima";
    PHT = "Asia/Manila";
    PKT = "Asia/Karachi";
    PST = "America/Los_Angeles";
    SGT = "Asia/Singapore";
    UTC = UTC;
    WAT = "Africa/Lagos";
    WEST = "Europe/Lisbon";
    WET = "Europe/Lisbon";
    WIT = "Asia/Jakarta";

答案 1 :(得分:2)

“支持”缩写的上面列表也是缩写列表..

如果你想看到一个相当完整的列表,这里有一些代码来生成它:

(以下输出的一小部分显示一些不在上面列表中的内容) 不幸的是,操作系统不会使用这些额外的区域)你必须转换为-0600类型

NSDate *myDate = [NSDate date];
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateStyle:NSDateFormatterLongStyle];
[f setTimeStyle:NSDateFormatterLongStyle];
[f setDateFormat:@"ZZZ"];

NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
for (NSString *name1 in timeZoneNames)
{
    NSTimeZone *tz = [NSTimeZone timeZoneWithName:name1];
    [f setTimeZone:tz];

    NSLog(@"%@ = \"%@\" = %d = %@", [tz abbreviation], name1, [tz secondsFromGMT], [f stringFromDate:myDate]);
}



 2012-02-13 15:53:26.497 HelloWorld[23220:207] NZDT = "Pacific/Auckland" = 46800 = +1300
2012-02-13 15:53:26.498 HelloWorld[23220:207] CHADT = "Pacific/Chatham" = 49500 = +1345
2012-02-13 15:53:26.508 HelloWorld[23220:207] CHUT = "Pacific/Chuuk" = 36000 = +1000
2012-02-13 15:53:26.509 HelloWorld[23220:207] EASST = "Pacific/Easter" = -18000 = -0500
2012-02-13 15:53:26.510 HelloWorld[23220:207] VUT = "Pacific/Efate" = 39600 = +1100
2012-02-13 15:53:26.511 HelloWorld[23220:207] PHOT = "Pacific/Enderbury" = 46800 = +1300
2012-02-13 15:53:26.512 HelloWorld[23220:207] TKT = "Pacific/Fakaofo" = -36000 = -1000
2012-02-13 15:53:26.513 HelloWorld[23220:207] FJT = "Pacific/Fiji" = 43200 = +1200
2012-02-13 15:53:26.514 HelloWorld[23220:207] TVT = "Pacific/Funafuti" = 43200 = +1200
2012-02-13 15:53:26.515 HelloWorld[23220:207] GALT = "Pacific/Galapagos" = -21600 = -0600
2012-02-13 15:53:26.516 HelloWorld[23220:207] GAMT = "Pacific/Gambier" = -32400 = -0900
2012-02-13 15:53:26.518 HelloWorld[23220:207] SBT = "Pacific/Guadalcanal" = 39600 = +1100
2012-02-13 15:53:26.518 HelloWorld[23220:207] ChST = "Pacific/Guam" = 36000 = +1000
2012-02-13 15:53:26.520 HelloWorld[23220:207] HST = "Pacific/Honolulu" = -36000 = -1000
2012-02-13 15:53:26.521 HelloWorld[23220:207] HST = "Pacific/Johnston" = -36000 = -1000
2012-02-13 15:53:26.522 HelloWorld[23220:207] LINT = "Pacific/Kiritimati" = 50400 = +1400
2012-02-13 15:53:26.523 HelloWorld[23220:207] KOST = "Pacific/Kosrae" = 39600 = +1100
2012-02-13 15:53:26.524 HelloWorld[23220:207] MHT = "Pacific/Kwajalein" = 43200 = +1200
2012-02-13 15:53:26.526 HelloWorld[23220:207] MHT = "Pacific/Majuro" = 43200 = +1200
2012-02-13 15:53:26.527 HelloWorld[23220:207] MART = "Pacific/Marquesas" = -34200 = -0930
2012-02-13 15:53:26.528 HelloWorld[23220:207] SST = "Pacific/Midway" = -39600 = -1100
2012-02-13 15:53:26.529 HelloWorld[23220:207] NRT = "Pacific/Nauru" = 43200 = +1200
2012-02-13 15:53:26.530 HelloWorld[23220:207] NUT = "Pacific/Niue" = -39600 = -1100
2012-02-13 15:53:26.531 HelloWorld[23220:207] NFT = "Pacific/Norfolk" = 41400 = +1130
2012-02-13 15:53:26.564 HelloWorld[23220:207] NCT = "Pacific/Noumea" = 39600 = +1100
2012-02-13 15:53:26.565 HelloWorld[23220:207] SST = "Pacific/Pago_Pago" = -39600 = -1100
2012-02-13 15:53:26.566 HelloWorld[23220:207] PWT = "Pacific/Palau" = 32400 = +0900
2012-02-13 15:53:26.567 HelloWorld[23220:207] PNT = "Pacific/Pitcairn" = -28800 = -0800
2012-02-13 15:53:26.570 HelloWorld[23220:207] PONT = "Pacific/Pohnpei" = 39600 = +1100
2012-02-13 15:53:26.571 HelloWorld[23220:207] PONT = "Pacific/Ponape" = 39600 = +1100
2012-02-13 15:53:26.572 HelloWorld[23220:207] PGT = "Pacific/Port_Moresby" = 36000 = +1000
2012-02-13 15:53:26.574 HelloWorld[23220:207] CKT = "Pacific/Rarotonga" = -36000 = -1000
2012-02-13 15:53:26.575 HelloWorld[23220:207] ChST = "Pacific/Saipan" = 36000 = +1000
2012-02-13 15:53:26.576 HelloWorld[23220:207] TAHT = "Pacific/Tahiti" = -36000 = -1000
2012-02-13 15:53:26.578 HelloWorld[23220:207] GILT = "Pacific/Tarawa" = 43200 = +1200
2012-02-13 15:53:26.579 HelloWorld[23220:207] TOT = "Pacific/Tongatapu" = 46800 = +1300
2012-02-13 15:53:26.580 HelloWorld[23220:207] CHUT = "Pacific/Truk" = 36000 = +1000
2012-02-13 15:53:26.582 HelloWorld[23220:207] WAKT = "Pacific/Wake" = 43200 = +1200
2012-02-13 15:53:26.583 HelloWorld[23220:207] WFT = "Pacific/Wallis" = 43200 = +1200