getDoubleForKey problem with chrono time

Dear Cocosers ,

I wanna make a limitation number for playing game, so I used getIntegerForKey to set/get it, which is no problem to set/get the limitation number to play.

but regard of it I wanna make the number be increases in regular inteverals like 30 seconds or a minute,
so I put getDoubleForKey function to init() like this:

double _dCurrentTime = duration(system_clock::now().time_since_epoch()).count();
double _dPastTime = _def->getDoubleForKey(“CURRENT TIME”, _dCurrentTime);
double _dur = _dCurrentTime - _dPastTime;
int n = (int)(_dur/10.0f);
m_nLimitation = m_nLimitation + n;

and I thought I should setDoubleForKey to the Exit so the idle time could be checked like this:

void MainMenu::onExit()
{
auto _defForDate = UserDefault::getInstance();

double _uCurrentTime =
//duration_cast
duration
(system_clock::now().time_since_epoch()).count();

_defForDate->setDoubleForKey(“CURRENT TIME”, _uCurrentTime);
_defForDate->flush();

log(“currentTime on Exit Menu: %lf”, _uCurrentTime);

Layer::onExit();

}

I tested following codes in update() function which called every 10 seconds, which is work properly:

double _dCurrentTime = duration (system_clock::now().time_since_epoch()).count();

But I found the “n” sometimes negative and sometimes even exceeded the actual duration when I restarted my game.
which means _dPastTime sometimes have bigger value than _dCurrentTime,
or the delta between _dCurrentTime and _dPastTime , for example, are not 10 seconds when I restart(moved to next scene then quit, and then restart) my game back
Did I something wrong?