The support ticket: one user can't log in, or every request fails with a TLS error, or their uploads are rejected with "request expired". Nobody else has the problem. The logs show nothing useful. After a long call it turns out the phone's date is set by hand, a few hours or a few years off.
Phones are usually right about the time, because they sync it from the network. But "usually" covers a lot of users: people who turned off automatic time to cheat a game timer, devices that booted without a network, test phones that sat in a drawer, and anyone who changed the time zone by hand instead of the time. Here's what breaks, and how to make the app tolerate it.
TLS fails before your code runs
Certificate validation checks the current date against the certificate's validity period. If the device thinks it's 2019, a certificate issued last month is "not yet valid". If it thinks it's 2031, the certificate has expired.
- iOS reports it as
NSURLErrorServerCertificateNotYetValid(-1204) orNSURLErrorServerCertificateHasBadDate(-1201). - Android usually shows an
SSLHandshakeException, with a cause that mentions the certificate not being valid yet or having expired, depending on the device and library.
To the user it looks like "no internet". If your error handling turns every networking error into "Check your connection", map these specific errors to a message that says the device's date and time look wrong. It's one of the few TLS errors the user can fix themselves.
Tokens that expire "early" or never
Access tokens (often JWTs) carry times: iat (issued at), nbf (not before), exp (expires).
- On the server, the clock is right, but a strict check with no leeway can reject a token whose
nbfis a second ahead because of skew between your own servers. A small leeway (often 30 to 60 seconds is used) is normal. - On the client, reading
expand comparing it with the device clock is where it goes wrong. A phone that's an hour fast thinks every new token has already expired and refreshes in a loop. A phone that's slow keeps sending expired tokens and relies on the401to recover.
Safer: don't trust the device clock for expiry decisions. Either use the expires_in value from the token response and count it down with a monotonic clock, or treat the server's 401 as the real signal and refresh then (once, not in a loop).
Signed requests and expiring URLs
Anything signed with a timestamp is checked against the server's clock:
- Request signing (HMAC over the method, path, body and a timestamp) typically allows a window of a few minutes. AWS Signature Version 4, for example, rejects requests whose time is too far from the server's, with an error like
RequestTimeTooSkewed. - Pre-signed upload or download URLs are usually created by the server, so they're fine, until the client decides on its own that a URL is "still valid for 10 minutes" using the device clock.
- One-time codes (TOTP) are computed from the current time. A device clock that's off by more than the allowed window produces codes that never work.
If the device builds any timestamp the server checks, use the server's time, not the device's.
Estimating server time
The server already tells you its time: every HTTP response normally carries a Date header.
- On a response, parse
Date(it's an HTTP date, with one-second resolution) and computeoffset = serverDate - deviceNow. - Store the offset together with a monotonic timestamp (
SystemClock.elapsedRealtime()on Android,ProcessInfo.processInfo.systemUptimeon iOS), not the wall clock, which the user can change at any moment. - When you need "now" for anything the server checks, use device time plus the offset.
- Ignore offsets of a second or two; they're latency and rounding. Log large ones, because "this user is 3 hours off" is very useful in a bug report.
Don't use this to "fix" the user's clock or to block the app. Just stop depending on it.
Dates in the payload
Separate from wrong clocks, date formatting on the device causes a steady stream of "works for me" bugs:
- Send timestamps in UTC with an explicit format, such as ISO 8601 with
Zor an offset (2026-11-25T09:30:00Z), or as epoch seconds. Not a local time without an offset. - iOS
DateFormatterwith a fixed format needslocale = Locale(identifier: "en_US_POSIX"). Without it, the user's 12/24-hour setting and calendar can change how the string is produced or parsed. Apple's technical Q&A on this exact problem has been around for years, and the bug still ships.ISO8601DateFormatteravoids it. - Android
SimpleDateFormatuses the default locale, which can produce non-ASCII digits or a different calendar in some locales. PassLocale.US(orLocale.ROOT) for machine-readable dates, or usejava.timewithDateTimeFormatter.ISO_INSTANT. - Time zone changes while the app is running (travel, or a manual change) can shift "today" boundaries. Compute "start of day" from the user's current time zone at the moment you need it, not one cached at launch.
Testing it
- Set the device clock wrong. Turn off automatic date and time, set it a day ahead, then a year behind. Open the app and walk through login, a normal request, an upload, and token refresh. Note which errors the user sees and whether any of them mention the date.
- Watch the requests. In a proxy or network inspector, look for timestamps the app builds itself (signature headers, query parameters, body fields) and compare them with the server's
Dateheader. - Change the locale and the 12/24-hour setting, then repeat anything that formats or parses dates. A device set to a region with a non-Gregorian calendar or non-ASCII digits is a good stress test.
- Simulators follow the host. The iOS Simulator takes its time from the Mac, so clock tests are easier on a real device or an Android emulator, where the date can be set separately.
This article was created with the help of AI.


