jbouwh / omnikdatalogger

Datalogger for Omnik solar power inverters with DSMR integration and output to Home Assistant, PVOUTPUT, InfluxDB and MQTT
https://jbsoft.nl/site/omnik-datalogger/
GNU General Public License v3.0
10 stars 3 forks source link

daylight: Use proper timezone instead of interpreting UTC as local time #77

Closed MarijnS95 closed 2 years ago

MarijnS95 commented 2 years ago

Fixes #72, CC @fritzzjan

Both datetime.now() and datetime.utcnow() return a datetime object without tzinfo. As such .astimezone() has no reference timezone to convert from when applying the desired timezone, and assumes the datetime object to be in local time. In this instance .astimezone() assumes the result from .utcnow() to be in local time, hence performs no conversion (in the case where city matches the local timezone) leading to localtime() returning unmodified UTC time disguised as a different timezone.

For this reason the documentation on datetime.utcnow() recommends to use datetime.now() with an explicit timezone argument like timezone.utc or in the case of this PR, the desired timezone directly.