为什么NSDateFormatter会为这4个时区返回nil日期? [英] Why does NSDateFormatter return nil date for these 4 time zones?

查看:259
本文介绍了为什么NSDateFormatter会为这4个时区返回nil日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在iOS6中运行此功能(尚未测试iOS6之前):

Try running this in iOS6 (haven't tested pre iOS6):

NSDateFormatter *julianDayDateFormatter = nil;
julianDayDateFormatter = [[NSDateFormatter alloc] init];
[julianDayDateFormatter setDateFormat:@"g"];

for (NSString *timeZone in [NSTimeZone knownTimeZoneNames]) {
    julianDayDateFormatter.timeZone = [NSTimeZone timeZoneWithName: timeZone];
    NSDate *date = [julianDayDateFormatter dateFromString:[NSString stringWithFormat:@"%d", 2475213]];
    if (date == nil)
        NSLog(@"timeZone = %@", timeZone);
}

您将获得以下输出:

America/Bahia
America/Campo_Grande
America/Cuiaba
America/Sao_Paulo

有人可以解释为什么这四个时区的行为都是这样的,而NSDateFormatter设置为朱利安日数?所有其他时区使NSDateFormatter返回实际的NSDates。

Can anyone explain why these four time zones behave like this with NSDateFormatter set to julian day numbers? All other time zones makes NSDateFormatter return actual NSDates.

推荐答案

我有怀疑。只有怀疑,但非常强烈。

I have a suspicion. Only a suspicion, but a pretty strong one.

该值代表2064年10月19日。巴西时区观察夏令时从当地午夜开始 - 那是他们的时钟前进的时候,因此午夜本身并不存在。 10月19日是其中一个转换。

That value represents October 19th 2064. The Brazilian time zones observe daylight saving time starting at local midnight - that's when their clocks go forward, so midnight itself doesn't exist. October 19th is one of those transitions.

以下是一些使用Noda Time的示例代码,我的.NET日期/时间API。它检查它所知道的每个时区的当天开始时间是否实际是午夜:

Here's some sample code using Noda Time, my .NET date/time API. It checks whether the start of the day in every time zone it knows about is actually midnight:

using System;
using NodaTime;

class Test
{
    static void Main()
    {
        var localDate = new LocalDate(2064, 10, 19);
        var provider = DateTimeZoneProviders.Tzdb;
        foreach (var id in provider.Ids)
        {
            var zone = provider[id];
            var startOfDay = zone.AtStartOfDay(localDate).LocalDateTime.TimeOfDay;
            if (startOfDay != LocalTime.Midnight)
            {
                Console.WriteLine(id);
            }
        }
    }
}

那个产生一个非常相似的清单:

That produces a very similar list:

America/Bahia
America/Campo_Grande
America/Cuiaba
America/Sao_Paulo
Brazil/East

我怀疑巴西/东部可能是别名America / Sao_Paolo,这就是为什么它不在你的名单上。

I suspect Brazil/East may be an alias for America/Sao_Paolo, which is why it's not on your list.

无论如何,要回到你的朱利安日问题 - 我怀疑格式化程序总是想要返回 NSDate * 位于当地午夜。 2064年10月19日在那些时区中存在 ...因此它返回零。就个人而言,我建议它应该返回1am值,但是嘿......

Anyway, to get back to your Julian day issue - I suspect the formatter always wants to return an NSDate * which is at the local midnight. That doesn't exist for October 19th 2064 in those time zones... hence it returns nil. Personally I'd suggest it should return the 1am value instead, but hey...

这篇关于为什么NSDateFormatter会为这4个时区返回nil日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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