metaeducation / ren-c

Library for embedding a Rebol interpreter into C codebases
GNU Lesser General Public License v3.0
126 stars 27 forks source link

Bug: negative delta-time across midnight #1136

Closed giuliolunati closed 2 years ago

giuliolunati commented 2 years ago

I got a negative result from delta-time running across midnight

hostilefork commented 2 years ago

Sorry for not looking at this sooner. Believed fixed here.

Rebol2 used NOW in DELTA-TIME with DIFFERENCE on the returned DATE!s. So its calculation took days into account, including short times spanning a midnight boundary.

But R3-Alpha used something called STATS/TIMER with SUBTRACT on the returned TIME!s. The idea behind STATS/TIMER is it told you the TIME! elapsed since the interpreter had been started. (I imagine he preferred this to NOW/PRECISE because it wouldn't involve timezones, and could be faster than NOW in theory--so it would contaminate the timing less with the call to capture the end time.)

I eliminated STATS/TIMER pretty early on, and probably thought NOW/TIME/PRECISE was good enough. But doing it that way does not include days, which STATS/TIMER (presumably) did.

As it turns out, Red's code has the same problem and won't account for days:

red>> source dt
dt: func [
    "Returns the time required to evaluate a block" 
    body [block!] 
    return: [time!] 
    /local t0
][
    t0: now/time/precise 
    do body 
    now/time/precise - t0
]

Perhaps I thought about just using plain NOW/PRECISE at first, but saw SUBTRACT would return an INTEGER! and didn't know about the DIFFERENCE function?