在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: %@",[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?

解决方法

从 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

从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

– -编辑 – –

根据formats及其rules的unicode文档,V格式可能是更好的选择:

…the same format as z,except that Metazone timezone abbreviations are to be displayed if available,regardless of the value of [the] commonlyUsed [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

ios – NSDateFormatter不为“z”或“zzz”说明符显示“Asia / Kolkata”的时区缩写,只显示GMT偏移量的更多相关文章

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

    在iOS5模拟器和设备上,NSDateFormatter不为“z”或“zzz”说明符显示“Asia/Kolkata”的时区缩写.我希望上面的代码输出:但它输出:编辑将语言环境设置为印度语语言环境似乎没有帮助.我希望上面的代码输出:但它输出:这是一个错误吗?难道我做错了什么?

  2. android – jodatime如何知道夏令时是否开启

    我有一个需要时区的API.例如如果我在加利福尼亚州,当夏令时,而在夏令时关闭时,我需要通过-7.但是,我无法弄明白当前的日期是否开启或关闭夏令时.我已经安装了JodaTime,甚至在那里我找不到方法.请建议如果本机java或jodatime,或者有办法这样做.解决方法当您使用JodaTime创建DateTime时,您不需要传递偏移量.相反,通过时区.它将负责确定正确的偏移量,包括对DST的考虑.UPDATE我仍然不确定你在问什么,但也许你正在寻找其中之一:

  3. JS彻底弄懂GMT和UTC时区

    这篇文章主要介绍了JS彻底弄懂GMT和UTC时区,对时间感兴趣的同学,可以参考下

  4. centos – 当/ etc / localtime符号链接到/usr/share / zoneinfo / GMT时,为什么$date仍显示EST?

    我在服务器上设置日期时遇到问题…出于某种原因,虽然时间17:31:06是GMT,但时区后缀是EST…并且硬件时钟也搞砸了–10:32:12不是EST:如果我将hwclock设置为系统时间或本地时间,则会将其设置为错误:知道发生了什么事吗?

  5. CentOS中的UTC时区在哪里?

    每IANA’stimezonedatabase,Etc/UTC是显示名称为UTC的时区的说明符–即UTC.所以你的问题的前提是不正确的.

  6. Centos 修改本地时钟

    如果想把时钟的时区修改为本地时间,那么你需要做的是在terminal中输入如下命令:然后重启服务器就可以了或者使用timedatectl命令至于为什么是Asia/Shanghai,而不是Asia/Beijing。这个算是个历史遗留问题吧…

  7. 在PHP中将日期和时间转换为GMT,反之亦然……?

    我有一个PHP项目,我需要显示user1向另一个用户user2发送消息的时间和日期.当user2打开此消息时,必须根据他的时区显示时间和日期.由于两个用户都属于不同的时区,因此它显示的是USER1输入的相同日期和时间.例如:如果User1在2011年2月15日上午11:30在其时区发送消息,并且User2打开此消息,则必须根据其时区显示日期和时间.让我们说User2属于印度(格林尼治标准时间5:3

  8. php – 计划和DST

    我正在用PHP编写日历/日程安排应用程序.现在我采取你希望事件发生的那一天和时间.我还要求时区并相应调整以获得GMT的活动时间.然后我将该时间存储为从节目当天午夜开始的偏移量.这很好,效果很好,但是当我达到夏令时时会发生什么?

  9. Ubuntu获取系统时间

    #include#include#includeintgettimeofday;intmain{structtimevaltv;structtm*gmt,*area;while{gettimeofday;area=localtime;//以本地时区显示时间printf;//打印文件名和此段程序行数,并显示系统时间和微秒数;//printf;//gmt=gmtime;//以GMT时区显示时间//printf;//sleep;}return0;}

  10. 使用PHP将NULL插入到MySQL的最佳做法

    这里的地址,电话和备注可以是NULL值.我需要它,所以当我将null参数传递到我的函数中时,它在数据库中存储一个空值.我需要知道这样做最好的解决方案.谢谢.这是PHP解决方案,但是您必须使用MysqLi,因为MysqL不推荐使用,请阅读更多关于mysqli.另外,你必须考虑SQLinjectionDEMO

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部