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

发布于 2025-01-01 16:04:36 字数 1544 浏览 1 评论 0原文

在 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 有错误,特别是在格式字符串中指定时区时。这可能是这些错误之一吗?

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

but it outputs:

GMT+05:30
IST

EDIT

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

but it outputs:

GMT+05:30
IST

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2025-01-08 16:04:36

来自 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的一些部分
功能。

问题是这样的:使用 z 指定的时区格式
(=zzz) 或 v (=vvv),可能会有很多歧义。例如,“ET”
东部时间”可能适用于许多国家的不同时区
不同地区。为了提高格式化和解析的可靠性,
短格式仅在带有“cu”(常用)标志的语言环境中使用
是为语言环境设置的。否则,仅使用长形式(例如
格式化和解析)。

对于“en”区域设置(=“en_US”),为元区域设置了 cu 标志,例如
如阿拉斯加、America_Central、America_Eastern、America_Mountain、
美洲_太平洋、大西洋、夏威夷_阿留申和 GMT。 设置
欧洲中部。

但是,对于“en_GB”语言环境,cu 标志设置为
欧洲_中部。

因此,为短时区样式“z”或“zzz”和区域设置设置了格式化程序
“en”或“en_US”不会解析“CEST”或“CET”,但如果语言环境是
相反,设置为“en_GB”,它解析这些内容。 “GMT”风格将是
全部解析。

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

希望这有帮助。

  • 彼得·埃德伯格

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

希斯,
是的,你是对的,对于你上面提供的例子,
[dateFormatter stringFromDate:[NSDate date]] 应该使用短
时区名称“IST”。事实并非如此,这是由于缺陷造成的
在 ICU 使用的 CLDR 数据版本中的“en_IN”区域设置数据中
当前的 OSX 和 iOS 版本(分别为 CLDR 1.9.1 和 2.0)。
这些 CLDR 版本中的“en_IN”区域设置未覆盖或
补充来自基本“en”语言环境的任何时区名称数据,
其默认内容是“en_US”。

几天后发布的 CLDR 21 版本已修复此问题。
这将被纳入 ICU 49,并将在
未来的 OSX 和 iOS 版本。

  • 彼得·E

---编辑---

根据 格式及其规则V 格式可能是更好的选择:

...格式与 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

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

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.

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).

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.

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

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.

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"

Hope this helps.

  • Peter Edberg

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

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".

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

---Edit---

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

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

I receive the following output:

V date string: IST
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文