I have a problem with pytz and daylight saving time. When I use the timezone Europe/Berlin
, it always uses the timezone offset without DST.
Minimal example:
print(repr(pytz.timezone("Europe/Berlin")))
<DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>
# Should probably be something like <DstTzInfo 'Europe/Berlin' CET+2:00:00 DST>
# Usage
from django.utils import timezone
from datetime import datetime
datetime_now = timezone.now()
print(my_time)
# Result: 00:00:00
print(datetime.combine(datetime_now, my_time, tzinfo=timezone.get_current_timezone()))
# Result: 2020-04-04 00:00:00+01:00, should be 2020-04-04 00:00:00+02:00
The minimal example for my use case would be an alarm clock. The user sets the clock to 06:00
(without thinking about timezones) and the clock should ring at 06:00
in the current timezone, i.e., 06:00+02
when it's DST and 06:00+01
otherwise for Europe/Berlin
.
The implementation is a Django model using django.models.TimeField
for the non-aware Time (e.g. 06:00
) and I want to compare it to the current time and other TimeFields by creating a datetime object that has the current date and the time storted in the TimeField
.
I am open to different suggestions about time objects (e.g using or not using django.utils.timezone
) as long as I can create datetime objects that I can compare to each other and increment / decrement with timedelta objects (or some similar method).
Another minimal example (with Django only for getting the current timezone):
from django.utils import timezone
import datetime
tz = timezone.get_current_timezone()
time_now = datetime.datetime.now(tz=tz)
clock_time = datetime.time(1,2)
combined_time = datetime.combine(time_now, clock_time, tzinfo=tz)
print(tz)
print(time)
print(time_now)
print(combined_time)
results in
Europe/Berlin
01:02:00
2020-04-12 18:50:11.934754+02:00
2020-04-12 01:02:00+01:00