grahamwetzler / smart-meter-texas

Package to connect to and retrieve data from the unoffical Smart Meter Texas API
MIT License
14 stars 14 forks source link

odrdate is incorrectly parsed into UTC #137

Closed utdrmac closed 1 year ago

utdrmac commented 1 year ago
DEBUG:smart_meter_texas:Reading completed: {'odrstatus': 'COMPLETED', 
'odrread': '88612.961', 'odrusage': '62.503', 'odrdate': '07/22/2023 22:50:28', 
'responseMessage': 'SUCCESS'}

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