Created
May 23, 2014 09:25
-
-
Save gpiffault/89b46f92e47d4f2b7fc7 to your computer and use it in GitHub Desktop.
pytz: conversion of POSIX timestamp to python timezone-aware datetime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytz | |
def ts_to_datetime(ts, tz="UTC"): | |
utc_dt = datetime.datetime.utcfromtimestamp(ts).replace(tzinfo=pytz.utc) | |
tz = pytz.timezone(tz) | |
dt = tz.normalize(utc_dt.astimezone(tz)) | |
return dt | |
# Reverse function is dt.timestamp() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment