This is the script code [save as placed in the current path:
:etime <begin_time> <end_time> <return>
rem The execution time of the task tested shall not exceed 1 day // Skinny version
setlocal&set be=%~1:%~2&set cc=(%%d-%%a)*360000+(1%%e-1%%b)*6000+1%%f-1%%c&set dy=-8640000
for /f "delims=: tokens=1-6" %%a in ("%be:.=%")do endlocal&set/a %3=%cc%,%3+=%dy%*("%3>>31")&exit/b
Function batch etime for calculating two time spreads
Today I have been interested in Dafa and thought about many bat issues, so I have been overnight
I saw a "function" that asked for time difference in the forum was disturbed and called many places (mostly test the code execution efficiency)
:time0
:: Calculate the time difference (package)
@echo off&setlocal&set /a n=0&rem code Follow the wind @
for /f "tokens=1-8 delims=.: " %%a in ("%~1:%~2") do (
set /a n+=10%%a%%100*360000+10%%b%%100*6000+10%%c%%100*100+10%%d%%100
set /a n-=10%%e%%100*360000+10%%f%%100*6000+10%%g%%100*100+10%%h%%100)
set /a s=n/360000,n=n%%360000,f=n/6000,n=n%%6000,m=n/100,n=n%%100
set "ok=%s% hours %f% minutes %m% seconds %n% milliseconds"
endlocal&set %~3=%ok:-=%&goto :EOF
The algorithm of this code is to find the time point at 0:00:00.00 in the morning and then calculate the time difference between any time point and the early morning (unit runs seconds)
Then find the time difference between any two time points, which is the difference between the number of time points in the morning time points.
Some techniques are used to handle illegal octal numbers like 09. There are two time parameters that are in sequence and can be fully specified.
But this line of code can be omitted (since it is often lost, the smaller the volume, the better):
@echo off&setlocal&set/a n=0&set "s=+:%~1^&echo -:%~2"
for /f "tokens=1-5 delims=.:" %%a in ('echo %s%') do (rem code with wind @
set/a n%%a=10%%b%%100*360000+10%%c%%100*6000+10%%d%%100*100+10%%e%%100)
set /a s=n/360000,n=n%%360000,f=n/6000,n=n%%6000,m=n/100,n=n%%100
set "ok=%s% hours %f% minutes %m% seconds %n% milliseconds"
endlocal&(if %3. equ . (echo %ok:-=%) else set %~3=%ok:-=%)&exit/b
After further research, there is a shorter version
I wrote this code in cn-dos, and today I optimized it to be shorter
The code algorithm goes deeper:
Use hmsw (each letter represents a two-digit number) to represent standard time
We remember that the relative time of his early morning is Tx
Tx=hmsw time point-0:00:00.00 time point=h*3600*100+m*60*100+100*s+w
hmsw 8-bit decimal number represents the time (unit 0.01 seconds) which is the second time that hmsw runs
hmsw=w+100*s+10000*m+1000000*h
hmsw-Tx=640000*h+4000*m
So Tx=hmsw-(640000*h+4000*m)=hmsw-4000*(160*h+m)
Then Tx_2-Tx_1=hmsw_2-hmsw_1-4000*(160*(h_2-h_1)+(m_2-m_1))
For illegal 8-digit numbers like 09, we can add 1 to each of them to ensure that they are decimal numbers and the difference remains unchanged.
For different days (this case is less, unless you call it near 0am) we will replace the negative sign with 8640000-
Then use set/a to assign value
It's the algorithm used in the following code
Tx_2-Tx_1=hmsw_2-hmsw_1-4000*(160*(1h_2-1h_1)+(1m_2-1m_1))
rem is compatible with time points across the sky, time format 00:00:00.00 or 0:00:00.00
:_difftime <Begin_Time> <End_Time> [ret] //Return the difference between two time points (unit 0.01 seconds)
Setlocal enabledelayedexpansion&set b=0%1&set e=0%2&set c=1!e:~-11!-1!b:~-11!&set c=!c::=!
set/a c=%c:.=%-4000*(160*(1%e:~-11,-9%-1%b:~-11,-9%)+1%e:~-8,-6%-1%b:~-8,-6%)
endlocal & (if %3.==. (echo %c:-=8640000-%) else set/a %3=%c:-=8640000-%)&exit/b
The addition of _ to difftime is to indicate that the subprocess is not written temporarily and also to the uniqueness of the tags of the library function in the future.
// Off topic:
For subprocess, if variable delay is enabled, in principle, all three lines can be written, so try to write as compactly as possible (because no one reads it), but for algorithms,
You should be able to be able to write and write with ideas. The more detailed the better. I found that no matter how well your code is written, no one will be able to modify it all, and will be slightly modified.
When you first write the code, you cannot consider the specific situations of everyone's use. Naturally, no one wants to read your code in detail, but your idea is
Others have provided a method, in this sense, the function of the function library plays the role of the method library