以下方法嘗試計算給定希伯來語年的NSHebrew日曆對象在一年中的天數。出於某種原因,我在各種設備上獲得了不一致的結果。測試案例是1996年5月25日的日期。一臺設備比另一臺設備短一天,它的返回時間更長(正確的值)。爲什麼NSDate會跨設備不一致?
- (NSInteger) lengthOfYearForYear:(NSInteger)year{
//
// Then get the first day of the current hebrew year
//
NSCalendar *hebrewCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];
NSDateComponents *roshHashanaComponents = [[[NSDateComponents alloc] init ]autorelease];
[roshHashanaComponents setDay:1];
[roshHashanaComponents setMonth:1];
[roshHashanaComponents setYear:year];
[roshHashanaComponents setHour:12];
[roshHashanaComponents setMinute:0];
[roshHashanaComponents setSecond:0];
NSDate *roshHashanaDate = [hebrewCalendar dateFromComponents:roshHashanaComponents];
//
// Then convert that to gregorian
//
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *gregorianDayComponentsForRoshHashana = [gregorianCalendar components:NSWeekdayCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:roshHashanaDate];
//Determine the day of the week of the first day of the current hebrew year
NSDate *oneTishreiAsGregorian = [gregorianCalendar dateFromComponents:gregorianDayComponentsForRoshHashana];
//
// Then get the first day of the next hebrew year
//
NSDateComponents *roshHashanaOfNextYearComponents = [[NSDateComponents alloc] init ];
NSInteger tempYear = year+1;
[roshHashanaOfNextYearComponents setDay:1];
[roshHashanaOfNextYearComponents setMonth:1];
[roshHashanaOfNextYearComponents setYear:tempYear];
[roshHashanaOfNextYearComponents setHour:12];
[roshHashanaOfNextYearComponents setMinute:0];
[roshHashanaOfNextYearComponents setSecond:0];
NSDate *roshHashanaOfNextYearAsDate = [hebrewCalendar dateFromComponents:roshHashanaOfNextYearComponents];
[roshHashanaOfNextYearComponents release];
[hebrewCalendar release];
//
// Then convert that to gregorian
//
NSDateComponents *gregorianDayComponentsForRoshHashanaOfNextYear = [gregorianCalendar components:NSWeekdayCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:roshHashanaOfNextYearAsDate];
//Determine the first day of the week of the next hebrew year
NSDate *oneTishreiOfNextYearAsGregorian = [gregorianCalendar dateFromComponents:gregorianDayComponentsForRoshHashanaOfNextYear];
// Length of this year in days
NSTimeInterval totalDaysInTheYear = [oneTishreiOfNextYearAsGregorian timeIntervalSinceReferenceDate] - [oneTishreiAsGregorian timeIntervalSinceReferenceDate];
//
// We round here because of slight offsets in the Gregorian calendar.
//
totalDaysInTheYear = round(totalDaysInTheYear/86400);
if(totalDaysInTheYear == 353 || totalDaysInTheYear == 383){
totalDaysInTheYear = 0;
}else if(totalDaysInTheYear == 354 || totalDaysInTheYear == 384){
totalDaysInTheYear = 1;
}else if(totalDaysInTheYear == 355 || totalDaysInTheYear == 385){
totalDaysInTheYear = 2;
}
return totalDaysInTheYear;
}
我認爲這可能是因爲我沒有使用NSHourCalendarUnit
小,但我不知道。會這樣做嗎?還有什麼是公然不正確的嗎?
一些夏令時可能? – stefanB 2011-04-18 00:35:16
檢查設備時間和時區。 – 2011-04-18 00:54:05
@HetalVora - 我檢查了兩者,我不認爲*(我可能是錯的),應該沒有什麼區別。在兩種情況下,我都將日期和時間歸一化爲零。 – Moshe 2011-04-18 00:56:06