I have an android app. The application performs some tasks and updates the status of those tasks in local DB with the timestamp. Suppose the User logged an event. So an entry is added to event table with event_name and time_stamp. For timestamp, I get the current time in milliseconds, convert it to UTC time and save it in DB.
val cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"))
val t = cal.time.time
The application has a sync service running. This service basically maintains a timestamp which represents the time of the last sync. Service iterates all tables in DB finds the records whose timestamps are greater than the timestamp maintained by service and syncs those records to the server. The server in response to syncing returns its current time in UTC to sync service at the client app. And client app updates its timestamp value to the one provided by the server.
But the problem is when users change their current local time/time zone. In that case, the UTC time I get locally is not proper. So whenever some event is logged in local DB it is logged with a wrong timestamp. So when sync service starts iterating all tables it does not find correct records which are modified by the user since the last sync.
How do i get the proper UTC time stamp at client app even if user has changed the time locally?
I can not ask server to give its current time every time i go to save a record in local DB.
Should i get server time once my app starts and maintain a clock service which keeps track of elapsed time?
Or should i use some other parameter instead of time to keep track of modified rows?
How do I handle this problem?