
An imported timestamp such as 2026-11-19T09:00:00 looks complete until you ask where that 9 a.m. happened. If a script silently treats it as the server's local time, moving the script to another machine can change the meaning of the same input.
I would make the missing information visible rather than attach UTC just to stop an error. Python distinguishes a datetime with usable UTC-offset information from one without it. That distinction belongs in an import rule, not in a guess made halfway through a report.
Parse the value, then check its offset
For a source that promises offset-bearing ISO-style timestamps, this small function converts accepted values to aware UTC datetimes:
from datetime import datetime, timezone
def event_time_utc(text):
value = datetime.fromisoformat(text)
if value.utcoffset() is None:
raise ValueError('Timestamp needs an explicit UTC offset')
return value.astimezone(timezone.utc)
print(event_time_utc('2026-11-19T09:00:00-05:00').isoformat())
The output is 2026-11-19T14:00:00+00:00. The supplied offset says that the original clock reading is five hours behind UTC. Conversion changes the clock reading while preserving the instant.
This code uses Python's standard library. It checks for offset information after parsing, but it is not a strict validator for every API's timestamp grammar. If your source requires an exact format, enforce that contract separately. Invalid input can raise ValueError; handle that at the import boundary instead of dropping the record unnoticed.
Do not replace a conversion with a label change
These operations do different jobs:
value.astimezone(timezone.utc)
value.replace(tzinfo=timezone.utc)
Python documents astimezone() as adjusting the date and time to preserve the same UTC instant. In contrast, replace(tzinfo=...) attaches different timezone information without adjusting the clock fields.
For the offset-bearing example above, replacing the timezone with UTC would label the original 09:00 reading as 09:00 UTC. That is five hours earlier than the correct converted value of 14:00 UTC. A method call can succeed while the interpretation is wrong.
Attaching UTC can be appropriate when a source's documented contract already says its offset-free timestamps mean UTC. That is an explicit interpretation of known input, not a general repair for naive values. Keep that rule separate and record where it came from.
Missing timezone information is a data question
If your input has no offset and no documented timezone rule, the function intentionally refuses it. Do not substitute the laptop's timezone because it happens to be where the script runs.
Python allows astimezone() on naive datetimes and presumes they represent the system timezone. That convenience is exactly why the explicit check matters in a portable importer.
A named regional timezone is a different input contract from a numeric offset. Local clock times around daylight-saving changes can need extra information. This example handles timestamps with supplied offsets; it does not resolve ambiguous regional wall-clock times or implement recurring meeting rules.
Test the boundary, not only the happy path
I ran the example with the supplied negative offset and confirmed the 14:00 UTC result. I also passed the same timestamp without an offset and confirmed it raised the intended error. Finally, I compared timezone replacement with conversion and observed the two different clock readings described above. These are limited local checks, not tests of an external service.
Before accepting real records, add cases for your source's supported formats and its invalid-input behavior. Keep the original timestamp alongside any normalized value when your audit requirements call for it. A successful parse tells you the text was understood by a parser; a documented timezone rule tells you what event time it was supposed to mean.
Comments
Post a Comment