Created
November 2, 2012 00:09
-
-
Save russelldavis/3997763 to your computer and use it in GitHub Desktop.
Convert between UTC and local time without third party tz libraries
This file contains 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
def utc_to_local(dt): | |
""" | |
Converts a naive datetime from UTC to local time, returning a new naive | |
datetime. | |
""" | |
return datetime.fromtimestamp(calendar.timegm(dt.utctimetuple())) | |
def local_to_utc(dt): | |
""" | |
Converts a naive datetime from local to UTC time, returning a new naive | |
datetime. | |
""" | |
return datetime.utcfromtimestamp(time_m.mktime(dt.timetuple())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment