Skip to content

Instantly share code, notes, and snippets.

@cosmin
Created March 21, 2010 05:08
Show Gist options
  • Save cosmin/339106 to your computer and use it in GitHub Desktop.
Save cosmin/339106 to your computer and use it in GitHub Desktop.
# tzdate - helper functions for using timezones from dateutil. utcnow and localnow.
import datetime
from dateutil import tz
UTC_TZ = tz.gettz('UTC')
LOCAL_TZ = tz.gettz()
def utcnow():
return datetime.datetime.now(UTC_TZ)
def localnow():
return datetime.datetime.now(LOCAL_TZ)
def naive_to_tz(naive_date, target_tz):
b = list(naive_date.timetuple())[:-2]
b.append(LOCAL_TZ)
return datetime.datetime(*b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment