2011-04-18 13 views
0

以下方法嘗試計算給定希伯來語年的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小,但我不知道。會這樣做嗎?還有什麼是公然不正確的嗎?

+0

一些夏令時可能? – stefanB 2011-04-18 00:35:16

+0

檢查設備時間和時區。 – 2011-04-18 00:54:05

+0

@HetalVora - 我檢查了兩者,我不認爲*(我可能是錯的),應該沒有什麼區別。在兩種情況下,我都將日期和時間歸一化爲零。 – Moshe 2011-04-18 00:56:06

回答

0

這段代碼的問題是我忽略了NSDateComponents的標識符。 NSDateComponents,沒有必要的標識符,會擾亂小時,分鐘和秒鐘,從而在不同設備間產生不同的年份。

0

在我的測試版測試人員之間的設備之間有相同的觀察。不同的蜂窩網絡是指不同的時間信號傻,但他們用他們的時間進行計費,所以他們在蜂窩網絡中提供他們自己的信號。

iPod由通常由NTP服務器設置的主機設置。這些應該很好地同步。

+0

對,但不是所有的運營商都有相同的*年*?另外,我手動設置我比較的日期對象上的時間。 – Moshe 2011-04-18 07:37:24

+0

這就是問題所在:應該。對於一些人來說,unix時間戳是一個神話,我遇到了具有不同的時間信號開始日期的移動提供商。最好的辦法是檢查/顯示你到達那裏。 – 2011-04-18 07:45:51

+0

你是說運營商使用希伯來語年? – Moshe 2011-04-18 07:46:57

相關問題