appdaemon icon indicating copy to clipboard operation
appdaemon copied to clipboard

parse_time("sunset") and impact of "aware"

Open peterwup opened this issue 3 years ago • 5 comments

I read in the documentation that: parse_time(self, time_str: str, name: str = None, aware: bool = False)

My understandig: False = working with UTC / True = working with local time.

Default value = False which would lead (to my understanding) to a return time in UTC, if I do not set aware to True.

As I was insecure I did a little test:

    self.log("PT1: {}".format(self.parse_time("sunset")))
    self.log("PT2: {}".format(self.parse_time("sunset",aware=False)))
    self.log("PT3: {}".format(self.parse_time("sunset",aware=True)))
    self.log("UTC diff: {}".format(self.get_tz_offset()))

Result of the test was:

2023-02-06 19:29:43.641860 INFO WzBlindControl: UTC diff: 60.0
2023-02-06 19:29:43.636152 INFO WzBlindControl: PT3: 17:34:46.316067
2023-02-06 19:29:43.631426 INFO WzBlindControl: PT2: 17:34:46.316067
2023-02-06 19:29:43.623843 INFO WzBlindControl: PT1: 17:34:46.316067

so it seems as if "aware" has no impact, which is totally OK for me. I was afraid that I always had to add "aware=True".

Is that intended ?

thank you Peter

peterwup avatar Feb 06 '23 19:02 peterwup

Which version of AD are you seeing this in? I made some changes in that area a few days ago I’m the dev branch.

Message ID: @.***>

In general just use whichever works best. Aware timestamps are probably better on systems that have the time zone set to something other than local, e.g. UTC but it isn’t mandatory.

The 2 results should be different - on should have a time zone the other should not hence my question above as to which code you saw this in so I can take a look.

acockburn avatar Feb 07 '23 19:02 acockburn

Hi, I use AppDaemon v4.2.1. As I said, everithing works fine for me, I just was insecue becaue it was not clear for me what is the meaning of "aware: bool" in the specification.

import appdaemon.plugins.hass.hassapi as hass
from datetime import datetime, date, timedelta
import adbase as ad

class BlindControl(hass.Hass):
  ## Init  
  def initialize(self): 
     self.log("PT1: {}".format(self.parse_time("sunset")))
     self.log("PT2: {}".format(self.parse_time("sunset",aware=False)))
     self.log("PT3: {}".format(self.parse_time("sunset",aware=True)))
     self.log("UTC diff: {}".format(self.get_tz_offset()))
     self.log("Now: : {}".format(self.get_now()))
     self.log("TZ: {}".format(self.get_timezone()))
     self.log("---- BlindControl initialized  ")
```



peterwup avatar Feb 07 '23 20:02 peterwup

Sorry, I think I found the problem. My fault. I will test it tomorrow and give feed back.

peterwup avatar Feb 07 '23 22:02 peterwup

Ok back again. I have one idea:

is it possible that self.log("".format()) convert a time stamp always into local time ?

Here the code:

import appdaemon.plugins.hass.hassapi as hass

class MyAdTest(hass.Hass):
  def initialize(self): 
    self.log("PT1: {}".format(self.parse_time("sunset")))
    self.log("PT2: {}".format(self.parse_time("sunset",False)))
    self.log("PT3: {}".format(self.parse_time("sunset",True)))
    self.log("UTC diff: {}".format(self.get_tz_offset()))
    self.log("Now: : {}".format(self.get_now()))
    self.log("TZ: {}".format(self.get_timezone()))
    self.log("---- BlindControl initialized  ")

and the result in log file:

2023-02-08 18:46:14.773109 INFO ad_test: TZ: Europe/Berlin
--
2023-02-08 18:46:14.764276 INFO ad_test: Now: : 2023-02-08 18:46:14.760366+01:00
2023-02-08 18:46:14.758570 INFO ad_test: UTC diff: 60.0
2023-02-08 18:46:14.749521 INFO ad_test: PT3: 17:38:22.537785
2023-02-08 18:46:14.744240 INFO ad_test: PT2: 17:38:22.537785
2023-02-08 18:46:14.732194 INFO ad_test: PT1: 17:38:22.537785

peterwup avatar Feb 08 '23 17:02 peterwup

OK, there was a small bug here which will be fixed in the next release - 4.3.0.

However even with that fix your app won't work as you expect because python doesn't print out the timezone info for time objects (it does for date time objects). If you run the following in your app it will demonstrate that the call with aware=True does return a time object with a timezone in it:

        self.log("PT1: {}".format(self.parse_time("sunset")))
        self.log("PT2: {}".format(self.parse_time("sunset", aware=False)))
        self.log("PT3: {}".format(self.parse_time("sunset", aware=True)))
        self.log("PT3: {}".format(self.parse_time("sunset", aware=True).tzinfo))
        self.log("UTC diff: {}".format(self.get_tz_offset()))
        self.log("Now: : {}".format(self.get_now()))
        self.log("TZ: {}".format(self.get_timezone()))

acockburn avatar Mar 04 '23 12:03 acockburn