0

次のメソッドは、ヘブライ年を指定して、NSHebrewカレンダーオブジェクトの1年の日数を計算しようとします。何らかの理由で、デバイス間で一貫性のない結果が得られています。テストケースは1996年5月25日の日付でした。あるデバイスは、1日短い別のデバイスよりも1日長く(正しい値)戻ってきました。

- (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ません。それでいいですか?露骨に間違っているものは他にありますか?

4

2 に答える 2

0

ベータテスター全体のデバイス間で同じ観察がありました。セルラーネットワークが異なれば、時報も異なります。愚かですが、彼らは請求に時間を使うので、セルラーネットワークで独自の信号を提供します。

iPodは、通常NTPサーバーによって設定されるホストマシンによって設定されます。それらはかなり同期しているはずです。

于 2011-04-18T07:16:29.797 に答える
0

このコードの問題は、NSDateComponentsの識別子を省略したことです。NSDateComponentsは、必要な識別子がないため、時間、分、秒を台無しにするため、デバイス間で年の長さが異なります。

于 2011-04-28T06:01:48.740 に答える