コードの結果を理解できません。助けが必要です。
たとえば、現在の時刻が午前 5 時である場合、メソッドを呼び出すときに 2 つのタイムスタンプを取得します。
[self getPeriodValues:@"day"];
- 00H00 での当日のタイムスタンプ。
- 午前 5 時の現在時刻のタイムスタンプ。
最初のタイムスタンプでは、結果が得られましたが、さらに 32 秒あります。
2 番目の例では、現在の日付が 2011 年 12 月 30 日の午前 5 時で、メソッドを呼び出すときに 2 つのタイムスタンプが必要な場合:
[self getPeriodValues:@"year"];
- 日付 2011/01/01 00H00 の現在の年のタイムスタンプ。
- 現在の日時のタイムスタンプ: 2011/12/30 at 5H00 AM.
最初のタイムスタンプでは、結果が得られましたが、さらに 16 秒あります。
私のすべてのケース:
- @"day" の場合、あと 32 秒あります。
- @"yesterday" の場合、あと 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 が必要です。