How to make timer for game? ex. hayday, clash of clan

I want to make regen hp. By time between user close app or pause app.

I make timestamp (get server time) when user pause or user quit app.

I want to know perfect condition about timer.

(Internet connection, Time Stamp)

Start Game > Check Network and Load TimeStamp > Calculate (Latest Time - TIme Stamp)

When Pause Game > Save TimeStamp but if user network fail it’s can’t load server time. (How to fix this ?) How to make like hayday or clash of clan ?

When Unpause > Check Network and Load TimeStamp > Calculate (Latest Time - TIme Stamp)

You can get the servertime upfront and then just check how much time has passed in the game(for when it started or how much time while it’s active) and add that time to the timestamp and save it. You can user server time first, but if it fails, this would be a backup.

I don’t play hayday or clash of clan, so don’t know their system.

I’m fairly certain those game don’t work unless you’re online. If they do, then they’re not synced to server time and they’re using local system time with some cheat detection once the user reconnects.