为什么我计算的TimeStamp不准确(+32或+16秒)?

时间:2011-12-30 10:33:57

标签: objective-c ios timestamp nsdate nscalendar

我无法理解我的代码的结果,我需要你的帮助。

例如,如果当前时间是5H00 AM,我想在调用方法时获得两个时间戳

[self getPeriodValues:@"day"];
  • 当天00H00的时间戳。
  • 当前时间早上5点的时间戳。
  

对于第一个时间戳,我有结果,但还有32秒!

在我的第二个例子中,如果当前日期是2011年12月30日上午5点,当我调用我的方法时我想要两个时间戳

[self getPeriodValues:@"year"];
  • 当前年份的时间戳,日期为2011/01/01 00:00。
  • 当前日期和时间的时间戳:2011/12/30 at 5H00 AM。
  

对于第一个时间戳,我有结果,但还有16秒!

我的所有案例:

  • 对于@“day”我还有32秒。
  • 对于@“昨天”我还有32秒。
  • 对于@“周”我还有32秒。
  • 对于@“月”,我还有32秒。
  • 对于@“previousMonth”我还有32秒。
  • 对于@“年”,我还有16秒钟。
  • 对于@“previousYear”我还有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秒?

编辑以添加日志:

for @“day”case:

2012-01-02 09:09:43.855 TestApp[33070:207] -[BEServiceWeather getPeriodValues:] [Line 82] year : 2012
2012-01-02 09:09:43.856 TestApp[33070:207] -[BEServiceWeather getPeriodValues:] [Line 83] month : 1
2012-01-02 09:09:43.856 TestApp[33070:207] -[BEServiceWeather getPeriodValues:] [Line 84] day : 2
2012-01-02 09:09:43.857 TestApp[33070:207] -[BEServiceWeather getPeriodValues:] [Line 85] hour : 9
2012-01-02 09:09:43.857 TestApp[33070:207] -[BEServiceWeather getPeriodValues:] [Line 86] minute : 9
2012-01-02 09:09:43.858 TestApp[33070:207] -[BEServiceWeather getPeriodValues:] [Line 87] second : 43
2012-01-02 09:09:43.858 TestApp[33070:207] -[BEServiceWeather getPeriodValues:] [Line 88] date : 1325491783
2012-01-02 09:09:43.859 TestApp[33070:207] -[BEServiceWeather getPeriodValues:] [Line 232] Start timestamp : 1325458816000
2012-01-02 09:09:43.859 TestApp[33070:207] -[BEServiceWeather getPeriodValues:] [Line 233] End timestamp : 1325491840000

我们可以看到,对于开始的一天,我有1325458816000,我应该有:1325458800000

0 个答案:

没有答案