そのため、NSCalendar と NSDateComponents を使用して NSDate オブジェクトから日付コンポーネントを抽出すると、奇妙な動作に遭遇しました。日付が 1970 年から 0 秒の場合、週のコンポーネントは 53 を返します。これについての説明、またはモジュラス 52 の明らかな方法以外の修正方法はありますか?
マシンで実行してテストできるコードは次のとおりです。
-
(void)testDate {
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDateComponents *comp = [cal components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSWeekCalendarUnit) fromDate:date];
DLog(@"%d/%d/%d week: %d", [comp day],[comp month], [comp year], [comp week]);
}
出力は次のとおりです。
31/12/1969 week: 53
(gdb) po date
1970-01-01 00:00:00 +0000
(gdb)