bremor / public_transport_victoria

Custom component for retrieving departure times for Public Transport Victoria.
31 stars 7 forks source link

times displaying in UTC format #20

Closed rozdog closed 1 month ago

rozdog commented 12 months ago

The times for the upcoming trains are incorrectly showing. I'm pretty sure the time is displaying in UTC format rather than AEST.

ajobbins commented 11 months ago

I'm seeing the same issue. Attributes show the below. It looks like the attributes know the times are in UTC, but it's converting to local.

Attribute Value
Scheduled departure utc 23 November 2023 at 13:33:00
Estimated departure utc Unknown
At platform false
Platform number 1
Flags
Departure sequence 0
Departure 02:33 AM
ajobbins commented 11 months ago

I've fixed this for myself. I'm running HA in a docker container, but while I had a timezone set in HA, the container was running on UTC. I fixed this by setting a timezone for the container with the environment variable TZ=Australia/Melbourne. Now the sensor is displaying local time.

I'm not a developer and I don't know python, but it looks to me the function on lines 151-155 of 'public_transport_victoria.py' is using a function that must invoke the containers timezone not HAs.

def convert_utc_to_local(utc):
    d = datetime.datetime.strptime(utc, '%Y-%m-%dT%H:%M:%SZ')
    d = d.replace(tzinfo=datetime.timezone.utc)
    d = d.astimezone()
    return d.strftime('%I:%M %p')
rozdog commented 11 months ago

thanks for the response. I ended up fixing it as well.

I was also running HA on docker, and decided to migrate to a virtual machine (for a number of reasons) which instantly fixed the issue. Seems like it is largely a bug with docker HA installs.

james-callahan commented 1 month ago

I also ran into this (running homeassistant in a docker container); setting TZ=Australia/Melbourne worked as a workaround but I'd love to see a proper fix upstream.

@ajobbins you indeed seem correct on that function: I'm not sure why the code converts it to localtime; I expect we can set the units somehow so that homeassitant already understands it to be UTC

catatonicChimp commented 1 month ago

Fixed in https://github.com/bremor/public_transport_victoria/commit/e5b358773b938ab346fb77c87e2d78333cd921ea Switch the standard python local tz with the one from home assistant, so now it won't care about what the OS's time zone is, just what is set in Home Assistant.