Handling timestamps and timezone conversion in Python

If you are handling timestamps where a source timezone is not known (i.e. if UTC is not being assumed in something like a logfile or spreadsheet, and the desired output is also not UTC), the pytz package in Python is a go-to for handling timezone data.

Given a timestamp like 01-25-2018 02:17 AM and you know the source for this data is US/Central on a particular point of presence, for example, on an access log, but you also know that data will be consumed by someone else in another time zone, you can use the pytz package to enforce that source timezone, and then convert the time based on the timezone you’d like it to be converted for.

You can operationalize this in a function:

def timeFmt(timeEntry):
inputTime = parse(timeEntry).replace(
tzinfo=pytz.timezone("US/Central")
)
convTime = inputTime.astimezone(pytz.timezone("US/Mountain"))
return convTime

which would return convTime for the new timezone.

What I like about this Python package is that many different formats are included as modules, so to return the time in ISO format:

def timeFmt(timeEntry):
inputTime = parse(timeEntry).replace(
tzinfo=pytz.timezone("US/Pacific")
)
convTime = inputTime.astimezone(pytz.timezone("US/Eastern"))
fmtTime = convTime.isoformat()
return fmtTime

where fmtTime returns your timestamp in ISO format.

More commonly, if your use case does not require a specific geographic use case (i.e. if you want to process local timestamps in their physical timezone, but want consumption of those times to be posted in UTC), to use this module to provide a UTC timestamp, you might replace convTime with something like:

"US/Pacific".normalize("US/Pacific".localize(inputTimes)).astimezone(pytz.utc)

to complete the conversion to UTC before doing any other text processing on the time data.

You can use something like:

datetime.now(tzlocal()).tzname()

or

time.tzname

or if your timezone is enforced as an environmental variable:

os.environ['TZ']

to retrieve your local timezone (or the timezone of the node the timestamps are collected from) for processing upstream in a function like the following to normalize it across the rest of your codebase while preserving the source if you use local time on your systems in different locations.