#1 03 Mar 2010 16:47

insthink
Member
Registered: 24 Oct 2009
Posts: 51

Chronometer/Timer app for CMD?

I've googled this already, but results are scarce, and those results are dead links.

Basically I'm looking for a cmd app that would start a timer/chrono until it is told to stop. Once stopped, it would tell the time between start/stop

Example:

12:00:00 - timer.exe start
>timer started
12:10:52 - timer.exe stop
>652  [seconds.ms]

Offline

#2 04 Mar 2010 00:49

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 939

Re: Chronometer/Timer app for CMD?

How about this?

@echo off

setlocal

set time=
set time=%time: =0%

set stamp.file=%temp%\%~n0.stamp

if /i "%~1" EQU "start" call :make.stamp
if /i "%~1" EQU "stop"  call :read.stamp stop
if /i "%~1" EQU "lap"   call :read.stamp lap
if    "%~1" EQU ""      call :status

endlocal

goto :EOF

:status

if exist "%stamp.file%" (
    if /i "%~1" NEQ "/q" echo:Timer is active.
    exit /b 0
)

echo:Timer is not active.

exit /b 1

:make.stamp

if exist "%stamp.file%" call :read.stamp stop

set start.time=%time%

(echo:%start.time%) > "%stamp.file%"

echo:Timer started %start.time%

goto :EOF

:read.stamp

call :status /q

if errorlevel 1 goto :EOF

set stop.time=%time%

set /p start.time=< "%stamp.file%"

echo:Timer started %start.time%
echo:Timer %1ped %stop.time%

if %1 EQU stop del "%stamp.file%"

call :calc.time.code %start.time%
set start.time.code=%errorlevel%

call :calc.time.code %stop.time%
set stop.time.code=%errorlevel%

set /a diff.time.code=stop.time.code - start.time.code

if %diff.time.code% LSS 0 set /a diff.time.code+=(24 * 60 * 60 * 100)

setlocal

set /a hs=diff.time.code %% 100
set /a diff.time.code/=100
set /a ss=diff.time.code %% 60
set /a diff.time.code/=60
set /a mm=diff.time.code %% 60
set /a diff.time.code/=60
set /a hh=diff.time.code

set hh=0%hh%
set mm=0%mm%
set ss=0%ss%
set hs=0%hs%

endlocal & set diff.time=%hh:~-2%:%mm:~-2%:%ss:~-2%.%hs:~-2%

echo %diff.time.code% hundredths of a second
echo %diff.time%

goto :EOF

:calc.time.code

setlocal

for /f "usebackq tokens=1,2,3,4 delims=:." %%a in ('%1') do (
    set hh=%%a
    set mm=%%b
    set ss=%%c
    set hs=%%d
)

set /a hh=((%hh:~0,1% * 10) + %hh:~1,1%) * 60 * 60 * 100
set /a mm=((%mm:~0,1% * 10) + %mm:~1,1%) * 60 * 100
set /a ss=((%ss:~0,1% * 10) + %ss:~1,1%) * 100
set /a hs=((%hs:~0,1% * 10) + %hs:~1,1%)

set /a time.code=hh + mm + ss + hs

endlocal & exit /b %time.code%

Use it with "start", "stop" and "lap" as parameters (using "lap" will get the time since start, without stopping the counter).

It'll handle crossing midnight but has no concept of date, so timing events >1 day won't work.  It shouldn't be too difficult to integrate the DATEMATH script to deal with that though.

EDIT: Running without parameters will display a brief message about whether or not the timer is active.
EDIT: Fixed ineffective crossing-midnight code.  Thanks RG.

Last edited by bluesxman (05 Mar 2010 16:24)


cmd | *sh  | KiX | AutoIT3 | PowerShell | Puppet

Offline

#3 04 Mar 2010 01:53

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 321

Re: Chronometer/Timer app for CMD?

Nice !


CMD and InstallShield

Offline

#4 04 Mar 2010 03:06

insthink
Member
Registered: 24 Oct 2009
Posts: 51

Re: Chronometer/Timer app for CMD?

bluesxman wrote:

How about this?
Use it with "start", "stop" and "lap" as parameters (using "lap" will get the time since start, without stopping the counter).

It'll handle crossing midnight but has no concept of date, so timing events >1 day won't work.  It shouldn't be too difficult to integrate the DATEMATH script to deal with that though.

I see what you did there big_smile

But wow, that little tool will come in handy for all sorts of things.
I understand how it can't handle dates, thus crossing 24h resets the timer to 0, but that shouldn't be an issue, ever, at all.

Bluesxman is credit to team!

Offline

#5 05 Mar 2010 01:06

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 939

Re: Chronometer/Timer app for CMD?

I've made a couple of minor tweaks -- code above updated.

Last edited by bluesxman (05 Mar 2010 01:07)


cmd | *sh  | KiX | AutoIT3 | PowerShell | Puppet

Offline

#6 05 Mar 2010 13:34

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 321

Re: Chronometer/Timer app for CMD?

Very practical!  To correctly handle the crossing of midnight the line

if diff.time.code LSS 0 set /a diff.time.code*=-1
should be
if diff.time.code LSS 0 set /a diff.time.code+=8640000

This is easily tested by:
Set date/time back
Start timer
Reset date/time
Stop timer


CMD and InstallShield

Offline

#7 05 Mar 2010 16:25

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 939

Re: Chronometer/Timer app for CMD?

Yeah you have a point there -- thanks for drawing attention to my *cough* thorough testing big_smile

In fact that whole line was never going to work cause I managed to miss the "%" out (a bug you've duplicated in your correction!)

All corrected above.

Last edited by bluesxman (05 Mar 2010 16:25)


cmd | *sh  | KiX | AutoIT3 | PowerShell | Puppet

Offline

#8 08 Mar 2010 16:49

insthink
Member
Registered: 24 Oct 2009
Posts: 51

Re: Chronometer/Timer app for CMD?

Wow thanks guys!
You guys should create some sort of database of pre-made batch scripts like this.
Saves a lot of time when you're working on a project, and you need a certain function wink

Offline

Board footer

Powered by FluxBB