Chronometer/Timer app for CMD?

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Chronometer/Timer app for CMD?

Post by MigrationUser »

03 Mar 2010 16:47
insthink

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]

----------------------------

#2 04 Mar 2010 00:49
bluesxman

How about this?

Code: Select all

@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)

----------------------------
03 Oct 2018 11:32
Simon Sheppard
insthink wrote:

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
This is such a handy script, I've just put a copy on the main site to make it easier to find. With a credit to bluesxman and a link back to this thread.
https://ss64.com/nt/syntax-timer.html

26 Jun 2019 13:43
nanobot

Clear enough, thanks, now i can measure or timing long QT build smile

----------------------------

#32 27 Jun 2019 00:20
Simon Sheppard


OK done, I have also updated the TIME page with some better info about regional differences.

Also thanks for taking the time to report this issue nanobot, it's always great to see code being tested and fixed to work in different regions.

----------------------------

#33 27 Jun 2019 09:04
nanobot


Thanks to you too Simon Sheppard for also helping and assisting my issue, also what a great community in here.
Post Reply