I have a date string 18 May 14:30
which corresponds to the British summertime (WEST
or UTC+1
). I would like to convert it to central Euopean (summer)time.
Here is my code
# from datetime import datetime
# from pytz import timezone
d = '18 May 14:30'
# Attempt 1
dd=datetime.strptime(d, '%d %b %H:%M').replace(year=datetime.now().year, tzinfo=timezone('WET'))
dd.astimezone(timezone('CET'))
# datetime.datetime(2019, 5, 18, 16, 30, tzinfo=<DstTzInfo 'CET' CEST+2:00:00 DST>)
# It should be 15:30, not 16:30
# Attempt 2
dd=datetime.strptime(d, '%d %b %H:%M').replace(year=datetime.now().year, tzinfo=timezone('WET'))
# Same result as above
# Attempt 3
dd=datetime.strptime(d, '%d %b %H:%M').replace(year=datetime.now().year, tzinfo=timezone('Etc/GMT-1'))
dd.astimezone(timezone('CET'))
# datetime.datetime(2019, 5, 18, 15, 30, tzinfo=<DstTzInfo 'CET' CEST+2:00:00 DST>)
# This works
So my problem in the third attempt I had to manually specify GMT-1
whereas CET
automatically transforms to CEST
. I hoped this would work identically for WET
(to WEST
).
Besides, what also confuses me is the fact that according to Wiki British summertime should be UTC +1
but I had to set GMT-1
(as GMT+1
returns 18:30
).
But despite this intention, the practice hasn't always proved popular over the years and, in 2019, the European parliament voted in favour of scrapping Daylight Savings Time altogether. This change was due to take effect for the first time in 2021 but plans have been stalled.
British Summer Time (BST), otherwise known as daylight saving time, was originally devised to keep factories open for longer in the summer – since there was enough light to do so. It has also been suggested that BST reduces energy consumption.
The clocks have gone back this weekend, with British Summer Time (BST) ending as autumn draws in and the nights grow longer. It means the UK will now move back into Greenwich Mean Time (GMT) until spring next year.
Why do the clocks change? The clocks go back to revert to Greenwich Mean Time (GMT), which was in place before British Summer Time started in March.
In case it interests someone, I did manage to find a workaround
dd=datetime.strptime(d, '%d %b %H:%M').replace(year=datetime.now().year)
timezone('WET').localize(dd).astimezone(timezone('CET'))
# datetime.datetime(2019, 5, 18, 15, 30, tzinfo=<DstTzInfo 'CET' CEST+2:00:00 DST>)
# Correct result without having to specify the shift
# Let's check with another date (non-summertime)
d = '18 Jan 14:30'
dd=datetime.strptime(d, '%d %b %H:%M').replace(year=datetime.now().year)
timezone('WET').localize(dd).astimezone(timezone('CET'))
# datetime.datetime(2019, 1, 18, 15, 30, tzinfo=<DstTzInfo 'CET' CET+1:00:00 STD>)
# Yay!
It's not very elegant but at least it does the job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With