Objective c 为什么我计算的时间戳不准确(+;32或+;16秒)?
我无法理解代码的结果,我需要你的帮助 例如,如果当前时间是凌晨5点,我想在调用我的方法时得到两个时间戳:Objective c 为什么我计算的时间戳不准确(+;32或+;16秒)?,objective-c,ios,timestamp,nsdate,nscalendar,Objective C,Ios,Timestamp,Nsdate,Nscalendar,我无法理解代码的结果,我需要你的帮助 例如,如果当前时间是凌晨5点,我想在调用我的方法时得到两个时间戳: [self getPeriodValues:@"day"]; [self getPeriodValues:@"year"]; 当日00时00分的时间戳 当前时间上午5点的时间戳 对于第一个时间戳,我有结果,但还有32秒 在我的第二个示例中,如果当前日期是2011/12/30上午5点,并且我在调用我的方法时需要两个时间戳: [self getPeriodValues:@"day"];
[self getPeriodValues:@"day"];
[self getPeriodValues:@"year"];
- 当日00时00分的时间戳
- 当前时间上午5点的时间戳
[self getPeriodValues:@"day"];
[self getPeriodValues:@"year"];
- 当前年份的时间戳,日期为2011/01/01,00时00分
- 当前日期和时间的时间戳:2011/12/30上午5点
-(void)getPeriodValues:(NSString*)period
{
log_debug("@@@@@ BackEnd - getPeriodValues @@@@@")
float startDateFloatTimeStamp;
float endDateFloatTimeStamp;
NSString * startDateStrNumericTimeStamp;
NSString * endDateStrNumericTimeStamp;
NSDateComponents *compsStart;
NSDateComponents *compsEnd;
NSDate * startDate;
NSDate * endDate;
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit| NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date] ;
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
//NSInteger week = [dateComponents week];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
log_debug (@"year : %ld", (long)year);
log_debug (@"month : %ld", (long)month);
log_debug (@"day : %ld", (long)day);
log_debug (@"hour : %ld", (long)hour);
log_debug (@"minute : %ld", (long)minute);
log_debug (@"second : %ld", (long)second);
log_debug (@"date : %ld", (long)[date timeIntervalSince1970]);
if ([period isEqualToString:@"day"]) {
// start at 00h00m32s of current day to current time
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:month];
[compsStart setDay:day];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:day];
[compsEnd setHour:hour];
[compsEnd setMinute:minute];
[compsEnd setSecond:second];
} else if ([period isEqualToString:@"yesterday"]) {
// Start yesterday at 00h00m32s, End today at 00h00m32s
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:month];
[compsStart setDay:day-1];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:day];
} else if ([period isEqualToString:@"week"]) {
// -7 days at 00h00m32s, End today at 00h00m32s
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:month];
[compsStart setDay:day-7];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:day];
} else if ([period isEqualToString:@"month"]) {
// ...
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:month];
[compsStart setDay:1];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:day];
[compsEnd setHour:hour];
[compsEnd setMinute:minute];
[compsEnd setSecond:second];
} else if ([period isEqualToString:@"previousMonth"]) {
// ...
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:month-1];
[compsStart setDay:1];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:1];
} else if ([period isEqualToString:@"year"]) {
// du 01/01 de cette année à 00h00m16s, à aujourd'hui pour l'heure actuelle
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:1];
[compsStart setDay:1];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:day];
[compsEnd setHour:hour];
[compsEnd setMinute:minute];
[compsEnd setSecond:second];
} else if ([period isEqualToString:@"previousYear"]) {
// ...
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year-1];
[compsStart setMonth:1];
[compsStart setDay:1];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:1];
[compsEnd setDay:1];
} else {
// ... same as day
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:month];
[compsStart setDay:day];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:day];
[compsEnd setHour:hour];
[compsEnd setMinute:minute];
[compsEnd setSecond:second];
}
startDate = [[NSCalendar currentCalendar] dateFromComponents:compsStart];
startDateFloatTimeStamp = [startDate timeIntervalSince1970];
endDate = [[NSCalendar currentCalendar] dateFromComponents:compsEnd];
endDateFloatTimeStamp = [endDate timeIntervalSince1970];
startDateStrNumericTimeStamp = [NSString stringWithFormat:@"%i000",(int) floor(startDateFloatTimeStamp)];
endDateStrNumericTimeStamp = [NSString stringWithFormat:@"%i000",(int) floor(endDateFloatTimeStamp)];
log_debug(@"Start timestamp : %@",startDateStrNumericTimeStamp);
log_debug(@"End timestamp : %@",endDateStrNumericTimeStamp);
...
}
- 对于"一天",我还有32秒
- 对于"昨天",我还有32秒
- 对于"一周",我还有32秒
- 对于"一个月",我还有32秒
- 对于"上个月",我还有32秒
- 对于"年",我还有16秒
- 对于"上一年",我还有16秒
-(void)getPeriodValues:(NSString*)period
{
log_debug("@@@@@ BackEnd - getPeriodValues @@@@@")
float startDateFloatTimeStamp;
float endDateFloatTimeStamp;
NSString * startDateStrNumericTimeStamp;
NSString * endDateStrNumericTimeStamp;
NSDateComponents *compsStart;
NSDateComponents *compsEnd;
NSDate * startDate;
NSDate * endDate;
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit| NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date] ;
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
//NSInteger week = [dateComponents week];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
log_debug (@"year : %ld", (long)year);
log_debug (@"month : %ld", (long)month);
log_debug (@"day : %ld", (long)day);
log_debug (@"hour : %ld", (long)hour);
log_debug (@"minute : %ld", (long)minute);
log_debug (@"second : %ld", (long)second);
log_debug (@"date : %ld", (long)[date timeIntervalSince1970]);
if ([period isEqualToString:@"day"]) {
// start at 00h00m32s of current day to current time
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:month];
[compsStart setDay:day];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:day];
[compsEnd setHour:hour];
[compsEnd setMinute:minute];
[compsEnd setSecond:second];
} else if ([period isEqualToString:@"yesterday"]) {
// Start yesterday at 00h00m32s, End today at 00h00m32s
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:month];
[compsStart setDay:day-1];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:day];
} else if ([period isEqualToString:@"week"]) {
// -7 days at 00h00m32s, End today at 00h00m32s
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:month];
[compsStart setDay:day-7];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:day];
} else if ([period isEqualToString:@"month"]) {
// ...
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:month];
[compsStart setDay:1];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:day];
[compsEnd setHour:hour];
[compsEnd setMinute:minute];
[compsEnd setSecond:second];
} else if ([period isEqualToString:@"previousMonth"]) {
// ...
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:month-1];
[compsStart setDay:1];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:1];
} else if ([period isEqualToString:@"year"]) {
// du 01/01 de cette année à 00h00m16s, à aujourd'hui pour l'heure actuelle
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:1];
[compsStart setDay:1];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:day];
[compsEnd setHour:hour];
[compsEnd setMinute:minute];
[compsEnd setSecond:second];
} else if ([period isEqualToString:@"previousYear"]) {
// ...
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year-1];
[compsStart setMonth:1];
[compsStart setDay:1];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:1];
[compsEnd setDay:1];
} else {
// ... same as day
compsStart = [[[NSDateComponents alloc] init] autorelease];
[compsStart setYear:year];
[compsStart setMonth:month];
[compsStart setDay:day];
compsEnd = [[[NSDateComponents alloc] init] autorelease];
[compsEnd setYear:year];
[compsEnd setMonth:month];
[compsEnd setDay:day];
[compsEnd setHour:hour];
[compsEnd setMinute:minute];
[compsEnd setSecond:second];
}
startDate = [[NSCalendar currentCalendar] dateFromComponents:compsStart];
startDateFloatTimeStamp = [startDate timeIntervalSince1970];
endDate = [[NSCalendar currentCalendar] dateFromComponents:compsEnd];
endDateFloatTimeStamp = [endDate timeIntervalSince1970];
startDateStrNumericTimeStamp = [NSString stringWithFormat:@"%i000",(int) floor(startDateFloatTimeStamp)];
endDateStrNumericTimeStamp = [NSString stringWithFormat:@"%i000",(int) floor(endDateFloatTimeStamp)];
log_debug(@"Start timestamp : %@",startDateStrNumericTimeStamp);
log_debug(@"End timestamp : %@",endDateStrNumericTimeStamp);
...
}
最后,为什么我的方法会增加32秒或16秒?
编辑以添加日志:
“日”案件:
我们可以看到,对于开始日,我有1325458816000,而我应该有:1325458800000我编译了这个,当我在方法底部打印startDate和endDate时,我看不到任何额外的秒数。你能以单元测试的形式提供一个断言失败的小例子吗?@Gary,我为你提供了de day案例的日志。我无法复制这个,你能不能提供一个断言失败的最小可能测试案例,以便我可以复制它。