Skip to content

Instantly share code, notes, and snippets.

@deskobj
Last active March 6, 2020 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deskobj/4757603 to your computer and use it in GitHub Desktop.
Save deskobj/4757603 to your computer and use it in GitHub Desktop.
Desktop Objects: Process execution timing in batch file -- https://p19z.blogspot.com/2011/08/process-execution-timing-in-batch.html
@rem MODULE ..............: timed
@rem AUTHOR ..............: Patryk Szczepankiewicz ( pszczepa [at] gmail [dot] com )
@rem REFERENCES ..........: https://p19z.blogspot.com/2011/08/process-execution-timing-in-batch.html
@rem USAGE ...............: you can use this to wrap a command
@rem
@rem $ >NUL call timed timeout /t 1
@rem $ SET _timediff
@rem
@rem will return:
@rem
@rem _timediff=00:00'01
@rem _timediff_fh=00
@rem _timediff_fm=00
@rem _timediff_fs=01
@rem _timediff_h=0
@rem _timediff_m=0
@rem _timediff_s=1
@rem
@rem RESULT ..............: Creates the following environment variables
@rem _timediff -- result in the format "hh:mm'ss", using zero padding
@rem _timediff_h, _timediff_m and _timediff_s -- integer values, without zero padding
@rem _timediff_fh, _timediff_fm and _timediff_fs -- two digits string values, with zero padding
@rem REMARKS .............: Computation of a time difference bigger than 9 hours is not supported (eg. 00:00:00,00 10:00:00,00) and
@rem times that span across two different days (eg. 23:59:59,00 00:00:01,00) are not supported either
@rem HISTORY .............: 2013-01-18 - Added SETLOCAL/ENDLOCAL in order to protect caller's variables (eg. time1, time2)
@rem
@SETLOCAL
@set time1=%time%
@ %*
@call "%~dp0.\timediff-calc" "%time1%" "%time%"
@( ENDLOCAL
@set _timediff=%_timediff%
@set _timediff_h=%_timediff_h%
@set _timediff_m=%_timediff_m%
@set _timediff_s=%_timediff_s%
@set _timediff_fh=%_timediff_fh%
@set _timediff_fm=%_timediff_fm%
@set _timediff_fs=%_timediff_fs%
)
@rem MODULE ..............: timediff-calc
@rem AUTHOR ..............: Patryk Szczepankiewicz ( pszczepa [at] gmail [dot] com )
@rem REFERENCES ..........: https://p19z.blogspot.com/2011/08/process-execution-timing-in-batch.html
@rem USAGE ...............: set timex=%time%& call timediff-calc %time1% %time2%
@rem RESULT ..............: Creates the following environment variables
@rem _timediff -- result in the format "hh:mm'ss", using zero padding
@rem _timediff_h, _timediff_m and _timediff_s -- integer values, without zero padding
@rem _timediff_fh, _timediff_fm and _timediff_fs -- two digits string values, with zero padding
@rem EXEMPLE 1 ...........: call timediff-calc 12:23:40,24 20:09:12,25 (fr regional settings)
@rem EXEMPLE 2 ...........: call timediff-calc 12:23:40.24 20:09:12.25 (uk regional settings)
@rem EXEMPLE 3 ...........: call timediff-calc 0:10:33,23 0:10:33,23 (fr regional settings)
@rem REMARKS .............: Computation of a time difference bigger than 9 hours is not fully supported (eg. 00:00:00,00 10:00:00,00) and
@rem times that span across two different days (eg. 23:59:59,00 00:00:01,00) are not supported either. Note that
@rem the result might be correct though.
@rem TODO ................: see REMARKS, perform extensive testing
@rem HISTORY .............: 2013-01-18 - Added SETLOCAL/ENDLOCAL in order to protect caller's variables (eg. time1, time2)
@rem
@SETLOCAL
@set time1=%~1
@set time2=%~3
@if "%time2%"=="" set time2=%~2
@rem ~ @echo [%~n0] get rid of space characters at first position...
@if "%time1:~0,1%"==" " set time1=%time1:~1%
@if "%time2:~0,1%"==" " set time2=%time2:~1%
@rem ~ @echo [%~n0] (time1 = "%time1%", time2 = "%time2%")
@rem ~ @echo [%~n0] replace symbols by spaces...
@set time1=%time1::= %
@set time2=%time2::= %
@rem ~ @echo [%~n0] (time1 = "%time1%", time2 = "%time2%")
@rem ~ @echo [%~n0] trim string to 8 characters "12 45 78" or "1 34 67."...
@set time1=%time1:~0,8%
@set time2=%time2:~0,8%
@rem ~ @echo [%~n0] (time1 = "%time1%", time2 = "%time2%")
@rem ~ @echo [%~n0] trim the last character if required...
@set time1=%time1:.=%
@set time2=%time2:.=%
@set time1=%time1:,=%
@set time2=%time2:,=%
@rem ~ @echo [%~n0] (time1 = "%time1%", time2 = "%time2%")
@rem ~ @echo [%~n0] do some other shit...
@if "%time1:~0,1%"=="0" if "%time1:~0,2%" neq "0 " set time1=%time1:~1,8%
@if "%time2:~0,1%"=="0" if "%time2:~0,2%" neq "0 " set time2=%time2:~1,8%
@rem ~ @echo [%~n0] (time1 = "%time1%", time2 = "%time2%")
@rem ~ @echo [%~n0] remove leading zeros...
@set time1=%time1: 0= %
@set time2=%time2: 0= %
@rem ~ @echo [%~n0] call computing subscript...
@call "%~dp0.\timediff-subCalc" %time1% %time2%
@rem ~ @echo [%~n0] add the zero paddings if required...
@set _timediff_fh=0%_timediff_h%
@set _timediff_fm=0%_timediff_m%
@set _timediff_fs=0%_timediff_s%
@set _timediff_fh=%_timediff_fh:~-2%
@set _timediff_fm=%_timediff_fm:~-2%
@set _timediff_fs=%_timediff_fs:~-2%
@set _timediff=%_timediff_fh%:%_timediff_fm%'%_timediff_fs%
@( ENDLOCAL
@set _timediff=%_timediff%
@set _timediff_h=%_timediff_h%
@set _timediff_m=%_timediff_m%
@set _timediff_s=%_timediff_s%
@set _timediff_fh=%_timediff_fh%
@set _timediff_fm=%_timediff_fm%
@set _timediff_fs=%_timediff_fs%
)
@rem MODULE ..............: timediff-subCalc
@rem AUTHOR ..............: Patryk Szczepankiewicz ( pszczepa [at] gmail [dot] com )
@rem REFERENCES ..........: https://p19z.blogspot.com/2011/08/process-execution-timing-in-batch.html
@rem http://forums.techguy.org/software-development/757804-calculating-time-differences-batch-script.html
@rem USAGE ...............: use "timediff-subCalc hr1 mn1 sc1 hr2 mn2 sc2"
@rem RESULT ..............: Creates the following environment variables
@rem _timediff_h, _timediff_m and _timediff_s -- integer values, without zero padding
@rem EXEMPLE .............: timediff-subCalc 12 23 40 20 9 12
@rem REMARKS .............: Computation of a time difference bigger than 9 hours is not fully supported (eg. 00:00:00,00 10:00:00,00) and
@rem times that span across two different days (eg. 23:59:59,00 00:00:01,00) are not supported either. Note that
@rem the result might be correct though.
@rem TODO ................: see REMARKS, perform extensive testing
@rem HISTORY .............: 2013-01-18 - Added SETLOCAL/ENDLOCAL in order to protect caller's variables (eg. time1, time2)
@rem
@SETLOCAL
@rem ~ @echo [%~n0] called with %*
@rem ~ @echo [%~n0] convert input hours to seconds...
@set /a hh1=60*60*%1
@set /a hh2=60*60*%4
@rem ~ @echo [%~n0] convert input minutes to seconds...
@set /a mm1=60*%2
@set /a mm2=60*%5
@rem ~ @echo [%~n0] input seconds...
@set /a ss1=%3
@set /a ss2=%6
@rem ~ @echo [%~n0] add input hours , minutes , seconds...
@set /a t1=hh1+mm1+ss1
@set /a t2=hh2+mm2+ss2
@rem ~ @echo [%~n0] calculate difference in seconds...
@set /a ds=t2-t1
@rem ~ @echo [%~n0] increment by one day...
@if %ds% LSS 0 set /a ds=%ds%+24*60*60
@rem ~ @echo [%~n0] reconvert back to original units...
@set /a h=(%ds%/3600)
@set /a m=(%ds%/60)-60*%h%
@set /a s=%ds%-60*(%ds%/60)
@rem ~ @echo [%~n0] hrs=%h% min=%m% sec=%s%
@set _timediff_h=%h%
@set _timediff_m=%m%
@set _timediff_s=%s%
@( ENDLOCAL
@set _timediff_h=%_timediff_h%
@set _timediff_m=%_timediff_m%
@set _timediff_s=%_timediff_s%
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment