NSDateFormatter时区缩写

时间:2013-08-13 13:39:17

标签: ios objective-c nsdateformatter

我需要识别Tue Aug 13 17:29:20 MSK 2013之类的日期字符串。而且,据我所知,要做到这一点,我需要在NSDateFormmater中使用'V'符号,但它只识别GMT+04:00个时区。有没有其他方法来解析时区缩写?

以下是代码:

  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"])很好。

1 个答案:

答案 0 :(得分:2)

根据http://www.openradar.me/9944011,Apple在Lion / iOS 5时代更新了NSDateFormatter依赖的ICU库。并且它不再处理大多数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];
 }

基于this code

相关问题