Skip to content

Instantly share code, notes, and snippets.

@russelldavis
Created November 2, 2012 00:09
Show Gist options
  • Save russelldavis/3997763 to your computer and use it in GitHub Desktop.
Save russelldavis/3997763 to your computer and use it in GitHub Desktop.
Convert between UTC and local time without third party tz libraries
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