简体   繁体   中英

NSTimeInterval, NSDate & int64_t Yields Unreasonable Time Calculation

I am doing what should be a simple calculation involving things like int64_t, NSTimeInterval and NSDate in order to keep track of the expiration of a token which expires within an hour of its acquisition and is refreshed (exchanged for a new one) every 50 minutes.

The code I have in place works most of the time, but every once in a while, (for example) it calculates that the time that has passed since the last token exchange was 16,000 days. Obviously something is very wrong somewhere, but I am just not seeing it.

Here is the code:

1) Get a new token, its expiration is given in seconds until the token expires, which is always 3600 seconds, or 1 hour. I take this value in seconds and add it to the current time in terms of seconds since 1970 and store this as an int64_t value in core data:

NSTimeInterval secondsSince1970  = [[NSDate date] timeIntervalSince1970];
NSTimeInterval secondsUntilExpirationFrom1970 = secondsSince1970  + [[responseObject objectForKey:@"expires_in"]doubleValue];

account.tokenExp = [self.dataManager updateOAuthTokenExpirationDateForAccount:account tokenExpiration:secondsUntilExpirationFrom1970]; //method that saves to managed object context

2) Now, every time a need to use the token, I first check to see if it is within 10 minutes or 600 seconds of expiring. If it is, I request a new one and if not, I don't.

- (BOOL)tokenRequiresRefreshForAccount:(Account*)account
{
  NSTimeInterval secondsSince1970 = [[NSDate date] timeIntervalSince1970];
  NSTimeInterval secondsUntilTokenExpiration = account.tokenExp - secondsSince1970;
                  //account.tokenExp is the saved expiration of the token (a double) in core data

  NSLog(@"There are %f minutes until the token expires and we %@ the token.", secondsUntilTokenExpiration / 60, secondsUntilTokenExpiration < 600 ? @"will refresh" : @"will not refresh");

  return secondsUntilTokenExpiration < 600;
}

As I said, the above log most recently returned a value that equated to 16,000 days having passed since the last check, this was logged 3 minutes after a log that said that there were 45 minutes remaining until the token expiration.

If anyone can see my error or has a better approach for acquiring the same result, I'd very much appreciate it.

UPDATE

I've seen some posts about 'underflowing' a number variable. The variable "secondsUntilTokenExpiration" is occasionally a rather negative number...is it possible that I am underflowing this variable? I have tried storing it as an int64_t and an NSTimeInterval and both yield occasionally (very) incorrect values. If this could be the problem, how should I be storing this value?

The first thing that comes to mind is to make sure one of the function calls used to derive the value doesn't drop the accuracy (NSDate, round, responseObject...). It's a bit of a stretch, but that's all I can see at the moment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM