2

I need to recognize date strings like Tue Aug 13 17:29:20 MSK 2013. And, as I know, to do that I need to use 'V' symbol in NSDateFormmater, but it only recognizes GMT+04:00 like time zones. Is there any other way to parse time zone abbreviation?

Here is the code:

  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"]);

Output:

Tue Aug 13 17:37:41 GMT+04:00 2013
(null)

With NSLog(@"%@", [dft dateFromString:@"Tue Aug 13 17:29:20 GMT+04.00 2013"]) output is fine.

4

1 回答 1

2

根据http://www.openradar.me/9944011,Apple更新了 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];
 }

基于此代码

于 2013-08-13T13:58:16.860 回答