Clock Sync (clock synchronization) is the process of aligning a device’s internal clock with a reference time source. In mobile applications, precise synchronization is critical for the correct operation of push notifications, SSL/TLS certificates, cryptographic protocols, and analytics. According to the Google Security Blog (2024), more than 30% of HTTPS connection failures on mobile devices are caused by system time desynchronization exceeding 5 seconds.
Key Takeaways
Clock synchronization (Clock Sync) is a mechanism for aligning a device’s internal clock with the reference UTC time (Universal Coordinated Time). Without synchronization, the quartz crystal oscillator in a mobile device gradually drifts — the drift is 1–10 seconds per day depending on temperature and component quality. Synchronization compensates for this drift by obtaining accurate time from external sources: NTP servers on the internet, GPS satellites, or cell towers. Ideally, a device should synchronize every 4–6 hours to maintain accuracy within 1 second.
A mobile device has two types of clocks: hardware (RTC, Real-Time Clock) with a separate battery backup — they continue running even when the device is off, and software (system time), managed by the operating system. Upon boot, the system time is initialized from RTC and then maintained via clock generator interrupts. NTP synchronization corrects system time, and in some cases, writes the correction to RTC as well. On Android, access to the hardware RTC is restricted — apps cannot modify it without root access.
Many aspects of mobile app operation critically depend on accurate system time. SSL certificates have validity periods: if the device’s time is set before the certificate issue date or after its expiration date, the HTTPS connection will be blocked. OAuth tokens and JWT authentication use timestamps to check expiration — desynchronization leads to false authorization failures. Push notifications are scheduled by time, and if the clock drifts, the user receives notifications at the wrong time or doesn’t receive them at all.
Application security also suffers from incorrect time: time-based encryption (time-based OTP), event logs with incorrect timestamps, incorrect rate-limiting on the server side (the server blocks “future” requests). According to the OWASP Mobile Top 10 (2024), distrust of system time falls into the category of insufficient platform security. Developers are advised to always check time on the server rather than rely solely on client clocks. If the discrepancy exceeds a threshold (5 seconds is recommended), the application should block critical operations until synchronization.
| Scenario | Effect of Desynchronization |
|---|---|
| HTTPS/TLS | Certificates are considered expired or invalid |
| OAuth 2.0 / JWT | Tokens are rejected as expired |
| Push notifications | Notifications arrive at the wrong time |
| Analytics | Events with incorrect timestamps distort reports |
| Cryptography | Time-based OTP doesn’t match the server |
| Rate limiting | Server blocks requests with “future” time |
The main protocols for clock synchronization are NTP and its simplified version SNTP. NTP (RFC 5905) is a full protocol with server filtering, drift analysis, and PLL correction. It is used on servers and network equipment. SNTP (RFC 4330) is a lightweight version for client devices that do not require constant synchronization. An SNTP client sends a request, receives a response, and sets the time without history analysis. On mobile devices, SNTP is specifically used — Android’s built-in Google Time Service (GTS) synchronizes via SNTP with time.google.com servers.
Besides NTP/SNTP, time synchronization on mobile devices is possible via GPS receiver (accuracy up to 10 ns in ideal conditions) and cellular network (via NITZ — Network Identity and Time Zone). GPS provides maximum accuracy but works only outdoors and consumes a lot of power. NITZ is provided by the cellular operator automatically upon network registration, but not all operators support it. Android uses a combination of all methods: GTS (SNTP) as priority, NITZ as backup, and GPS for applications requiring high accuracy.
In distributed systems — when the server and client are on different devices — clock synchronization faces fundamental limitations. Network latency makes it impossible to unambiguously determine the exact time on the client: if a packet took 200 ms, the time on the server at the moment of request and response is already different. NTP solves this problem through RTT measurement and statistical processing, but for distributed transactions (e.g., bank transfers), this is insufficient — logical clocks (Lamport timestamps) or vector clocks are used.
Physical clocks (wall clock) — real UTC time, synchronized via NTP. Logical clocks — ordinal numbers of events in the system, not tied to physical time. In distributed systems, vector clocks are often used for event ordering: each node stores a counter vector for all cluster nodes. For mobile applications, physical synchronization with 1–5 second accuracy is sufficient — this ensures correct operation of OAuth, SSL, and push notifications. If strict event ordering is required (e.g., in real-time chats), logical synchronization is added at the server level.
Implementing clock synchronization in an Android application can be done in several ways. The simplest is to get server time via REST API: the server returns a Unix Timestamp in the response body or in the HTTP Date header. This approach requires no additional libraries and guarantees the time matches the server. The second way is to use an SNTP client for direct queries to an NTP server. The third is to rely on Android Google Time Service, which automatically synchronizes system time if the device is connected to the internet.
In Android applications with authorization and financial operations, a combined approach is recommended: with each API request, the difference between server time and System.currentTimeMillis() is saved. This difference is applied to all time calculations on the client, regardless of whether the system clock is synchronized. This approach is called clock skew correction and is implemented through a class that stores the last known server difference. Additionally, background NTP synchronization can be run every 4–6 hours via WorkManager.
// Clock skew correction
class ClockSyncManager {
private var serverTimeDiff: Long = 0 // serverTime - deviceTime (ms)
fun updateServerTime(serverTimestampMs: Long) {
serverTimeDiff = serverTimestampMs - System.currentTimeMillis()
}
fun getCorrectedTime(): Long {
return System.currentTimeMillis() + serverTimeDiff
}
fun isSyncValid(maxDiffMs: Long = 5000): Boolean {
return Math.abs(serverTimeDiff) < maxDiffMs
}
}
For periodic background time synchronization on Android, use WorkManager with PeriodicWorkRequest. The synchronization task performs an SNTP request or REST API call, obtains server time, and updates ClockSyncManager. The minimum interval for PeriodicWorkRequest is 15 minutes, but for time synchronization, 4–6 hours is sufficient. When synchronizing, consider the network state — use NetworkType.CONNECTED to prevent unnecessary requests while roaming. If synchronization fails, save the previous correction — it remains valid with gradually decreasing accuracy.
Modern mobile devices synchronize time automatically through built-in services. On Android — Google Time Service (GTS), part of Google Play Services. On iOS — an NTP client built into the operating system. These services work independently of applications and require no additional configuration. The user can disable automatic synchronization in settings, creating a risk for apps — this is precisely when the developer needs to implement their own synchronization. It is recommended to check the auto-sync status via Settings.Global.getInt(AUTO_TIME) and warn the user when it is disabled.
| Platform | Synchronization Service | Protocol |
|---|---|---|
| Android | Google Time Service (GTS) | SNTP |
| iOS | Built-in NTP client | NTP |
| Cellular network | NITZ (carrier) | NITZ |
| GPS receiver | Satellite signal | GPS Atomic Time |
Relying solely on automatic synchronization is dangerous — the user may disable it or be in an area without internet. Best practice is to obtain time from the server with each API request and store the desynchronization in SharedPreferences or DataStore. For critical operations (payments, authorization, document signing), always check isSyncValid() before execution. If desynchronization exceeds the threshold — show the user a screen suggesting to enable auto-sync or wait for synchronization. For gaming and entertainment apps, it is sufficient to get time from the server on startup and update once an hour.
Frequently Asked Questions
Clock synchronization is the process of aligning a device’s system time with reference UTC. It works via NTP or SNTP protocols: the device sends a request to a server, measures network latency, and calculates a correction for its clock. The result is accurate time with an error of 1–100 ms depending on the network.
Without synchronization, failures are possible: SSL certificates block HTTPS, OAuth tokens are considered expired, push notifications arrive at the wrong time, analytics records incorrect timestamps. For critical operations (payments, authorization), desynchronization exceeding 5 seconds is considered a security threat and should block the operation.
The main ones are NTP (accuracy 1–50 ms, with filtering and PLL) and SNTP (10–100 ms, simplified). Additionally: GPS (10 ns, but only outdoors) and NITZ (via cellular carrier, accuracy ~1 second). Android uses Google Time Service on SNTP, iOS uses a built-in NTP client.
Use the Apache Commons Net library (NTPUDPClient class) for direct SNTP queries to time.google.com or pool.ntp.org. An alternative is to get server time from your API’s HTTP response headers. For continuous correction, implement a ClockSyncManager that stores the difference between server and local time.
Implement clock skew correction: with each API request, save the difference between server time and System.currentTimeMillis(). Use this difference for time correction in all application operations. If the difference exceeds 5 seconds — block critical transactions and suggest the user enable auto-sync in settings.
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.
Read also