2012-01-15 67 views
4

我想將nsstring轉換爲nsdate,然後到systemtimezone。我的代碼是否正確?任何幫助讚賞。GMT時區轉換在目標c

NSString *[email protected]"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這與源代碼相同!

+0

「我的代碼是否正確?」不是在這裏問的問題。 http://codereview.stackexchange.com – esqew 2012-01-15 17:28:35

+1

我不認爲原始海報是要求審查,但可能沒有明確說明問題。無論如何,我已在下面回答。 – lxt 2012-01-15 17:38:43

回答

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"; 
+0

非常感謝!它現在有效! – stefanosn 2012-01-15 17:57:45

+0

還有一件事要正確工作,你需要做bottomLabel2.text = [NSString stringWithFormat:@「 - %@ - 」,[dateFormatter stringFromDate:destinationDate]]; – stefanosn 2012-01-15 18:03:23

2

上面的列表中的「支持」的縮寫也是一個簡表..

如果你想看到一個相當完整的列表

,這裏是一些代碼來生成它:

(和下面的輸出中的一小部分,顯示有些不在上述名單) 不幸的是,OS將不會使用這些額外的區域),你就必須做一個轉換爲-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