2014-11-03 21 views
0

當我運行下面的PSTAKST時區從不運行這段代碼......上述這些問題都完美運行:PST和農業知識與科技時區錯誤

NSTimeZone *pacificTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"PST"]; 
NSTimeZone *alaskaTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"AKST"]; 

但是這些沒有一個是運行:

}else if ([local isEqual: pacificTimeZone]) { 
    NSLog(@"Pacific"); 

} else if ([local isEqual: alaskaTimeZone]) { 
    NSLog(@"Alaska"); 

有什麼建議嗎?

NSTimeZone *local = [NSTimeZone localTimeZone]; 
    NSTimeZone *centralTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"CST"]; 
    NSTimeZone *easternTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"]; 
    NSTimeZone *mountainTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"MST"]; 
    NSTimeZone *pacificTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"PST"]; 
    NSTimeZone *alaskaTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"AKST"]; 
    NSTimeZone *honoluluTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"HST"]; 

    NSLog(@"LOCAL TIME ZONE: %@", local); 

    NSLog(@"CST TIME %@", centralTimeZone); 
    NSLog(@"EST TIME %@", easternTimeZone); 
    NSLog(@"MST TIME %@", mountainTimeZone); 
    NSLog(@"PST TIME %@", pacificTimeZone); 
    NSLog(@"AKST TIME %@", alaskaTimeZone); 
    NSLog(@"HST TIME %@", honoluluTimeZone); 


    if ([local isEqual: centralTimeZone]) 
    { 
     NSLog(@"Central"); 

    } else if ([local isEqual: easternTimeZone]) { 
     NSLog(@"Eastern"); 

    } else if ([local isEqual: mountainTimeZone]) { 
     NSLog(@"Mountain"); 

    }else if ([local isEqual: pacificTimeZone]) { 
     NSLog(@"Pacific"); 

    } else if ([local isEqual: alaskaTimeZone]) { 
     NSLog(@"Alaska"); 

    } else if ([local isEqual: honoluluTimeZone]) { 
     NSLog(@"Honolulu"); 

    } 

謝謝!

回答

2

the documentation

在一般情況下,你是從使用除了諸如「UTC」或「GMT」唯一實例縮寫氣餒。時區縮寫沒有標準,所以給定的縮寫,可以有多重含義 - 例如,「EST」是指東部時間同時在美國和澳大利亞

相反,使用timeZoneWithName。支持的時區來自IANA TZ database,您可以找到它們的列表here

爲美國最常見的區域如下:

Alaska = "America/Anchorage" 
Hawaii = "Pacific/Honolulu" 
Pacific = "America/Los_Angeles" 
Arizona = "America/Phoenix" 
Mountain = "America/Denver" 
Central = "America/Chicago" 
Eastern = "America/New_York" 
+0

夏威夷及其太平洋/火奴魯魯 – rgreso 2016-10-20 10:11:25

+0

@rgreso - 是的,你是正確的。編輯。謝謝。 – 2016-10-20 19:57:28