why is python reporting wrong time on windows startup?

Viewed 43

recently i've been doing a bit of experimenting and found that if i call python's datetime.datetime.now on windows startup running as a service, for the first minute it seems to report the wrong datetime. I believe it was reporting as UTC before going into the right timezone, eg US.

is there a way for python to check to continue only if windows timezone is synced?

how i did this is to run python script as a service using nssm. I did something like this, i'm retyping it so there may be indentation errors:

    def testoutput(str):
        with open("c:\\test.log", "a") as f:
          dtime = datetime.datetime.now().strftime("%m/%d/%Y-%H:%M:%S")
          f.write("[%s] %s\n" %(dtime, str))


    def main():
        testoutput("starting time..")
        while True:
            testoutput("loop time..")
            time.sleep(1)

    main() 

so.. when I start windows without logging in, i used another pc to look at the output path. In the file test.log i see something like this :

[07/15/2022-10:57:51] starting time..

[07/15/2022-10:57:52] loop time..

[07/15/2022-10:57:53] loop time..

[07/15/2022-10:57:54] loop time..

[07/15/2022-10:57:55] loop time..

[07/15/2022-03:57:56] loop time..

[07/15/2022-03:57:57] loop time..

even though i'm using the same datetime now, it is showing different time.

edit: i actually did put in some timezone info to see if there's any difference during the loop to see startup vs when the time changed. But i did not see any.

local_now = datetime.datetime.now().astimezone()
local_tz = local_now.tzinfo
local_tzname = local_tz.tzname(local_now)
testoutput("current tzname : [%s]" % (local_tzname))
testoutput("current gmtime: [%s]" % (strftime("%z", gmtime())))

this will yield me something like..

current tzname : [Pacific Daylight Time]

current gmtime : [-0800]

0 Answers
Related