我有一個日期存儲爲1600年1月1日以來我需要處理的天數。這是一種遺留日期格式,我需要在我的應用程序中多次閱讀。自1600起至NSDate的日期?
以前,我一直在創建日曆,空日期組件和根日期如下:
self.gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar
] autorelease];
id rootComponents = [[[NSDateComponents alloc] init] autorelease];
[rootComponents setYear: 1600];
[rootComponents setMonth: 1];
[rootComponents setDay: 1];
self.rootDate = [gregorian dateFromComponents: rootComponents];
self.offset = [[[NSDateComponents alloc] init] autorelease];
然後,將整後轉換爲日期,我用這個:
[offset setDay: theLegacyDate];
id eventDate = [gregorian dateByAddingComponents: offset
toDate: rootDate
options: 0];
(我從來沒有在其他任何地方的偏移更改任何值。)
的問題是,我得到一個不同的時間rootDate
在iOS與Mac OS X的Mac OS X的,我越來越midn飛行。在iOS上,我得到8:12:28。 (到目前爲止,這似乎是一致的。)當我添加我的天數後,奇怪的時間停留。
OS | legacyDate | rootDate | eventDate ======== | ========== | ==========================|========================== Mac OS X | 143671 | 1600-01-01 00:00:00 -0800 | 1993-05-11 00:00:00 -0700 iOS | 143671 | 1600-01-01 08:12:28 +0000 | 1993-05-11 07:12:28 +0000
在我以前的產品發佈中,我並不在乎時間;現在我做了。爲什麼在iOS上奇怪的時間,我該怎麼辦呢? (我假設時差是DST。)
我試着將rootComponents的小時,分鍾和秒設置爲0.這沒有任何影響。如果我將它們設置爲0以外的值,它會將它們添加到8:12:28。我一直在想,這是否與閏秒或其他累積時鐘變化有關。
或者這是完全錯誤的方法來使用iOS?
這是一個好主意,但不幸的是它仍然會留下+/- 1小時的誤差。 – 2011-01-12 19:44:53