I have this method that deletes all the objects in the dictionary if a new day has begun:
-(void)flushDictionaryIfNeeded{
NSLog(@"Flush called");
if([self.messagedTodayDict count]>0) {
//get any date
NSDate *aDate = nil;
NSArray *values = [self.messagedTodayDict allValues];
aDate = [values objectAtIndex:0];
NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:aDate];
NSDateComponents *today = [[NSCalendar currentCalendar] components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
NSLog(@"Today is %@",[NSDate date]);
NSLog(@"Date in the dicitonary is %@",aDate);
if([today day] == [otherDay day] &&
[today month] == [otherDay month] &&
[today year] == [otherDay year] &&
[today era] == [otherDay era]) {
NSLog(@"Don't flush");
}
else {
NSLog(@"It's a new day! erase dictionary!");
[self.messagedTodayDict removeAllObjects];
}
}
}
I live in Taiwan, the NSLog for any date I print is 8 hours behind the time here. I call this method five minutes before midnight (Taiwan time) I get this:
Today is 2015-04-03 15:55:09 +0000
Date in the dicitonary is 2015-04-03 15:09:09 +0000
This makes sense, dates stored as 8 hours behind and the dictionary doesn't flush/delete all objects.
Now I call the method at midnight:
Today is 2015-04-03 16:00:22 +0000
Date in the dicitonary is 2015-04-03 15:09:09 +0000
and the dictionary deletes all the objects. Why does it do this if I am comparing date components below? The month and day are the same for today and the date in the dictionary. Don't really understand what is happening here. Some pointers would be really appreciated.