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 off set NSEC= set NEG=N if /i "%3""+" goto OK1 If /i "%3"
"-" goto OK1 if "%3""" goto OK1 :syntax @echo JSITimeM \[HH1 MM1 +/- HH2 MM2\] set /a NSEC=999999 set N1HH= set N1MM= set N2HH= set N2MM= set NEG= goto end :OK1 if not "%1"
"" goto OK2 for /f "Tokens=1,2 Delims=:" %%i in ('time /t') do call :now %%i %%j goto end :now set AMM=%2 set NHH="%1" set NMM="%2" set NHH=%NHH:"=% set /a NHH=100%NHH%%%100 set AHH=%NHH% if not %NHH% GTR 9 set AHH=0%AHH% set NMM=%NMM:"=% set /a NMM=100%NMM%%%100 set /a NSEC=%NHH%*3600 set /a WSEC=%NMM%*60 set /a NSEC=%NSEC% + %WSEC% set WSEC= goto end :OK2 if "%5""" goto syntax set N1HH="%1" set N1HH=%N1HH:"=% set /a N1HH=100%N1HH%%%100 set N1MM="%2" set N1MM=%N1MM:"=% set /a N1MM=100%N1MM%%%100 set N2HH="%4" set N2HH=%N2HH:"=% set /a N2HH=100%N2HH%%%100 set N2MM="%5" set N2MM=%N2MM:"=% set /a N2MM=100%N2MM%%%100 if %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 OK8 set /a W1SEC=%N1HH%*3600 set /a W2SEC=%N1MM%*60 set /a NSEC=%W1SEC% + %W2SEC% set /a W1SEC=%N2HH%*3600 set /a W2SEC=%N2MM%*60 set /a NSEC=%NSEC% + %W1SEC% + %W2SEC% if %NSEC% GTR 86399 set /a NHH=(%NSEC% - 86400) :OK3 set /a NHH=%NSEC%/3600 set /a W1SEC=%NHH%*3600 set /a W2SEC=%NSEC% - %W1SEC% set /a NMM=%W2SEC/60 if %NHH% GTR 9 goto OK4 set AHH=0%NHH% goto OK5 :OK4 set AHH=%NHH% :OK5 if %NMM% GTR 9 goto OK6 set AMM=0%NMM% goto OK7 :OK6 set AMM=%NMM% :OK7 if "%NEG%"=="N" goto OK7A set /a NSEC=0-%NSEC% :OK7A set N1HH= set N1MM= set N2HH= set N2MM= set W1SEC= Set W2SEC= set NEG= goto end :OK8 set /a W1SEC=%N1HH%*3600 set /a W2SEC=%N1MM%*60 set /a NSEC=%W1SEC% + %W2SEC% set /a W1SEC=%N2HH%*3600 set /a W2SEC=%N2MM%*60 set /a NSEC=%NSEC% - %W1SEC% - %W2SEC% if %NSEC% LSS 0 goto OK9 goto OK3 :OK9 set NEG=Y set /a NSEC=0-%NSEC% set /a NSEC=86400 - %NSEC% goto OK3 :end
0 comments
Hide comments