I'm using this code to compare two dates, to see if the stored date is yesterday essentially:
NSDate *date = [wordData objectForKey:@"date"];
NSLog(@"Word data date: %@", [wordData objectForKey:@"date"]);
NSDate *nowDate = [NSDate date];
NSLog(@"Date now: %@", nowDate);
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *date1Components = [cal components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
NSDateComponents *date2Components = [cal components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
NSComparisonResult comparison = [[cal dateFromComponents:date1Components] compare:[cal dateFromComponents:date2Components]];
if(comparison == NSOrderedAscending)
{
NSLog(@"Word out of date, fetching new date");
[self fetchCurrentWord];
}
if(comparison == NSOrderedDescending)
{
NSLog(@"Word is newer");
}
else if(comparison == NSOrderedSame)
{
NSLog(@"Same date");
}
However, when the date changes on device normally the code is returning NSOrderedSame
, if I simulate it by changing the device time manually it works, or if I use dateWithTimeIntervalSinceNow
it works. I've added debug prints and the dates look fine, they're originally created using NSDate so there should be no format differences. I'm drawing a blank on what it could actually be.
Failure log:
Oct 17 11:25:05 Nicholas-Smiths-iPhone WOTD[2493] <Warning>: Word is: CLIMATURE, A climate. [Obs.] Shak., 2014-10-16 23:05:16 +0000
Oct 17 11:25:05 Nicholas-Smiths-iPhone WOTD[2493] <Warning>: Word CLIMATURE, definition A climate. [Obs.] Shak.
Oct 17 11:25:05 Nicholas-Smiths-iPhone WOTD[2493] <Warning>: Word date: 2014-10-16 23:05:16 +0000
Oct 17 11:25:05 Nicholas-Smiths-iPhone WOTD[2493] <Warning>: Word data date: 2014-10-16 23:05:16 +0000
Oct 17 11:25:05 Nicholas-Smiths-iPhone WOTD[2493] <Warning>: Date now: 2014-10-17 10:25:05 +0000
Oct 17 11:25:05 Nicholas-Smiths-iPhone WOTD[2493] <Warning>: Same date
Success log:
2014-10-15 16:35:37.748 WOTD[960:31212] Word is: CONTORTUPLICATE, Plaited lengthwise and twisted in addition, as the bud of themorning-glory. Gray., 2014-10-15 14:43:04 +0000
2014-10-15 16:35:37.749 WOTD[960:31212] Word CONTORTUPLICATE, definition Plaited lengthwise and twisted in addition, as the bud of themorning-glory. Gray.
2014-10-15 16:35:37.749 WOTD[960:31212] Word date: 2014-10-15 14:43:04 +0000
2014-10-15 16:35:37.749 WOTD[960:31212] Word data date: 2014-10-15 14:43:04 +0000
2014-10-15 16:35:37.749 WOTD[960:31212] Date now: 2014-10-15 15:35:37 +0000
2014-10-15 16:35:37.750 WOTD[960:31212] Same date
I changed the time manually at this stage
2014-10-16 16:37:29.286 WOTD[1133:38254] Word is: CONTORTUPLICATE, Plaited lengthwise and twisted in addition, as the bud of themorning-glory. Gray., 2014-10-15 14:43:04 +0000
2014-10-16 16:37:29.287 WOTD[1133:38254] Word CONTORTUPLICATE, definition Plaited lengthwise and twisted in addition, as the bud of themorning-glory. Gray.
2014-10-16 16:37:29.287 WOTD[1133:38254] Word date: 2014-10-15 14:43:04 +0000
2014-10-16 16:37:29.287 WOTD[1133:38254] Word data date: 2014-10-15 14:43:04 +0000
2014-10-16 16:37:29.287 WOTD[1133:38254] Date now: 2014-10-16 15:37:29 +0000
2014-10-16 16:37:29.288 WOTD[1133:38254] Word out of date, fetching new date
2014-10-16 16:37:29.616 WOTD[1133:38538] Word PAC, definition A kind of moccasin, having the edges of the sole turned up andsewed to the upper. Knight.