You are not logged in.
Pages: 1
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
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
Offline
Nice !
CMD and InstallShield
Offline
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 ![]()
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
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
Offline
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
Yeah you have a point there -- thanks for drawing attention to my *cough* thorough testing ![]()
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
Offline
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 ![]()
Offline
Pages: 1