time manipulation, different approaches yielding different results Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BartmanEH
Posts: 23
Joined: 01 Apr 2021, 15:48

time manipulation, different approaches yielding different results

12 Apr 2021, 07:39

I have the following simple code with comments showing the debug console output. In an attempt to reduce the numbers of lines in my code, I had tried to shorten the first two statements into the one third statement but it yields different results. Would someone explain the difference? I am aware that there could be a slight difference due to A_Now being established at different times (this is only debug code), however, that could not explain a 520 Second difference.

Code: Select all

DebugMessage(endTime)                               ;20210412083958
remainingTime := endTime
DebugMessage(remainingTime)                         ;20210412083958
remainingTime -= A_Now, Seconds
DebugMessage(remainingTime)                         ;803
remainingTime_alt := endTime - A_Now, Seconds       ;result not same as previous
DebugMessage(remainingTime_alt)                     ;1323
just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: time manipulation, different approaches yielding different results  Topic is solved

12 Apr 2021, 07:44

Code: Select all

remainingTime_alt := endTime - A_Now, Seconds       ;result not same as previous
is equal to

Code: Select all

remainingTime_alt := endTime - A_Now
Seconds
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: time manipulation, different approaches yielding different results

12 Apr 2021, 07:46

the third line is not equivalent to the EnvSub shorthand syntax
simply put, ure not allowed to combine/extend the syntax in that way(however logical u may think it is)
BartmanEH
Posts: 23
Joined: 01 Apr 2021, 15:48

Re: time manipulation, different approaches yielding different results

12 Apr 2021, 07:59

Lots of people trying to be helpful as usual. Thank you. But I still do not understand. I'm following the syntax defined as Var -= Value , TimeUnits according to https://www.autohotkey.com/docs/commands/EnvSub.htm, where it says these are equivalent:
EnvSub, Var, Value , TimeUnits
Var -= Value , TimeUnits
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: time manipulation, different approaches yielding different results

12 Apr 2021, 08:08

Yes, but your line 6 does not follow either syntax.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: time manipulation, different approaches yielding different results

12 Apr 2021, 08:10

Yes but remainingTime_alt := endTime - A_Now, Seconds does not correspond to this sytax rules.
remainingTime_alt := endTime - A_Now is one expression, and Seconds is another expression, independent of the first (it does not achieve much though :P ), separated from the first by a comma.
Last edited by braunbaer on 12 Apr 2021, 08:11, edited 1 time in total.
BartmanEH
Posts: 23
Joined: 01 Apr 2021, 15:48

Re: time manipulation, different approaches yielding different results

12 Apr 2021, 08:10

I see the point everyone is trying to make. My noobie eyes were too tired.

Is there a way to accomplish my original goal of reducing the first two statements into one new statement? I'm trying to avoid EnvSub as this is removed (renamed) in AHKv2 and I'm trying to make my scripts v2 compliant.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: time manipulation, different approaches yielding different results

12 Apr 2021, 08:16

not tested, but I think should work

Code: Select all

DebugMessage(endTime), remainingTime := endTime, DebugMessage(remainingTime), (remainingTime -= A_Now, Seconds), debugMessage(remainingTime) 
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: time manipulation, different approaches yielding different results

12 Apr 2021, 08:17

Code: Select all

remainingTime := endTime := 202104120930
remainingTime -= A_Now, Seconds
BartmanEH
Posts: 23
Joined: 01 Apr 2021, 15:48

Re: time manipulation, different approaches yielding different results

12 Apr 2021, 08:18

Not to insult anyone but I've marked this as complete based on the first answer which should've been all I needed :)

Thanks, @braunbaer although your solution is simply comma separated multi-statements in one line, close enough.
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: time manipulation, different approaches yielding different results

12 Apr 2021, 08:28

This is an inaccurate formula. It will work for some times but not all. Timestamps are a bit like formatted strings and are not always conducive to native math functions. You can prove it by setting the two times to different days. The math will yield a number, but not the number of seconds.
BartmanEH
Posts: 23
Joined: 01 Apr 2021, 15:48

Re: time manipulation, different approaches yielding different results

12 Apr 2021, 08:43

Hmm, which formula is inaccurate? This?

Code: Select all

remainingTime -= A_Now, Seconds
Seems to be documented as equivalent to EnvSub which is documented as how to "compare date-time values"
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: time manipulation, different approaches yielding different results

12 Apr 2021, 08:51

The plain subtraction formula marked in "Topic is solved" will not always yield seconds. The statement of equality of the two scripts is accurate, but the formula itself does not yield seconds. This is perhaps already understood, but I wanted to clarify in case other readers believe that the highlighted area is how times can be subtracted.
BartmanEH
Posts: 23
Joined: 01 Apr 2021, 15:48

Re: time manipulation, different approaches yielding different results

12 Apr 2021, 08:58

Thank you, understood and agree it's valuable to clarify for future readers.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, ht55cd3, mamo691 and 370 guests