Skip to content

Instantly share code, notes, and snippets.

@bjmc
Created June 27, 2014 19:07
Show Gist options
  • Save bjmc/ce4e518de8fcbca96489 to your computer and use it in GitHub Desktop.
Save bjmc/ce4e518de8fcbca96489 to your computer and use it in GitHub Desktop.
>>> tz = tzlocal.get_localzone()
>>> utc = pytz.timezone("UTC")
>>> utc
<UTC>
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2014, 6, 27, 12, 6, 12, 676354)
>>> tz
<DstTzInfo 'America/Los_Angeles' LMT-1 day, 16:07:00 STD>
>>> tz.localize(now)
datetime.datetime(2014, 6, 27, 12, 6, 12, 676354, tzinfo=<DstTzInfo 'America/Los_Angeles' PDT-1 day, 17:00:00 DST>)
>>> tz.localize(now).astimezone(utc)
datetime.datetime(2014, 6, 27, 19, 6, 12, 676354, tzinfo=<UTC>)
>>> in_utc = tz.localize(now).astimezone(utc)
>>> back_to_local = in_utc.astimezone(tz)
>>> back_to_local
datetime.datetime(2014, 6, 27, 12, 6, 12, 676354, tzinfo=<DstTzInfo 'America/Los_Angeles' PDT-1 day, 17:00:00 DST>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment