Created
June 14, 2013 23:15
-
-
Save Flushot/5786008 to your computer and use it in GitHub Desktop.
Functions for dealing with naive and aware dates
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 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