How to convert local time string to UTC?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Converting a local time string to UTC only works correctly if you know what local timezone the string belongs to. A bare string such as "2026-03-07 09:30:00" does not contain enough information by itself, because the same clock time means different UTC moments in different zones and can become ambiguous around daylight saving transitions.
The Key Rule: Parse First, Then Attach a Time Zone
You should think of the conversion in two stages:
- parse the string into a naive local date-time
- attach the correct timezone, then convert to UTC
If you skip the timezone step and assume the runtime will guess correctly, the conversion can easily be wrong.
Python Example with zoneinfo
In modern Python, zoneinfo is the standard library tool for named timezones.
This works because the timezone is explicit and the final conversion is done with timezone-aware values.
Why Offsets Are Better Than Guessing
If the input string already contains an offset such as -05:00, the conversion is much safer because the offset disambiguates the instant.
Whenever you control the data format, storing timestamps with an offset or directly in UTC is preferable to storing ambiguous local strings.
Daylight Saving Time Is the Real Trap
Some local clock times are ambiguous or even nonexistent.
Examples:
- during spring-forward, a local time may never occur
- during fall-back, one local clock time can refer to two different UTC instants
That is why a timezone name such as "America/Toronto" is more useful than a fixed offset alone when interpreting local business timestamps.
A Reusable Helper Function
Here is a practical helper for Python:
This is a better building block than scattering ad hoc parsing logic across the codebase.
If the String Means "System Local Time"
Sometimes the input is supposed to be interpreted in the current machine's local timezone. That is possible, but it is less predictable for distributed systems because two machines can produce different UTC results from the same string.
For server applications, it is usually better to pass the timezone explicitly rather than depending on host configuration.
Output Format Choices
After conversion, you can store or return UTC in several forms:
- ISO 8601 string
- Unix timestamp
- timezone-aware
datetime
Example Unix timestamp:
Pick one standard and use it consistently across the application.
Common Pitfalls
The biggest mistake is trying to convert a local string to UTC without knowing which timezone the string belongs to. Another is assuming that the machine's local timezone is always the intended one. Teams also frequently ignore daylight saving transitions and only discover problems during edge dates. Finally, storing ambiguous local strings instead of offset-aware or UTC timestamps creates avoidable complexity in every later processing step.
Summary
- A local time string cannot be converted safely to UTC without timezone context.
- Parse the string first, then attach the correct timezone explicitly.
- Use
zoneinfofor named timezones in Python. - Prefer storing offset-aware values or UTC to avoid ambiguity.
- Treat daylight saving transitions as a real data issue, not an edge case you can ignore.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.