测试已测试 | ✓ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2016年4月 |
由Ian Keen维护。
依赖关系 | |
IKCore | ~> 1.0 |
ISO8601 | >= 0 |
为 iOS 与 Mac 快速解析和写入 ISO8601 日期。
Carthage 是推荐安装 ISO8601 的方式。将以下内容添加到你的 Cartfile
github "soffes/ISO8601"
你也可以使用 CocoaPods 进行安装
pod 'ISO8601'
对于手动安装,我建议将项目作为子项目添加到你的项目或工作空间中,并将框架作为目标依赖项添加。
首先,导入适当的头文件
@import ISO8601; // Use #import <ISO8601/ISO8601.h> if you're using CocoaPods
该库使用 NSDateComponents
读取和写入。以下是一个例子
// Reading
NSDateComponents *dateComponents = [ISO8601Serialization dateComponentsForString:@"1999-05-19T23:55:21+09:00"];
// Writing
NSString *ISO8601String = [ISO8601Serialization stringForDateComponents:dateComponents];
有一个 NSDate
分类,方便转换
// Reading
NSDate *date = [NSDate dateWithISO8601String:@"1999-05-19T23:55:21+09:00"];
// Writing
NSString *ISO8601String = [date ISO8601String];
如果你需要对转换有更多控制或者需要知道输入的时区,可以使用分类提供的高级方法
// Reading
NSTimeZone *timeZone;
NSDate *date = [NSDate dateWithISO8601String:@"1999-05-19T23:55:21+09:00" timeZone:&timeZone usingCalendar:calendarOrNil];
// Writing
NSString *ISO8601String = [date ISO8601StringWithTimeZone:timeZoneOrNil usingCalendar:calendarOrNil];
NSDateComponents
是核心数据结构,因为 NSDate
未能很好地保存时区信息。
值得注意的是,NSDateComponents
中的一个值如果不在输入字符串中将是 nil
。例如,1999-05-19T23:55:21
将有一个 nil
时区,但 1999-05-19T23:55:21+00:00
和 1999-05-19T23:55:21Z
将有一个 UTC 时区。
该 +[NSDate dateWithISO8601String:]
导致将始终返回一个 UTC 日期。如果你需要一个其他时区的日期,你应该使用 +[NSDate ISO8601StringWithTimeZone:usingCalendar:]
(你可以传递 nil
作为日历参数来使用当前日历)。
尽情享受。