简体   繁体   English

NSTimeInterval,NSDate和int64_t产生不合理的时间计算

[英]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. 我在做什么应该涉及之类的东西的int64_t,NSTimeInterval和NSDate的,以跟踪其到期收购的一个小时内,并刷新令牌(换一个新的交换)每50分钟过期的简单计算。

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. 我拥有的代码通常在大多数时间都能正常工作,但是每隔一段时间(例如)它计算一次,自上一次令牌交换以来已过去的时间为16,000天。 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. 1)获取一个新令牌,令牌的到期时间以秒为单位,直到令牌到期,通常为3600秒或1小时。 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: 我以秒为单位获取此值,并将其以1970年以来的秒数为单位添加到当前时间,并将其作为int64_t值存储在核心数据中:

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. 2)现在,每次需要使用令牌时,我首先检查它是否在到期的10分钟或600秒内。 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. 正如我所说,上述日志最近返回的值等于自上次检查以来已过去16,000天,该值在日志中说3分钟后才记录,直到令牌到期为止还有45分钟。

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? 变量“ secondsUntilTokenExpiration”有时是一个相当负的数字...我是否有可能在此变量下溢? I have tried storing it as an int64_t and an NSTimeInterval and both yield occasionally (very) incorrect values. 我尝试将其存储为int64_t和NSTimeInterval,并且两者都偶尔(非常)产生不正确的值。 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...). 首先想到的是确保用于导出值的函数调用中的一个不会降低精度(NSDate,Round,responseObject ...)。 It's a bit of a stretch, but that's all I can see at the moment. 这有点费力,但这就是我目前能看到的全部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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