Question

J'essaie de convertir NSString en NSDate puis sur SystemTimeZone. Mon code est-il juste? Toute aide appréciée.

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

Le résultat du code est 2012-01-15 06:27:42 +0000, ce qui est le même que la source!

Était-ce utile?

La solution

je pense Je peux voir quel est le problème ici - c'est cette ligne:

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

GMT-07:00 n'est pas une abréviation valide pour cette méthode. Si vous appelez [NSTimeZone abbreviationDictionary] Vous obtiendrez un dictionnaire contenant toutes les abréviations disponibles. Vous devez utiliser l'abréviation réelle (par exemple, «PST» pour le temps standard du Pacifique), et non le format «GMT-07: 00».

Pour vous faire gagner du temps, voici la liste complète des abréviations prises en charge en utilisant 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";

Autres conseils

La liste ci-dessus des abréviations «prises en charge» est également une liste abrégée.

Si vous souhaitez voir une liste assez complète, voici un code pour le générer:

(et une petite partie de la sortie ci-dessous pour afficher certains qui ne sont pas dans la liste ci-dessus) Malheureusement, le système d'exploitation n'utilisera pas ces zones supplémentaires) Vous devrez faire une conversion en -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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top