Issue when using date from global value

I have a KWGT widget with a global date variable which is equal to the current date, OR in case another global boolean variable called debug is true is a custom date based on several global number variables. The formula for it is:

$dp(if(gv(debug), gv(dbgYear) + "y" + gv(dbgMonth) + "M" + gv(dbgDay) + "d" + gv(dbgHour) + "h" + gv(dbgMinut) + "m"))$

which evaluates to "2019y09M07d10h07m00s" when debug is false.

This global variable is then used to calculate the date of the next weekday. However when the formula to do this is used twice in only the first instance evaluates to the correct date.

For example, this formula:

$dp(gv(date) + if(df(f, gv(date)) > 5, "a" + (8 - df(f, gv(date))) + "d", ""))$
$dp(gv(date) + if(df(f, gv(date)) > 5, "a" + (8 - df(f, gv(date))) + "d", ""))$

results in the following:


I.e. the first instance results in the correct date, while the second instance results in the current date.

The result is the same when debug is true, using the same date.

It seems like the global date variable is somehow modified when it's first used, or is interpered differently depending on where it's retrieved.