'Calculate difference between CET and UTC to handle daylight saving automatically
I am trying to handle daylight saving time (DST) in my code in more automatic way. I want to calculate if difference between current UTC and CET time is one hour or two hours. I have two variables which shows current datetime in UTC and CET. Since, time is same, I am getting their difference as 0 and not as 2. Below is my code. Any suggestions will be helpful.
from datetime import datetime, timezone, timedelta
import pytz
date = pd.to_datetime(time.strftime('%Y-%m-%d'))
cet_now = datetime.now(pytz.timezone("CET"))
utc_now = datetime.now(pytz.timezone("UTC"))
h = abs((cet_now-utc_now)).total_seconds()
Solution 1:[1]
I found a solution of my question. '''
cet_now = datetime.now(pytz.timezone("CET")).hour
utc_now = datetime.now(pytz.timezone("UTC")).hour
h = abs(cet_now - utc_now)
''' Above worked.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dhruv Bhatt |