这是我尝试批量测量时差。
它尊重 %TIME% 的区域格式,不考虑时间和小数分隔符的字符类型。
代码已注释,但我也会在这里描述。
它很灵活,因此也可以用于标准化非标准时间值
主要功能:timediff
:: timediff
:: Input and output format is the same format as %TIME%
:: If EndTime is less than StartTime then:
:: EndTime will be treated as a time in the next day
:: in that case, function measures time difference between a maximum distance of 24 hours minus 1 centisecond
:: time elements can have values greater than their standard maximum value ex: 12:247:853.5214
:: provided than the total represented time does not exceed 24*360000 centiseconds
:: otherwise the result will not be meaningful.
:: If EndTime is greater than or equals to StartTime then:
:: No formal limitation applies to the value of elements,
:: except that total represented time can not exceed 2147483647 centiseconds.
:timediff <outDiff> <inStartTime> <inEndTime>
(
setlocal EnableDelayedExpansion
set "Input=!%~2! !%~3!"
for /F "tokens=1,3 delims=0123456789 " %%A in ("!Input!") do set "time.delims=%%A%%B "
)
for /F "tokens=1-8 delims=%time.delims%" %%a in ("%Input%") do (
for %%A in ("@h1=%%a" "@m1=%%b" "@s1=%%c" "@c1=%%d" "@h2=%%e" "@m2=%%f" "@s2=%%g" "@c2=%%h") do (
for /F "tokens=1,2 delims==" %%A in ("%%~A") do (
for /F "tokens=* delims=0" %%B in ("%%B") do set "%%A=%%B"
)
)
set /a "@d=(@h2-@h1)*360000+(@m2-@m1)*6000+(@s2-@s1)*100+(@c2-@c1), @sign=(@d>>31)&1, @d+=(@sign*24*360000), @h=(@d/360000), @d%%=360000, @m=@d/6000, @d%%=6000, @s=@d/100, @c=@d%%100"
)
(
if %@h% LEQ 9 set "@h=0%@h%"
if %@m% LEQ 9 set "@m=0%@m%"
if %@s% LEQ 9 set "@s=0%@s%"
if %@c% LEQ 9 set "@c=0%@c%"
)
(
endlocal
set "%~1=%@h%%time.delims:~0,1%%@m%%time.delims:~0,1%%@s%%time.delims:~1,1%%@c%"
exit /b
)
示例:
@echo off
setlocal EnableExtensions
set "TIME="
set "Start=%TIME%"
REM Do some stuff here...
set "End=%TIME%"
call :timediff Elapsed Start End
echo Elapsed Time: %Elapsed%
pause
exit /b
:: put the :timediff function here
:timediff 函数说明:
function prototype :timediff <outDiff> <inStartTime> <inEndTime>
输入输出格式同%TIME%
从左到右取3个参数:
Param1: 将结果保存到的环境变量的名称。
Param2: 要传递给包含 StartTime 字符串的函数的环境变量的名称Param3: 要传递给包含 EndTime 字符串的函数的环境变量的名称
如果 EndTime 小于 StartTime 则:
EndTime 将被视为第二天的时间
在这种情况下,该函数测量 24 小时的最大距离减去 1 厘秒之间的时间差
时间元素的值可以大于其标准最大值,例如:12:247:853.5214
提供的总表示时间不超过 24*360000 厘秒或 (24:00:00.00) 否则结果将无意义。
如果 EndTime 大于或等于 StartTime,则:
元素的值没有正式的限制,
除了总表示时间不能超过 2147483647 厘秒。
更多具有文字和非标准时间值的示例
EndTime 小于 StartTime 的文字示例:
@echo off
setlocal EnableExtensions
set "start=23:57:33,12"
set "end=00:02:19,41"
call :timediff dif start end
echo Start Time: %start%
echo End Time: %end%
echo,
echo Difference: %dif%
echo,
pause
exit /b
:: put the :timediff function here
开始时间:23:57:33,12
结束时间:00:02:19,41
差异:00:04:46,29
@echo off
setlocal EnableExtensions
set "start=00:00:00.00"
set "end=27:2457:433.85935"
call :timediff normalized start end
echo,
echo %end% is equivalent to %normalized%
echo,
pause
exit /b
:: put the :timediff function here
27:2457:433.85935 相当于 68:18:32.35
@echo off
setlocal EnableExtensions
set "start=00:00:00.00"
set "end=00:00:00.2147483647"
call :timediff normalized start end
echo,
echo 2147483647 centiseconds equals to %normalized%
echo,
pause
exit /b
:: put the :timediff function here
2147483647 厘秒等于 5965:13:56.47