Instead of doing a lot of date and time comparisons, just set the end date of the thing you want.

For example instead of checking to see if a token expires in an hour like this

- (BOOL)hasExpired
{
    if (self.tokenExpirationDate == nil) {
        return YES;
    }
    return [SPTMessageClockUtils oneHourHasPassedFrom:self.tokenExpirationDate];
}

Just set the expiration date on the object itself, and see if it is later than now like this.

- (BOOL)isValid
{
	return ([(NSDate *)[NSDate date] compare:self.expirationDate] == NSOrderedAscending);
}

This simplifies the computing and requires less coding to account for time checks and such.

Advertisements