question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getCurrentTimeMs() return the wrong date time everytime I manually change the device clock

See original GitHub issue

If I do something like:

  • Open my app
  • Minimize it and go to System > Date & Time and then change the device clock for some wrong date & time
  • Then come back to my app and try to call getCurrentTimeMs() this will give me the wrong date that I manually setup

If I have an internet connection, after that wrong response the next one works as expected, giving me the right date & time.

If that is expected behavior could you please explain a little bit more why this happens? And what do you recommend to minimize that behavior?

I am now calling kronosClock.syncInBackground(); every time my app goes to the foreground, please let me know if there is any other better strategy to deal with it.

Thank you in advance, and congrats for the work, apart from that minor issue this library is great!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:10

github_iconTop GitHub Comments

4reactions
Rajin9601commented, Feb 11, 2022

I think the problem is related to the way that kronos-android detects whether the system has been rebooted. https://github.com/lyft/Kronos-Android/blob/b2774ab9458ffd0202e2c4880e2a181134953beb/kronos-java/src/main/java/com/lyft/kronos/internal/ntp/SntpClient.java#L260-L269

The library uses deviceElapsedTimestampMs from SystemClock in android as the source of local time. However, this value will be reset when the system is booted. So library needs to know whether the system has been rebooted since it got NTP time from the servers.

To detect the reboot, library uses difference between deviceCurrentTimeMillis and deviceElapsedTime. If the diff has changed significantly, it assumes that the device is rebooted. So if you change the device clock and open the app, the diff will change and library thinks that the device is rebooted and throw away the cache. (Which is ok since most users don’t change the system clock & even when they do, kronos will try to synchronize and eventually get the NTP time)


I cannot think of other way of detecting boot without changing the Clock interface in kronos. I think using Settings.Global#BOOT_COUNT will fix this problem.

4reactions
jh0719commented, Feb 3, 2022

I am also experiencing strange behavior with this testing flow. kronos will have a valid NTP time and return it to me. When I minimize and manually set phone time, and return to my app, kronos appears to have cleared the NTP time offsets for some reason, and now I am being returned the incorrect phone time.

I expected the whole point of this library was to maintain the offset between RTC clock and NTP time and thus still be able to give me NTP time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

System.currentTimeMillis() returns wrong date - Stack Overflow
System.currentTimeMillis() returns the number of milliseconds since 1970-01-01 00:00:00 UTC. UTC is the common time standart in the world.
Read more >
How to Change Time and Date in Windows 10 and Fix Wrong ...
1. You can right-click the clock on your taskbar to open a context menu with various Windows options. 2. Click "Adjust date/time" to...
Read more >
Why is My Automatic Date and Time Wrong on Android? 5 ...
Tap Date and Time option; Tap Automatic Home Clock button. At the same time, try disabling Automatic time zone. Your device may think...
Read more >
A Guide to Handling Date and Time for Full Stack JavaScript ...
Learn how to work with date and time values in JavaScript.
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
now() to directly get the time stamp without having to create a new Date object. Parsing a Date. Converting a string to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found