Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Wrong result from simple clock arithmetic (`clock add` with negative base).

Closed / Fixed482db1d796540e6865abdce17e2aa391f3fb88ffCreated 2 years agoUpdated 2 years ago
Descriptionwiki2025-03-11 14:25:38
I observe a wrong result from [clock add -631152000 2372806200 seconds]. This should obviously produce 1741654200 (-631152000 + 2372806200), but it returns 1741567800 instead.
sebreswiki2025-03-11 17:41:13

The reason is a bug by porting [clock add] from Tcl to C - calculating seconds of day localSeconds % SECONDS_PER_DAY on a negative base generates negative result in C (-82800 instead of 3600 as it'd be in Tcl), so after all the seconds of day are wrong and the whole result deviate by one day:

- UTC -631152000 => LOCTM -631148400 % 86400 == -82800  // C vs.
+ UTC -631152000 => LOCTM -631148400 % 86400 ==   3600  ## Tcl
Fixing it now for 8.7+, add coverage, etc...

sebreswiki2025-03-11 18:45:47

Basically fixed in [27cbd7afc65a5c79], but found more places for a compiler "fix" (signed-mod operation with potentially negative dividend), so added more corrections in [e33fcd57b0397d45] (including few code simplifications and normalizations).

Test coverage is added too, so I close the ticket as fixed for now.

sebres Ticket [482db1d796]: 4 changes [History diff]2025-03-11 19:07:40
Log in (anonymous is fine) to add a comment.