在可可中解析ISO8601日期的正确方法是什么? [英] What's the right way to parse an ISO8601 date in cocoa?

查看:77
本文介绍了在可可中解析ISO8601日期的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Cocoa中分析ISO8601日期,无论是对于iOS 4+还是OSX 10.6 +

I would like to parse ISO8601 dates in Cocoa, both for iOS 4+ and OSX 10.6+

在StackOverflow上有一些问题,我的意见包含良好的答案。这是我认为构成一个好的答案:

There are a few questions about this on StackOverflow already, but in my opinion none of them contain good answers. Here's what I think constitutes a good answer:


  1. 答案应该指向支持ISO8601的代码。

  1. The answer should point to code with support for ISO8601. This code should compile cleanly under XCode 4 for both iOS 4+ and OSX 10.6+.

代码应该支持所有 ISO8601日期格式。

The code should support all possible ISO8601 date formats.

请注意,这里有很多很多可能性。只需回答一个或两个格式字符串 NSDateFormatter 就不会剪了。

Please note that there are many, many possibilities here. Simply answering with one or two format strings for NSDateFormatter is not going to cut it.

此库。这是因为它是充满危险的32位的假设,它远比需要的复杂,它不能编译干净与XCode4 / Clang。底线:我根本不信任它!

The answer should not be this library. That's because it is riddled with dangerous 32-bit assumptions, it's far more complicated than necessary, and it doesn't compile clean with XCode4/Clang. Bottom line: I don't trust it at all!

谢谢你,Cocoa-ites的同事。我很高兴在这里找到一个真正的答案。

Thanks, fellow Cocoa-ites. I'm excited to find out if there's a real answer here!

推荐答案

最好的方法是此图书馆。 ☺

我应该在该页面上添加一个链接到 Bitbucket repo ,它包含较新的源代码(包括32位和ang修复!),并有一个问题跟踪。如果您发现任何其他错误,请将其提交。

I should add a link on that page to the Bitbucket repo, which contains newer source code (including 32-bit and Clang fixes!) and has an issue tracker. If you find any other bugs in it, please file them.

我也想知道您的意思是比需要更复杂。正常使用非常简单:

I'd also like to know what you mean by "more complicated than necessary". Normal usage is very simple:

ISO8601DateFormatter *formatter = [[[ISO8601DateFormatter alloc] init] autorelease]; //Or, if you prefer, create it once in -init and own it until -dealloc 
NSDate *parsedDate = [formatter dateFromString:inString];
NSString *unparsedString = [formatter stringFromDate:inDate];

您可以切换 dateFromString: code> stringFromDate:对于较长的方法之一,如果您需要更多信息(例如,保留时区)。

You can switch out dateFromString: or stringFromDate: for one of the longer methods if you need more information (e.g., to preserve the time zone).

如果你的意思是别的,我想听到它,所以我可以改进图书馆。

If you mean something else, I want to hear it so I can improve the library.

这篇关于在可可中解析ISO8601日期的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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