iOS NSDateFormatter即使是UTC也需要NSLocale [英] iOS NSDateFormatter needs NSLocale even it's UTC

查看:299
本文介绍了iOS NSDateFormatter即使是UTC也需要NSLocale的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑我无法理解为什么会这样,我向这个网站的众神求助:)

I have a doubt that I cannot understand why is the way it is and I appeal to the Gods of this site :)

我有这样的约会:

"1982-01-01T00:00:00Z" 

当我显示服务器发送的所有内容时(我知道,客户要求,不是好的做法...),我正在强制设备使用以下方法将该TimeZone设置为简化状态,而不进行错误检查,未优化,以及所有类似的东西:

As I'm displaying whatever the server sends (I know, customer requirement, not good practice...), I'm forcing the device to have that TimeZone with the following method, simplified without error checking, not optimized, and all that kind of things:

+ (NSString *) yearStringFromDate: (NSDate *) date
{
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"YYYY"];
    [formatter setTimeZone:[self timezoneForSIHF]];

    return [formatter stringFromDate:date];    
}

这应该是UTC,但如果我没有设置要获取的语言环境,则有时应该是错误的年份.因此,通过添加此数字,我可以得出所有情况下正确的年份:

This should be UTC, BUT if I don't set the locale I'm getting, and JUST sometimes the incorrect year. So by adding this I get the year correct for all cases:

[formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

有关更多信息,我正在使用不同的语言(en,de,es)在真实的设备和模拟器中进行测试.

For further info, I'm testing in a real device and simulator with different languages (en, de, es).

  • 这是为什么?
  • 即使timeZone正确,为什么语言环境也会影响日期?
  • 为什么有时需要处理某些日期,而有时却没有呢? 例如,1982年返回时未设置区域设置,1981年,如果我将其设置为1982年,1980年就不会发生这种情况,在两种情况下都返回1980年(或1987年,或...)
  • Why is this?
  • Why does the locale affect the date even though the timeZone is correct?
  • Why sometimes is working with some dates and sometimes it's not? For example, 1982 is returning without setting the locale, 1981 and if I set it 1982. This doesn't happen with 1980, returning in both cases 1980 (or 1987, or ...)

预先感谢您的所有答复:D 干杯!

Thanks in advance for all your replies :D Cheers!

推荐答案

将ISO 8601/RFC 3339日期字符串转换为NSDate对象时,如果用户不使用公历,则使用en_US_POSIX语言环境.参见技术问答1480 .

When converting ISO 8601/RFC 3339 date string to NSDate object, one uses the en_US_POSIX locale in case the user is not using a Gregorian calendar. See Technical Q&A 1480.

但是,在您的情况下,您尝试从日期对象获取年份字符串表示形式.在这种情况下,您可能不需要en_US_POSIX.这仅在您需要使用公历而不管设备当前可能使用哪种日历时才需要.

In your case, though, you are trying to get year string representation from date object. In that case you might not need en_US_POSIX. That's only necessary if you need to use Gregorian calendar regardless of what sort of calendar the device might currently be using.

但是,正如其他人所指出的,您应该使用yyyy而不是YYYY.前者返回日历年.后者会在基于年度"的日历中返回年份,该值可能并不总是与日历年相同.

As noted by others, though, you should be using yyyy and not YYYY. The former returns the calendar year. The latter returns the year, in "Week of Year" based calendars, which may not always be the same value as calendar year.

请参见日期格式设置对比yY的讨论.

顺便说一句,如果您确实需要日期的年部分,则还可以使用NSCalendar的与NSDateComponents相关的方法.

By the way, if you really need the year component of the date, you can also use the NSDateComponents related methods of NSCalendar.

这篇关于iOS NSDateFormatter即使是UTC也需要NSLocale的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆