I get different answers depending on which watch the following code is run on
val prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE)
val appClosedTime = prefs.getLong(KEY_APP_CLOSE_TIME, System.currentTimeMillis()) // default to now
val elapsedTime = System.currentTimeMillis() - appClosedTime // in milliseconds
The above code gives the right answer on both the Samsung Galaxy Watch 4 (Wear OS 3) and a Google Emulator (Wear Round API 30)
Unfortunately when the same code is run on the TicWatch (Wear OS 2) the elapsedTime includes the local time offset from UTC.
The following code can be used to adjust for the difference
val prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE)
val appClosedTime = prefs.getLong(KEY_APP_CLOSE_TIME, System.currentTimeMillis()) // default to now
val currentSystemTime = GregorianCalendar()
val localTimeOffsetFromUTC = currentSystemTime.timeZone.getOffset(appClosedTime)
val elapsedTime = System.currentTimeMillis() - (appClosedTime-localTimeOffsetFromUTC)
My code currently checks which watch I am using and makes the adjustment however this 'hack' is clearly impractical since it must be tested on each and every watch!
I am hoping someone can suggest something I am missing / overlooking / doing wrong ?
Code snippets and further information
System.currentTimeMillis() says it returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
appCloseTime is stored in onStop()
val editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit()
editor.putLong(KEY_APP_CLOSE_TIME, System.currentTimeMillis())
editor.apply()