2014-02-20 25 views
44

在Julian Day计算器上编写一些单元测试时,我发现1847年12月2日之前的日期被NSDate错误地初始化。他们似乎有75秒的时间。我无法找到指向那天的任何东西(这是格雷戈里日历截止之后)。这是一个错误还是有历史的日历调整,我没有遇到?额外的75秒来自哪里?

int main(int argc, const char * argv[]) 
{ 
    @autoreleasepool { 

     NSCalendar *cal = [NSCalendar currentCalendar]; 
     NSDateComponents *dateComps = [NSDateComponents new]; 
     dateComps.year = 1847; 
     dateComps.month = 12; 
     dateComps.day = 1; 
     NSDate *d1 = [cal dateFromComponents:dateComps]; 
     NSLog(@"d1 = %@", d1); 

     dateComps = [NSDateComponents new]; 
     dateComps.year = 1847; 
     dateComps.month = 12; 
     dateComps.day = 2; 
     NSDate *d2 = [cal dateFromComponents:dateComps]; 
     NSLog(@"d2 = %@", d2); 
    } 
    return 0; 
} 

输出:

D1 = 1847年12月1日○时01分15秒0000

D2 = 1847年12月2日00:00:00 0000

+0

我复制粘贴你的代码,没有任何改变,这是我的输出:' d1 = 1847-12-01 00:00:00 -064116 || d2 = 1847-12-02 00:00:00 -064116' – Merlevede

回答

92

根据http://www.timeanddate.com/worldclock/clockchange.html?n=136&year=1847,当时有75秒的时间向前移动。在当地时间即将到达1847年12月1日星期三12:00:00时,伦敦时钟被推进到1847年12月1日星期三上午12点01分15秒。

+0

啊哈!并猜测我在哪里......谢谢。 –

+26

支撑自己。 Upvotes即将到来.. –

0

NSDateComponents正在使用您当地的时区。尝试设置时区到UTC?

NSCalendar *cal = [NSCalendar currentCalendar]; 
NSDateComponents *dateComps = [NSDateComponents new]; 
dateComps.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; 
dateComps.year = 1847; 
dateComps.month = 12; 
dateComps.day = 1; 
NSDate *d1 = [cal dateFromComponents:dateComps]; 
NSLog(@"d1 = %@", d1); 

dateComps = [NSDateComponents new]; 
dateComps.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; 
dateComps.year = 1847; 
dateComps.month = 12; 
dateComps.day = 2; 
NSDate *d2 = [cal dateFromComponents:dateComps]; 
NSLog(@"d2 = %@", d2); 

[19875:60b] d1 = 1847-12-01 00:00:00 +0000 
[19875:60b] d2 = 1847-12-02 00:00:00 +0000 
+0

感谢尤金,我把你的代码与'UTC'和'GMT'(我在英格兰Turgis Green的时区)一起作为时区运行, 75秒的偏移量。其他时区也产生有趣的结果。美国东部时间似乎被抵消了不是5小时,而是4小时56分02秒。 PST由7h52m58s。我希望所有人都非常好奇,因为历史性的调整而不是奇怪的错误。 –

+0

秘密似乎是明确设置时区。奇怪的是,如果我在时区设置了'GMT',上面的代码按照'UTC'输出,但是如果我使用日期格式化程序设置为@“Z”,仍然可以看到75秒。 –

1

这是一个错误还是有历史的日历调整,我没有遇到过?

已经有很多次日历了... 修复了过去。

查看维基百科文章的“收养”部分,查看JulianGregorian日历。

但是,NSDate实例应该始终显示它初始化的任何时区的正确日期。

3

如果您正在进行任何类型的日期/时间操作,您需要The Calendar FAQ。它回答这个问题。

21

针对the post by Richard Krajunus,这里是由大多数计算机使用的zoneinfo database一些额外的信息来追踪这些种类的更改:

# From Paul Eggert (1993-11-18): 
# 
# Howse writes that Britain was the first country to use standard time. 
# The railways cared most about the inconsistencies of local mean time, 
# and it was they who forced a uniform time on the country. 
# The original idea was credited to Dr. William Hyde Wollaston (1766-1828) 
# and was popularized by Abraham Follett Osler (1808-1903). 
# The first railway to adopt London time was the Great Western Railway 
# in November 1840; other railways followed suit, and by 1847 most 
# (though not all) railways used London time. On 1847-09-22 the 
# Railway Clearing House, an industry standards body, recommended that GMT be 
# adopted at all stations as soon as the General Post Office permitted it. 
# The transition occurred on 12-01 for the L&NW, the Caledonian, 
# and presumably other railways; the January 1848 Bradshaw's lists many 
# railways as using GMT. By 1855 the vast majority of public 
# clocks in Britain were set to GMT (though some, like the great clock 
# on Tom Tower at Christ Church, Oxford, were fitted with two minute hands, 
# one for local time and one for GMT). The last major holdout was the legal 
# system, which stubbornly stuck to local time for many years, leading 
# to oddities like polls opening at 08:13 and closing at 16:13. 
# The legal system finally switched to GMT when the Statutes (Definition 
# of Time) Act took effect; it received the Royal Assent on 1880-08-02. 
# 
# In the tables below, we condense this complicated story into a single 
# transition date for London, namely 1847-12-01. We don't know as much 
# about Dublin, so we use 1880-08-02, the legal transition time. 

对不起使用该线程评论我无法回应; StackOverflow并不认为我值得这样。

+2

无论如何,这应该是一个答案,所以系统迫使你做正确的事情。成功! –