Skip to content

Instantly share code, notes, and snippets.

@msukmanowsky
Last active October 16, 2015 15:32
Show Gist options
  • Save msukmanowsky/e476612fdb41db2aef7b to your computer and use it in GitHub Desktop.
Save msukmanowsky/e476612fdb41db2aef7b to your computer and use it in GitHub Desktop.
import datetime as dt
import pprint
import pytz
print(pytz.__version__)
# '2015.4'
timezone = pytz.timezone('Europe/London')
tmsp = dt.datetime(2015, 3, 29, 1, tzinfo=pytz.utc)
tmsp_local = timezone.normalize(tmsp.astimezone(timezone))
# DST is active, so 1 AM actually becomes 2 AM
# datetime.datetime(2015, 3, 29, 2, 0, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>)
local_midnight = tmsp_local.replace(hour=0, minute=0, second=0)
# Flooring to midnight however, moves us out of DST (since DST begins at 1 AM) but
# replace didn't touch the DST UTC offset so we get
# datetime.datetime(2015, 3, 29, 0, 0, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>)
# instead of what we should get which is +00:00 UTC so, we normalize
local_midnight = timezone.normalize(local_midnight)
# however normalize doesn't properly check that the DST offset on the current
# datetime is bogus and instead subtracts an hour to arrive at a bogus midnight
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment