以下方法尝试计算给定希伯来语年的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