我无法理解我的代码的结果,我需要你的帮助。
例如,如果当前时间是 5H00 AM,我想在调用我的方法时获得两个时间戳:
[self getPeriodValues:@"day"];
- 当天 00H00 的时间戳。
- 当前时间凌晨 5 点的时间戳。
对于第一个时间戳,我有结果,但还有 32 秒!
在我的第二个例子中,如果当前日期是 2011 年 12 月 30 日凌晨 5 点,并且我在调用我的方法时想要两个时间戳:
[self getPeriodValues:@"year"];
- 当前年份的时间戳,日期为 2011/01/01,位于 00H00。
- 当前日期和时间的时间戳:2011 年 12 月 30 日凌晨 5 点。
对于第一个时间戳,我有结果,但还有 16 秒!
我所有的案例:
- 对于@“day”,我还有 32 秒。
- 对于@“昨天”,我还有 32 秒。
- 对于@“week”,我还有 32 秒。
- 对于@"month",我还有 32 秒。
- 对于@"previousMonth",我还有 32 秒。
- 对于@"year",我还有 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 秒?
编辑以添加日志:
对于@“day”案例:
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