Nsdateformatter + Timezone issue

2019-01-27 04:02发布

I am trying to parse this date timestamp Begin to string:

Wed, 11 Sep 2013 08:51:41 EEST

This issue with only

EEST

, I have tried z or zzz or V, nothing happened. Date formatter always getting NULL.

While I am cutting EEST from string, everything goes OK.

Could anyone suggest, how to solve this issue?

Thanks

UPDATE:

dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_EN_POSIX"]];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EEST"]];
[dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss V"];
dateFromString = [dateFormat dateFromString:beginString];

2条回答
再贱就再见
2楼-- · 2019-01-27 04:41

Your solution to this problem is to change the locale to en_GB and the date formatter will be able to parse your date string properly.

Here is the explanation from the Apple developer bug reporting team in reply to radar #9944011:

This is an intentional change in iOS 5. The issue is this: With the short formats as specified by z (=zzz) or v (=vvv), there can be a lot of ambiguity. For example, "ET" for Eastern Time" could apply to different time zones in many different regions. To improve formatting and parsing reliability, the short forms are only used in a locale if the "cu" (commonly used) flag is set for the locale. Otherwise, only the long forms are used (for both formatting and parsing). This is a change in open-source CLDR 2.0 / ICU 4.8, which is the basis for the ICU in iOS 5, which in turn is the basis of NSDateFormatter behavior.

For the "en" locale (= "en_US"), the cu flag is set for metazones such as Alaska, America_Central, America_Eastern, America_Mountain, America_Pacific, Atlantic, Hawaii_Aleutian, and GMT. It is not set for Europe_Central.

However, for the "en_GB" locale, the cu flag is set for Europe_Central.

So, a formatter set for short timezone style "z" or "zzz" and locale "en" or "en_US" will not parse "CEST" or "CET", but if the locale is instead set to "en_GB" it will parse those. The "GMT" style will be parsed by all.

If the formatter is set for the long timezone style "zzzz", and the locale is any of "en", "en_US", or "en_GB", then any of the following will be parsed, because they are unambiguous:

"Pacific Daylight Time" "Central European Summer Time" "Central European Time"

查看更多
相关推荐>>
3楼-- · 2019-01-27 04:49

Try this instead:

NSString *beginString = @"Wed, 11 Sep 2013 08:51:41 EEST";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-GB"]];
[dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSDate *dateFromString = [dateFormat dateFromString:beginString];

See details & explanation here

查看更多
登录 后发表回答