Skip to content

Instantly share code, notes, and snippets.

@jeffbryner
Last active January 3, 2016 04:59
Show Gist options
  • Save jeffbryner/8412843 to your computer and use it in GitHub Desktop.
Save jeffbryner/8412843 to your computer and use it in GitHub Desktop.
UTC date from anything
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
@jeffbryner
Copy link
Author

toUTC('yesterday 9:13am')
datetime.datetime(2014, 1, 13, 17, 13, tzinfo=)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment