The above odrdate is in America/Chicago timezone. reading_datetime() parses it as UTC. Storing this value in a database (influxdb, for example) to be presented in a graph (grafana, for example), all of the times are skewed by 5hrs.
Consider the following code change to parse the timestamp as America/Chicago first, then convert to UTC
from dateutil.tz import gettz
...
@property
def reading_datetime(self):
date = dateutil.parser.parse(self.reading_data["odrdate"]).replace(tzinfo=gettz("America/Chicago"))
date_as_utc = date.astimezone(datetime.timezone.utc)
return date_as_utc
Debugging:
>>> d=dateutil.parser.parse("07/22/2023 22:50:28")
>>> f=d.astimezone(datetime.timezone.utc)
>>> d
datetime.datetime(2023, 7, 22, 22, 50, 28)
>>> f
datetime.datetime(2023, 7, 22, 22, 50, 28, tzinfo=datetime.timezone.utc) <--- this is not correct
>>> d=dateutil.parser.parse("07/22/2023 22:50:28").replace(tzinfo=gettz("America/Chicago"))
>>> f=d.astimezone(datetime.timezone.utc)
>>> d
datetime.datetime(2023, 7, 22, 22, 50, 28, tzinfo=tzfile('/usr/share/zoneinfo/America/Chicago'))
>>> f
datetime.datetime(2023, 7, 23, 3, 50, 28, tzinfo=datetime.timezone.utc) <-- correct UTC
The above
odrdate
is in America/Chicago timezone.reading_datetime()
parses it as UTC. Storing this value in a database (influxdb, for example) to be presented in a graph (grafana, for example), all of the times are skewed by 5hrs.Consider the following code change to parse the timestamp as America/Chicago first, then convert to UTC
Debugging: