首发于公众号
自 iOS10 和 macOS 10.12 起,系统提供了 NSISO8601DateFormatter 类来处理 ISO8601 和 RFC 3339 格式的时间字符串。
不仅对最低版本有要求,而且对于时间中带有毫秒的格式还需要把最低系统版本支持提升到 iOS 11/macOS 10.13,确实很坑。
其实使用 NSDateFormatter 也可以处理,只需要定义好 dateFormat 的格式与 ISO 8601/RFC 3339 一致就可以了,而且还没有系统版本的限制。
仿照 NSISO8601DateFormatter 的接口,基于 NSDateFormatter 自定义一个自己的类:
@interface SFISO8601DateFormatter : NSDateFormatter
@property(nonatomic, assign) SFISO8601DateFormatOptions formatOptions;
@end
formatOptions 的定义直接照搬 NSISO8601DateFormatOptions,为了避免版本警告,改一下前缀和去掉 API_AVAILABLE 宏。
具体怎么根据 formatOptions 定义 dateFormat 的格式,官方的 NSISO8601DateFormatOptions 定义里的注释写的很清楚了,只需要简单的依样画葫芦就好,代码比较简单:
- (NSString *)dateFormatFormOptions {
NSString *year = @"", *month = @"", *weakOfYear = @"",
*day = @"", *hour = @"", *minute = @"", *second = @"", *fractionalSeconds = @"", *timeZone = @"";
NSString *dateAndTime = @"", *inDate = @"", *inTime = @"";
if (BIT_TEST(_formatOptions, SFISO8601DateFormatWithWeekOfYear)) {
year = @"YYYY";
day = @"ee";
weakOfYear = @"'W'ww";
}
if (BIT_TEST(_formatOptions, SFISO8601DateFormatWithYear)) {
if (year.length == 0) year = @"yyyy";
}
if (BIT_TEST(_formatOptions, SFISO8601DateFormatWithMonth)) {
month = @"MM";
if (day.length == 0) day = @"dd";
}
if (BIT_TEST(_formatOptions, SFISO8601DateFormatWithDay)) {
if (day.length == 0) day = @"DDD";
}
if (BIT_TEST(_formatOptions, SFISO8601DateFormatWithTime)) {
hour = @"HH";
minute = @"mm";
second = @"ss";
dateAndTime = @"'T'";
if (BIT_TEST(_formatOptions, SFISO8601DateFormatWithFractionalSeconds)) {
fractionalSeconds = @".SSS";
}
if (BIT_TEST(_formatOptions, SFISO8601DateFormatWithColonSeparatorInTime)) {
inTime = @":";
}
if (BIT_TEST(_formatOptions, SFISO8601DateFormatWithSpaceBetweenDateAndTime)) {
dateAndTime = @" ";
}
}
if (BIT_TEST(_formatOptions, SFISO8601DateFormatWithTimeZone)) {
if (BIT_TEST(_formatOptions, SFISO8601DateFormatWithColonSeparatorInTimeZone)) {
timeZone = @"ZZZZZ";
} else {
if (self.timeZone.secondsFromGMT != 0) {
timeZone = @"ZZZ";
} else {
timeZone = @"ZZZZZ";
}
}
}
if (BIT_TEST(_formatOptions, SFISO8601DateFormatWithDashSeparatorInDate)) {
inDate = @"-";
if (weakOfYear.length > 0) {
weakOfYear = [NSString stringWithFormat:@"-%@-", weakOfYear];
} else {
weakOfYear = inDate;
}
}
NSString *dateFormat = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@%@%@%@%@%@",
year, inDate, month, weakOfYear, day, dateAndTime,
hour, inTime, minute, inTime, second, fractionalSeconds, timeZone];
return dateFormat;
}
代码写好,测试一下结果:
SFISO8601DateFormatter *df = [[SFISO8601DateFormatter alloc] init];
df.formatOptions = SFISO8601DateFormatWithYear |
SFISO8601DateFormatWithMonth |
SFISO8601DateFormatWithDay |
SFISO8601DateFormatWithDashSeparatorInDate |
SFISO8601DateFormatWithTime |
SFISO8601DateFormatWithColonSeparatorInTime |
SFISO8601DateFormatWithFractionalSeconds |
SFISO8601DateFormatWithTimeZone;
df.timeZone = [NSTimeZone systemTimeZone];
NSDate *date = [df.copy dateFromString:@"2006-06-18T12:30:58.616+0800"];
Xcode 编译运行,查看一下 date 的输出结果:
转换后的日期和转换前的完全一样。
代码已开源在 github