-1

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.
Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55

1 Answers1

2

The example you're showing as failing actually is on the same day - this is down to comparing in UTC versus BST - the word date is just in the same day as 23:05 + 1hr = 00:05 = today.

You need to take care working with dates due to the timezones, if you want to know if something happened yesterday compared to now, a better approach might be:

NSDate *startOfDay;
NSTimeInterval lengthOfDayInSeconds;
BOOL success = [cal rangeOfUnit:NSCalendarUnitDay startDate:&startOfDay interval:&lengthOfDayInSeconds forDate:[NSDate date]];

This gives you a date for the start of today startOfDay (appropriate for the local calendar timezone), which you can then compare against your test date with a simple comparison.

Rob Glassey
  • 2,237
  • 20
  • 21
  • Hi, I've implemented it (been busy with my real job), and once it's tested I'll accept the answer. Does look like it should do the job though, so thanks! – Nicholas Smith Oct 22 '14 at 09:26