Hundreds of seconds since midnight timer function

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSuser
Posts: 4
Joined: 25 Sep 2023 17:35

Hundreds of seconds since midnight timer function

#1 Post by DOSuser » 25 Sep 2023 18:39

The following DOS batch file will set an environment variable to the number of seconds since midnight use it to time actions or to check if a given amount of time has passed the julian date can be used for calculations that pass midnight
rename as timesec.bat

Code: Select all

@echo off
if %1.==. GOTO SHOW_HELP
if %1.==-?. GOTO SHOW_HELP
if %1.==/?. GOTO SHOW_HELP
set THETIME=%TIME%
rem Change fraction part for non-English localizations
set THETIME=%THETIME:,=.%
rem
set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
    for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
        set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a Jdate=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
 
rem Convert time to 100's of seconds
for /F "tokens=1-4 delims=:.," %%a in ("%THETIME%") do (
   set /A "%1=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

GOTO END
:SHOW_HELP
echo sets an enviroment variable to hundredths of seconds since midnight
echo example call timesec SEC1  sets enviroment variable SEC1 to hundreds of seconds
echo also sets Jdate = julian date so %Jdate% can be used to determine number of days  and if past midnight adjust seconds
echo takes 11/100 sec to run test with batch program:
echo "call timesec start & timeout /T 4 & timesec end & set /A delta= end - start & echo its %delta% versus 400"
echo "for seconds use  set /A delta=(end-start)/100  also set /A hour=start/60/60/100 set /A min=start/60/100-hour*60"

:END
EXIT /b

Post Reply