Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deal with Birtish summer time

Tags:

python

pytz

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).

like image 604
user101 Avatar asked May 12 '19 10:05

user101


People also ask

Is the UK getting rid of British summer time?

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.

What is the point of British summer time?

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.

What does British Summer Time ending mean?

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 clocks go back at 2am UK?

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.


1 Answers

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.

like image 187
user101 Avatar answered Sep 30 '22 15:09

user101