1

時間文字列を別の timeZone に変換する際に問題が発生しています。

これは、サーバーから取得した文字列の例です: @"5/14/2013 4:19am"

timeZone が America/New_York であることはわかっていますが、これを自分のローカル timeZone: Europe/Rome に変換したいと考えています。

これをテストするために書いたコードは次のとおりです。

-(void) convertTimeZone
{   

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone=[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    dateFormatter.dateFormat=@"MM/dd/yyyy HH:mma";
    NSString *sourceDateString = @"5/14/2013 4:19am";

    NSTimeZone *sourceTimeZone = [[NSTimeZone alloc] initWithName:@"America/New_York"];
    NSTimeZone *destinationTimeZone = [NSTimeZone localTimeZone];
    NSInteger sourceGMToffset = sourceTimeZone.secondsFromGMT;
    NSInteger destinationGMToffset = destinationTimeZone.secondsFromGMT;
    NSInteger interval = destinationGMToffset-sourceGMToffset;


    NSLog(@"DateFormatter TimeZone: %@", dateFormatter.timeZone);
    NSLog(@"Source TimeZone: %@", sourceTimeZone);
    NSLog(@"Destination TimeZone: %@", destinationTimeZone);
    NSLog(@"sourceOffset: %u destinationOffset: %u interval: %u", sourceGMToffset, destinationGMToffset, interval);

    NSLog(@"----------------------------------------------------------------------------------------------------");

    NSLog(@"sourceDateString : %@", sourceDateString);

    //Convert from string to date

    NSDate *dateObject = [dateFormatter dateFromString:sourceDateString];
    NSLog(@"From string to date: %@", dateObject);

    //Now back from date to string
    NSLog(@"From date to string: %@", [dateFormatter stringFromDate:dateObject]);

    //Convert from sourceTimeZone to destinationTimeZone
    NSDate *newDate = [[NSDate alloc] initWithTimeInterval: interval sinceDate:dateObject];     
    NSLog(@"destinationDateString: %@", [dateFormatter stringFromDate: newDate]);

}

出力:


DateFormatter TimeZone: GMT (GMT) オフセット 0

ソース タイムゾーン: America/New_York (GMT-04:00) オフセット -14400 (夏時間)

Destination TimeZone: ローカル タイム ゾーン (ヨーロッパ/ローマ (CEST) オフセット 7200 (夏時間))

sourceOffset: 4294952896 destinationOffset: 7200 interval: 21600

sourceDateString : 2013 年 5 月 14 日午前 4 時 19 分

文字列から日付まで: 2013-05-14 00:19:00 +0000

日付から文字列まで: 2013/05/14 00:19AM

destinationDateString: 2013/05/14 06:19AM


怒るよ!!!!

元の日付が「5/14/2013 4:19am」の場合、なぜ日付フォーマッターは「05/14/2013 00:19AM」に変更しますか????????? 「05/14/2013 04:19AM +0000」に正しく保存されていれば、変換は完璧です (2 つのタイムゾーン間で 6 時間)!!!

ヒントはありますか?

ありがとう

ニコラ


編集

これらのパラメーターを lithu-tv の提案どおりに設定することにより、次のようになります。

dateFormatter.timeZone=[NSTimeZone timeZoneWithAbbreviation:@"GMT"];

NSString *sourceDateString = @"5/14/2013 4:19am -0400";

動作するように見えますが、動作しません。

私が「そう思われる」と言ったのは、次の理由による。

sourceDateString: 2013 年 5 月 14 日 04:19am -0400

文字列から日付まで: 2013-05-13 04:19:00 +0000 (OK)

日付から文字列へ: 05/14/2013 04:19 (OK)

destinationDateString: 2013 年 5 月 14 日 10:19 (正しい)

正しいですが、午後遅くに試してみると:

sourceDateString: 2013 年 5 月 14 日午後 10 時 19 分 -0400

文字列から日付まで: 2013-05-14 16:19:00 +0000 (間違っています)

日付から文字列まで: 2013/05/14 16:19

destinationDateString: 2013 年 5 月 14 日 22:19 (間違っています!)

正しい結果は次のようになります: 05/15/2013 04:19AM

4

3 に答える 3