Skip to content

Instantly share code, notes, and snippets.

@Flushot
Created June 14, 2013 23:15
Show Gist options
  • Select an option

  • Save Flushot/5786008 to your computer and use it in GitHub Desktop.

Select an option

Save Flushot/5786008 to your computer and use it in GitHub Desktop.
Functions for dealing with naive and aware dates
import datetime
from dateutil import tz
def isAware(dt):
return dt.tzinfo is not None and \
dt.tzinfo.utcoffset(dt) is not None
def makeAware(dt, tzinfo=None):
if dt is None: return None
if isAware(dt): return dt # already aware
return dt.replace(tzinfo=tzinfo or tz.tzlocal())
def makeNaive(dt):
if dt is None: return None
if not isAware(dt): return dt # already naive
return datetime.datetime(dt.year, dt.month, dt.day,
dt.hour, dt.minute, dt.second,
dt.microsecond) # everything except tzinfo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment