JSI Tip 0863. Time Math.

Jerold Schulman

November 23, 1998

2 Min Read
ITPro Today logo in a gray background | ITPro Today


In tip 721, we scripted a general purpose Date Math routine.

JSITimeM.bat is a general purpose Time Math routine which is called with the following syntax:

JSItimeM [HH1 MM1 +/- HH2 MM2]

When called without parameters, JSITimeM returns the following environment variables (your system must be configured for a 24 hour clock):

AHH

 is the current hour. 

AMM

 is the current minute. 

NHH

 is a numeric variable that contains the current hour. 

NMM

 is a numeric variable that contains the current minute. 

NSEC

 is a numeric variable which contains the number of seconds since mid-night. 

When called with parameters, JSITimeM returns:

AHH

 is the resultant hour. The range is 00 - 23. 

AMM

 is the resultant minute. The range is 00 - 59. 

NHH

 is a numeric variable that contains the resultant hour. 

NMM

 is a numeric variable that contains the resultant minute. 

NSEC

 is a numeric variable which contains the resultant number of seconds. A value of 999999 indicates an error in the parameters. If it is greater than 86399, the time returned is the next day. If negative, time2 was later than time1 during a subtraction. 

Examples:

- To return the current time:

call JSITimeM

- To cause a batch job to sleep until 05:00 PM, unless it is already later:

call JSITimeM
call JSITimeM 17 00 - %AHH% %AMM%
if %NSEC% EQU 999999 goto error
if %NSEC% LSS 0 goto continue
sleep %NSEC%
:continue

JSITimeM.bat contains:

@echo offset NSEC=set NEG=Nif /i "%3"
"+" goto OK1If /i "%3"
"-" goto OK1if "%3"
"" goto OK1:syntax@echo JSITimeM [HH1 MM1 +/- HH2 MM2]set /a NSEC=999999set N1HH=set N1MM=set N2HH=set N2MM=set NEG=goto end:OK1if not "%1"
"" goto OK2for /f "Tokens=1,2 Delims=:" %%i in ('time /t') do call :now %%i %%jgoto end:nowset AMM=%2set NHH="%1"set NMM="%2"set NHH=%NHH:"=%set /a NHH=100%NHH%%%100set AHH=%NHH%if not %NHH% GTR 9 set AHH=0%AHH% set NMM=%NMM:"=%set /a NMM=100%NMM%%%100set /a NSEC=%NHH%*3600set /a WSEC=%NMM%*60set /a NSEC=%NSEC% + %WSEC%set WSEC=goto end:OK2if "%5"
"" goto syntaxset N1HH="%1"set N1HH=%N1HH:"=%set /a N1HH=100%N1HH%%%100set N1MM="%2"set N1MM=%N1MM:"=%set /a N1MM=100%N1MM%%%100set N2HH="%4"set N2HH=%N2HH:"=%set /a N2HH=100%N2HH%%%100set N2MM="%5"set N2MM=%N2MM:"=%set /a N2MM=100%N2MM%%%100if %N1HH% GTR 23 goto syntax if %N2HH% GTR 23 goto syntax if %N1MM% GTR 59 goto syntax if %N2MM% GTR 59 goto syntax If /i "%3"
"-" goto OK8set /a W1SEC=%N1HH%*3600set /a W2SEC=%N1MM%*60set /a NSEC=%W1SEC% + %W2SEC%set /a W1SEC=%N2HH%*3600set /a W2SEC=%N2MM%*60set /a NSEC=%NSEC% + %W1SEC% + %W2SEC%if %NSEC% GTR 86399 set /a NHH=(%NSEC% - 86400):OK3set /a NHH=%NSEC%/3600set /a W1SEC=%NHH%*3600set /a W2SEC=%NSEC% - %W1SEC%set /a NMM=%W2SEC/60if %NHH% GTR 9 goto OK4set AHH=0%NHH%goto OK5:OK4set AHH=%NHH%:OK5if %NMM% GTR 9 goto OK6set AMM=0%NMM%goto OK7:OK6set AMM=%NMM%:OK7if "%NEG%"=="N" goto OK7Aset /a NSEC=0-%NSEC%:OK7Aset N1HH=set N1MM=set N2HH=set N2MM=set W1SEC=Set W2SEC=set NEG=goto end:OK8set /a W1SEC=%N1HH%*3600set /a W2SEC=%N1MM%*60set /a NSEC=%W1SEC% + %W2SEC%set /a W1SEC=%N2HH%*3600set /a W2SEC=%N2MM%*60set /a NSEC=%NSEC% - %W1SEC% - %W2SEC%if %NSEC% LSS 0 goto OK9goto OK3:OK9set NEG=Yset /a NSEC=0-%NSEC%set /a NSEC=86400 - %NSEC%goto OK3:end



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like