GMT timezone conversion in objective c

安稳与你 提交于 2019-12-17 10:33:31

问题


I am trying to convert nsstring to nsdate and then to the systemtimezone. Is my code right? Any help appreciated.

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];

The code result is 2012-01-15 06:27:42 +0000 which is the same as the source!


回答1:


I think I can see what the problem is here - it's this line:

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

GMT-07:00 is not a valid abbreviation for this method. If you call [NSTimeZone abbreviationDictionary] you'll get a dictionary containing all available abbreviations. You need to use the actual abbreviation (eg, 'PST' for Pacific Standard Time), and not the format "GMT-07:00".

To save you time, here's the full list of supported abbreviations using 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";



回答2:


The list above of “supported" abbreviations is also an abbreviated list..

if you want to see a fairly complete list, here is some code to generate it:

(and a small part of the output below to show some that are not in the list above) unfortunately the OS will not use these extra zones) you’ll have to do a conversion to -0600 types

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


来源:https://stackoverflow.com/questions/8871727/gmt-timezone-conversion-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!