Last active
January 3, 2016 04:59
-
-
Save jeffbryner/8412843 to your computer and use it in GitHub Desktop.
UTC date from anything
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
from datetime import datetime | |
from dateutil.parser import parse | |
import pytz | |
def toUTC(suspectedDate,localTimeZone="US/Pacific"): | |
'''make a UTC date out of almost anything''' | |
utc=pytz.UTC | |
objDate=None | |
if type(suspectedDate)==str: | |
objDate=parse(suspectedDate,fuzzy=True) | |
elif type(suspectedDate)==datetime: | |
objDate=suspectedDate | |
if objDate.tzinfo is None: | |
objDate=pytz.timezone(localTimeZone).localize(objDate) | |
objDate=utc.normalize(objDate) | |
else: | |
objDate=utc.normalize(objDate) | |
if objDate is not None: | |
objDate=utc.normalize(objDate) | |
return objDate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
toUTC('yesterday 9:13am')
datetime.datetime(2014, 1, 13, 17, 13, tzinfo=)