NSDateFormatter时区缩写
我需要识别datestring像Tue Aug 13 17:29:20 MSK 2013
。 而且,据我所知,要做到这一点,我需要在NSDateFormmater中使用'V'符号,但它只能识别GMT+04:00
像时区。 有没有其他方法来parsing时区缩写?
这里是代码:
NSDateFormatter *dft = [[NSDateFormatter alloc] init]; [dft setDateFormat:@"EEE MMM dd HH:mm:ss V yyyy"]; NSLog(@"%@", [dft stringFromDate:[NSDate date]]); NSLog(@"%@", [dft dateFromString:@"Tue Aug 13 17:29:20 MSK 2013"]);
输出:
Tue Aug 13 17:37:41 GMT+04:00 2013 (null)
用NSLog(@"%@", [dft dateFromString:@"Tue Aug 13 17:29:20 GMT+04.00 2013"])
输出很好。
根据http://www.openradar.me/9944011 ,苹果公司更新了NSDateFormatter所依赖的ICU库,在Lion / iOS 5时代的某个时候。 它不再处理大多数3字母的时区,特别是莫斯科时间(MSK),因为它们是不明确的,只有在为区域设置“cu”标志时才使用。
这意味着你要处理它。 例如:
NSString *threeLetterZone = [[dateString componentsSeparatedByString:@" "] objectAtIndex:4]; NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:threeLetterZone]; if (timeZone) { NSString *gmtTime = [dateString stringByReplacingOccurrencesOfString:threeLetterZone withString:@"GMT"]; date = [[pivotalDateFormatter dateFromString:gmtTime] dateByAddingTimeInterval:-timeZone.secondsFromGMT]; }
基于这个代码