Seems to be a bug in subtracting days

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
TheBeginner
Posts: 91
Joined: 19 Oct 2018, 12:05

Seems to be a bug in subtracting days

Post by TheBeginner » 30 Jul 2022, 12:17

subtraction doesn't work (says second parameter invalid) but adding does

Code: Select all

test := A_Now
test -= 1 , Days ; Doesn't work

; While this Does
test += 1 , Days ; works
bug?

User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Seems to be a bug in subtracting days

Post by flyingDman » 30 Jul 2022, 12:46

No bug. Read the docs*. (EnvSub and EnvAdd)

Code: Select all

test := A_Now
test += -1 , Days ; works
msgbox % test
* can the docs be clearer? yes!
14.3 & 1.3.7

TheBeginner
Posts: 91
Joined: 19 Oct 2018, 12:05

Re: Seems to be a bug in subtracting days

Post by TheBeginner » 30 Jul 2022, 14:29

this is an actual example from the docs

Code: Select all

Var -= Value , TimeUnits
if you use that example it will fail

Code: Select all

Var1 -= 1, days
plus since this is how it works in the += why would it work differently in the subtraction?
is there anything in the docs mentioning a exception for the subtraction method, I've read them and I didn't find any, if you see something listed in the docs mentioning why this doesn't work do point me to it.

also note that example number two, is

Code: Select all

MyCount -= 2
which is exactly the same, minus a TimeUnits parameter.

so either it's a bug or the documentation needs improvement

User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: Seems to be a bug in subtracting days

Post by boiler » 30 Jul 2022, 14:43

TheBeginner wrote: this is an actual example from the docs

Code: Select all

Var -= Value , TimeUnits
if you use that example it will fail
It doesn't fail when used as intended which is subtracting one date-time stamp from another.

TheBeginner wrote: is there anything in the docs mentioning a exception for the subtraction method, I've read them and I didn't find any, if you see something listed in the docs mentioning why this doesn't work do point me to it.
The EnvSub documentation states:
If present, this parameter directs the command to subtract Value from Var as though both of them are date-time stamps in the YYYYMMDDHH24MISS format.
...
To add or subtract a certain number of seconds, minutes, hours, or days from a timestamp, use EnvAdd (subtraction is achieved by adding a negative number).

What you are trying to do is different than two date-time stamps, and the documentation makes a distinction between finding the difference between two date-time stamps and subtracting a number of time units from a date-time stamp. Yes, it can be more clear, but it is technically correct.

TheBeginner
Posts: 91
Joined: 19 Oct 2018, 12:05

Re: Seems to be a bug in subtracting days

Post by TheBeginner » 30 Jul 2022, 15:39

I think what threw me off is that I thought The timeunit in

Var += Value , TimeUnits

Was to tell AutoHotkey what's the time unit in the Value so it can Add or Subtract from the date-time stamp, but it seems that the timeunit is there to indicate in which units the Var will be stored

Is my understanding correct?

User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Seems to be a bug in subtracting days

Post by flyingDman » 30 Jul 2022, 16:02

Think of it this way: if you want to add or subtract a number of time units (days, minutes, etc) from a date, use EnvAdd or Var += Value, TimeUnits. The result var will de a date.
If you want to subtract a date from another date (to calculated the date difference or the difference between 2 timestamps), use c]EnvSub[/c] or Var -= Value, TimeUnits. The result Var will be a number of time units.
14.3 & 1.3.7

Post Reply

Return to “Ask for Help (v1)”