NSDateFormatter不显示“z”或“zzz”说明符的“Asia / Kolkata”的时区缩写,只是GMT偏移量

在iOS5模拟器和设备上,NSDateFormatter不显示“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 

编辑

将语言环境设置为印度语言环境似乎没有帮助。

 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 

这是一个错误? 难道我做错了什么? 人们提到NSDateFormatter有bug,特别是当在格式string中指定时区时。 这可能是这些错误之一吗?

From http://www.cocoabuilder.com/archive/cocoa/310977-nsdateformatter-not-working-on-ios-5.html#311281

在iOS 5.0中parsing缩略时区名称的变化是开源ICU 4.8库(以及它使用的开源CLDR 2.0数据)的有意改变的结果,其修改版本用于实现一些NSDateFormatterfunction。

问题是这样的:对于由z(= zzz)或v(= vvv)指定的短时区格式,可能会有很多模糊性。 例如,东部时间的“ET”可能适用于许多不同地区的不同时区,为了提高格式化和parsing可靠性,如果“cu”(常用)标志被设置为区域设置,否则只使用长格式(格式化和parsing)。

对于“en”语言环境(=“en_US”),cu标志被设置为诸如阿拉斯加,America_Central,America_Eastern,America_Mountain,America_Pacific,Atlantic,Hawaii_Aleutian和GMT的元区域。 这不是为欧洲中心设定的。

但是,对于“en_GB”语言环境,cu标志设置为Europe_Central。

因此,设置为短时区样式“z”或“zzz”和区域设置“en”或“en_US”的格式化程序将不会parsing“CEST”或“CET”,但是如果区域设置被设置为“en_GB” 。 “GMT”风格将被所有人parsing。

如果格式化程序设置为长时区样式“zzzz”,区域设置为“en”,“en_US”或“en_GB”中的任何一个,则以下任一项都将被parsing,因为它们是明确的:“Pacific Daylight时间“”中欧夏令时间“”中欧时间“

希望这可以帮助。

  • 彼得·埃德伯格

From http://www.cocoabuilder.com/archive/cocoa/313301-nsdateformatter-not-working-on-ios-5.html#313301

Heath,是的,对于上面提供的示例,[dateFormatter stringFromDate:[NSDate date]] 应该使用短时区名称“IST”。 事实并不是由于当前OSX和iOS版本(分别为CLDR 1.9.1和2.0)的ICU使用的CLDR数据版本中的“en_IN”语言环境数据不足。 这些CLDR版本中的“en_IN”语言环境不会覆盖或补充基本“en”语言环境中的任何时区名称数据,其默认内容为“en_US”。

CLDR 21的发布在几天内就已经确定了。 这是正在被纳入ICU 49将在未来的OSX和iOS版本中被拿起。

  • 彼得E

– -编辑 – –

根据格式和规则的Unicode文档, V格式可能是一个更好的select:

…与z相同的格式,不同之处在于元区时区缩写将显示(如果可用),而不考虑[the] commonUsed [flag]的值。

在我的情况下,对于下面的代码:

 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:@"V"]; [dateFormatter setTimeZone:timeZone]; NSLog(@"V date string: %@", [dateFormatter stringFromDate:[NSDate date]]); 

我收到以下输出:

 V date string: IST