NSDateFormatter不显示“Asia / Kolkata”的时区缩写。对于“z”或“zzz”或“zzz”。说明符,只是GMT偏移量 [英] NSDateFormatter doesn't show time zone abbreviation for "Asia/Kolkata" for the "z" or "zzz" specifier, just the GMT offset

查看:309
本文介绍了NSDateFormatter不显示“Asia / Kolkata”的时区缩写。对于“z”或“zzz”或“zzz”。说明符,只是GMT偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS5模拟器和设备上,NSDateFormatter不会为z或zzz说明符显示Asia / Kolkata的时区缩写。

On iOS5 simulator and device, NSDateFormatter doesn't show time zone abbreviation for "Asia/Kolkata" for the "z" or "zzz" specifier.

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"

我希望输出上面的代码:

I expect the above code to output:

IST
IST

但它输出:

GMT+05:30
IST

编辑

将区域设置设置为印度区域设置不会似乎有帮助。

Setting the locale to an indian locale doesn't seem to help.

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"

我希望输出上面的代码:

I expect the above code to output:

IST
IST

但它输出:

GMT+05:30
IST

这是一个错误吗?难道我做错了什么? 人们已经提到NSDateFormatter存在错误,尤其是在格式字符串中指定了时区时。这可能是其中一个错误吗?

Is this a bug? Am I doing something wrong? 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?

推荐答案

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


在iOS 5.0中解析缩写的时区名称的变化是开源ICU 4.8库
(以及它使用的开源CLDR 2.0数据)故意更改的
结果,修改后的版本
用于实现一些NSDateFormatter
功能。

The change in parsing of abbreviated time zone names in iOS 5.0 is a result of an intentional change in the open-source ICU 4.8 library (and the open-source CLDR 2.0 data that it uses), a modified version of which is used to implement some of the NSDateFormatter functionality.

问题是:使用z
指定的时区格式(= zzz)或v(= vvv),可能存在很多歧义。例如,东部时间的ET
可以应用于许多
不同区域的不同时区。为了改善格式和解析可靠性,
短格式仅用于区域设置为区域设置设置cu(常用)标志
。否则,只使用长格式(
格式化和解析)。

The issue is this: With the short timezone formats as specified by z (=zzz) or v (=vvv), there can be a lot of ambiguity. For example, "ET" for Eastern Time" could apply to different time zones in many different regions. To improve formatting and parsing reliability, the short forms are only used in a locale if the "cu" (commonly used) flag is set for the locale. Otherwise, only the long forms are used (for both formatting and parsing).

对于en语言环境(=en_US),为alzones设置cu标志,例如Alaska,America_Central,America_Eastern,America_Mountain,
America_Pacific,Atlantic,Hawaii_Aleutian和GMT
对于Europe_Central,设置为

For the "en" locale (= "en_US"), the cu flag is set for metazones such as Alaska, America_Central, America_Eastern, America_Mountain, America_Pacific, Atlantic, Hawaii_Aleutian, and GMT. It is not set for Europe_Central.

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

However, for the "en_GB" locale, the cu flag is set for Europe_Central.

因此格式化程序设置为短时区样式z或zzz和区域设置
en 或en_US不会解析CEST或CET,但如果区域设置是
而是设置为en_GB,则解析那些。GMT样式将是
已解析所有。

So a formatter set for short timezone style "z" or "zzz" and locale "en" or "en_US" will not parse "CEST" or "CET", but if the locale is instead set to "en_GB" it will parse those. The "GMT" style will be parsed by all.

如果格式化程序设置为长时区样式zzzz,并且
语言环境是en,en_US中的任何一个,或en_GB,然后将解析以下
中的任何一个,因为它们是明确的:太平洋夏令时
中欧夏令时中欧时间

If the formatter is set for the long timezone style "zzzz", and the locale is any of "en", "en_US", or "en_GB", then any of the following will be parsed, because they are unambiguous: "Pacific Daylight Time" "Central European Summer Time" "Central European Time"

希望这会有所帮助。


  • Peter Edberg

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


希思,
是的,你是对的,对于你上面提供的例子,
[dateFormatter stringFromDate:[NSDate date]] 使用短
时区名称IST。它不是由于ICU在
当前OSX和iOS版本中使用的CLDR数据版本中的en_IN区域设置数据中缺少
(分别为CLDR 1.9.1和2.0) )。
这些CLDR版本中的en_IN区域设置未覆盖或
补充基本en区域设置中的任何时区名称数据,
的默认内容为en_US。

Heath, Yes, you are correct, for the example you provided above, [dateFormatter stringFromDate:[NSDate date]] should use the short time zone name "IST". The fact that it does not is due to a deficiency in the "en_IN" locale data in the versions of CLDR data used by ICU in the current OSX and iOS releases (CLDR 1.9.1 and 2.0 respectively). The "en_IN" locale in those CLDR versions did not override or supplement any of the timezone name data from the base "en" locale, whose default content is for "en_US".

这已经为几天内发布的CLDR 21版本修复了。
即将被纳入ICU 49,将在
未来的OSX和iOS版本中获取。

This is already fixed for the CLDR 21 release coming in a few days. That is being incorporated into ICU 49 which will be picked up in future OSX and iOS releases.


  • Peter E

---编辑---

根据格式<的unicode文档/ a>和他们的规则 V 格式可能是更好的选择:

According to the unicode documentation on formats and their rules, the V format may have been a better choice:


...格式与z相同,但metazone时区除外如果可用,将显示缩写,无论[common] [flag]的值是多少。

...the same format as z, except that metazone timezone abbreviations are to be displayed if available, regardless of the value of [the] commonlyUsed [flag].

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

In my case, for the following code:

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

这篇关于NSDateFormatter不显示“Asia / Kolkata”的时区缩写。对于“z”或“zzz”或“zzz”。说明符,只是GMT偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆