iOS5シミュレータおよびデバイスでは、NSDateFormatterは "Asia/「z」または「zzz」指定子の場合は「Kolkata」です。NSDateFormatterはGMTオフセット "z"または "zzz"指定子の "Asia/Kolkata"のタイムゾーン省略形を表示しません
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"z"; // or @"zzz"
dateFormatter.timeZone = timeZone;
NSLog(@"date string: %@", [dateFormatter stringFromDate:[NSDate date]]); // "GMT+05:30", expected "IST"
NSLog(@"time zone abbreviation: %@", [timeZone abbreviationForDate:[NSDate date]]); // "IST"
私は出力に上記のコードを期待:
IST
IST
をしかし、それは出力:インドのロケールにロケールを設定する
GMT+05:30
IST
EDIT
にはありません助けてくれるようです。
NSLocale *indianEnglishLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_IN"] autorelease];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:indianEnglishLocale];
[dateFormatter setDateFormat:@"z"]; // or @"zzz"
[dateFormatter setTimeZone:timeZone];
NSLog(@"date string: %@", [dateFormatter stringFromDate:[NSDate date]]); // "GMT+05:30", expected "IST"
NSLog(@"time zone abbreviation: %@", [timeZone abbreviationForDate:[NSDate date]]); // "IST"
私は出力に上記のコードを期待:
IST
IST
をしかし、それは出力:
GMT+05:30
IST
は、これはバグですか?私は何か間違っているのですか? People have mentioned that NSDateFormatter has bugs, especially when a time zone is specified in the format string. Could this be one of those bugs?
を現代のiOSでの唯一の方法は、日付を使用するために、アップルが推奨することに注意してください。フォーマッタは:https://stackoverflow.com/a/42370648/294884 – Fattie