NSDateFormatter 时区缩写 [英] NSDateFormatter time zone abbreviation
问题描述
我需要识别日期字符串,例如 Tue Aug 13 17:29:20 MSK 2013
.而且,据我所知,要做到这一点,我需要在 NSDateFormmater 中使用V"符号,但它只能识别 GMT+04:00
之类的时区.还有其他解析时区缩写的方法吗?
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?
代码如下:
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,Apple 在 Lion/iOS 5 时代的某个时间更新了 NSDateFormatter 所依赖的 ICU 库.并且它不再处理大多数 3 个字母的时区,特别是MSK"(莫斯科时间),因为它们是不明确的,只有在为语言环境设置了cu"标志时才使用它们.
according to http://www.openradar.me/9944011, Apple updated the ICU library which NSDateFormatter depends on, some time during the Lion/iOS 5 era. and it no longer handles most 3-letter timezones like "MSK" in particular (Moscow Time) because they're ambiguous, they only are used if the "cu" flag is set for the locale.
这意味着您可以处理它.例如:
Which means that you get to handle it. For example with this:
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];
}
基于此代码
这篇关于NSDateFormatter 时区缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!