Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Last active August 30, 2018 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinThoma/2c59338684dfdd63e75dae69d76b9130 to your computer and use it in GitHub Desktop.
Save MartinThoma/2c59338684dfdd63e75dae69d76b9130 to your computer and use it in GitHub Desktop.

Code

UTC time with offset:

>>> import pytz
>>> tz = pytz.timezone('Pacific/Apia')
>>> import datetime
>>> datetime.datetime(2011, 12, 30, 9, 59, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-29T23:59:00-10:00'
>>> datetime.datetime(2011, 12, 30, 10, 00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-31T00:00:00+14:00'

Local time:

>>> '{:%Y-%m-%d %H:%M}'.format(datetime.datetime(2011, 12, 30, 9, 59, tzinfo=datetime.timezone.utc).astimezone(tz))
'2011-12-29 23:59'
>>> '{:%Y-%m-%d %H:%M}'.format(datetime.datetime(2011, 12, 30, 10, 00, tzinfo=datetime.timezone.utc).astimezone(tz))
'2011-12-31 00:00'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment