我们的游戏必须在每天24:00:00进行每日更新,代码如下:
Timer nextDayTimer(boost::asio::io_service& ios) {
time_t now = time(NULL);
struct tm tm = {0};
localtime_r(&now, &tm);
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
time_t next = mktime(&tm);
if (next <= now) {
tm.tm_mday++;
next = mktime(&tm);
}
Timer timer = Timer(new boost::asio::deadline_timer(ios));
int diff = (int) (diffTime(next, now) * 1000.0);
timer->expires_from_now(boost::posix_time::milliseconds(diff));
return timer;
}
然后在达到计时器到期的时间进行每日更新。然后再次致电nextDayTimer
;
然而,我发现它有一些问题,它偶尔会在24:00:00进行两次每日更新,这会导致什么原因?