0

I've been using a fairly simple time script to track a software's run time measurement with clock time measurement to ensure they are roughly in sync. However, I've realized that this method will utterly fail if the clock goes past midnight.

Right now I'm doing this:

set start=!time!
[Code to measure goes here]
set end=!time!

for /F "tokens=1-4 delims=:.," %%a in ("!start!") do (
        set /a "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
    )
for /F "tokens=1-4 delims=:.," %%a in ("!end!") do (
        set /a "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
    )

set /A elapsed=end-start
echo Elapsed Time: !elapsed!

Does anyone know of any neat little tricks I could use to circumvent this problem?

JohnN
  • 968
  • 4
  • 13
  • 35
  • The canonical answer would be - convert to epoch time. Unfortunately, that's difficult on pure Windows. My first suggestion would be 'install perl'. My second would be just run `date /t` and compare them. Not being equal might none the less be a safe assumption that just the next day. Ref: http://stackoverflow.com/questions/3454112/is-there-a-way-to-get-epoch-time-using-a-dos-command - there's a vbscript answer too. – Sobrique Mar 06 '15 at 22:07
  • If installing perl is an option (e.g. from ActiveState or Strawberry) - you could replace your time command with `perl -e "print time"` (or more of the script) – Sobrique Mar 06 '15 at 22:09
  • Take a look here : http://stackoverflow.com/questions/1985822/how-to-measure-code-execution-time-in-vbscript-or-javascript – SachaDee Mar 06 '15 at 22:18

1 Answers1

2
 if %elapsed% lss 0 set /a elapsed +=8640000

Where 8640000 is the number of hundredths-of-seconds in 24 hours.

Naturally, if you're using a 12-hour clock (you don't say) you would need to use a different constant - and the critical time becomes 1am/1pm

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Yep - I do this all the time, and it works as long as the time interval is less than 24 hours. – dbenham Mar 07 '15 at 00:52
  • I feel incredibly stupid for not thinking of this simple yet brilliant solution! – JohnN Mar 11 '15 at 13:27
  • should you use just `+` instead of `+=`? – JohnN Mar 11 '15 at 14:48
  • `set /a elapsed +=8640000` addds 8640000 to `elapsed`. `set /a elapsed =8640000+elapsed` or `set /a elapsed =8640000+%elapsed%` would do the same thing, but the first form is less typing. – Magoo Mar 11 '15 at 15:27