Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created May 9, 2017 13:10
Show Gist options
  • Save kezabelle/4836860d1ac88fb77db229db29fef987 to your computer and use it in GitHub Desktop.
Save kezabelle/4836860d1ac88fb77db229db29fef987 to your computer and use it in GitHub Desktop.
>>> from django.utils import timezone
>>> import pytz
>>> now = timezone.now()
>>> x = pytz.timezone('Europe/London')
>>> now2 = now.replace(tzinfo=x)
>>> now3 = now2.astimezone(pytz.utc)
>>> now4 = now3.astimezone(x)
(datetime.datetime(2017, 5, 9, 12, 58, 13, 596984, tzinfo=<UTC>),
datetime.datetime(2017, 5, 9, 12, 58, 13, 596984, tzinfo=<DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD>),
datetime.datetime(2017, 5, 9, 12, 59, 13, 596984, tzinfo=<UTC>),
datetime.datetime(2017, 5, 9, 13, 59, 13, 596984, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>))
>>> now = timezone.now()
>>> now2 = timezone.localtime(now, x)
>>> now3 = timezone.localtime(now, pytz.utc)
>>> now4 = timezone.localtime(now3, x)
(datetime.datetime(2017, 5, 9, 13, 0, 51, 201378, tzinfo=<UTC>),
datetime.datetime(2017, 5, 9, 14, 0, 51, 201378, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>),
datetime.datetime(2017, 5, 9, 13, 0, 51, 201378, tzinfo=<UTC>),
datetime.datetime(2017, 5, 9, 14, 0, 51, 201378, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment