Iphone 为什么不同设备的NSDate不一致?
以下方法尝试计算给定希伯来年份的NSHebrew日历对象一年中的天数。由于某些原因,我在不同的设备上得到了不一致的结果。测试案例的日期为1996年5月25日。一台设备返回正确值的时间比另一台设备长一天,而另一台设备返回正确值的时间短一天Iphone 为什么不同设备的NSDate不一致?,iphone,objective-c,ios,nsdate,nscalendar,Iphone,Objective C,Ios,Nsdate,Nscalendar,以下方法尝试计算给定希伯来年份的NSHebrew日历对象一年中的天数。由于某些原因,我在不同的设备上得到了不一致的结果。测试案例的日期为1996年5月25日。一台设备返回正确值的时间比另一台设备长一天,而另一台设备返回正确值的时间短一天 - (NSInteger) lengthOfYearForYear:(NSInteger)year{ // // Then get the first day of the current hebrew year // NSCalendar *hebrew
- (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和更小的,但我不确定。这样行吗?还有什么明显不正确的吗?在我的测试版测试人员中,设备之间有相同的观察结果。不同的蜂窝网络意味着不同的时间信号。很傻,但是他们用自己的时间来计费,所以他们在蜂窝网络中提供自己的信号
iPod由主机设置,主机通常由NTP服务器设置。这些应该是非常同步的。这段代码的问题是我遗漏了NSDATE组件的标识符。NSDateComponents没有所需的标识符,会弄乱小时、分钟和秒,从而在不同的设备上产生不同的年长。可能会有一些夏令时的东西?检查设备的时间和时区。@HetalVora-我已经检查了这两个,我不认为我可能是错的,这不会有什么区别。在这两种情况下,我都将日期和时间标准化为零。它不应该是零。请发布您看到的不一致结果的代码…@amattn-添加了代码示例。谢谢是的,但是不是所有的航空公司都应该有相同的年份吗?另外,我正在手动设置我比较的日期对象的时间。这就是问题:应该。对于一些人来说,unix时间戳是一个神话,我遇到了一些手机提供商,他们的时间信号有不同的开始日期。最好是检查/显示您在那里得到的信息。您是说运营商使用的是希伯来年份吗?检查两个日期的unix时间戳是否相同。然后您就知道这是由于不同的日历和设置导致的转换错误还是实际日期的问题。