I use a code like this to get current time in Windows Bash:
set hour=%TIME:~0,2%
set minute=%TIME:~3,2%
set second=%TIME:~6,2%
But I need a current time +13 seconds. Is there any ways to get this?
I use a code like this to get current time in Windows Bash:
set hour=%TIME:~0,2%
set minute=%TIME:~3,2%
set second=%TIME:~6,2%
But I need a current time +13 seconds. Is there any ways to get this?
Assumming a 24h time hh:mm:ss,cc format,
@echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=1-3 delims=:., " %%a in ("%time%") do (
set /a "second=100%%c %% 100 +13", ^
"minute=100%%b %% 100 +second/60", ^
"hour=(100%%a %% 100 +minute/60) %% 24 +100", ^
"minute=minute %% 60 +100", ^
"second=second %% 60 +100"
)
echo %time%
echo %hour:~-2%:%minute:~-2%:%second:~-2%
The time string is tokenized (yes, substring operations are equally valid, but i see this as an easier way), so the hour is retrieved in %%a
, minutes in %%b
and seconds in %%c
and the calcs done
For the three hour elements, the retrieved value from the %time%
string are prefixed with 100
and then a mod 100
operation is done, all to avoid the problem with 08
and 09
values being considered as wrong octal values. Once the correct decimal values are retrieved we can operate
second = currentSeconds + 13
minute = currentMinutes + 1 ( if second > 60 )
hour = currentHour + 1 (if minute > 60 ) adjusted to 24h + 100 (for padding)
minute = minute adjusted to 0-59 range + 100 (for padding)
second = second adjusted to 0-59 range + 100 (for padding)
We end with values in the range 100-159 in second, 100-159 in minute, 100-123 in hour. That way we can properly output padded time elements retrieving the last two digits from each of the variables.
Something like the other solutions, but I calculate first the seconds of the day, then adding 13.
And then format the resulting number back to a hh:mm:ss format.
rem ** Calculate the seconds of the day
for /f "tokens=1-3 delims=:., " %%a in ("%time: =0%") do (
set /a "secOfDay=1%%c %% 100 + 60*(1%%b %% 100) + 3600*(1%%a %% 100 )"
)
REM ** Add the desired offset
set /a timePlus13sec=secOfDay+13
REM ** Convert the new time to the HH:mm:ss format
set /a sec=100+timePlus13sec %% 60
set /a temp=timePlus13sec / 60
set /a min=100+temp %% 60
set /a hour=100+(temp / 60) %% 24
echo %hour:~-2%:%min:~-2%:%sec:~-2%
This method uses JScript auxiliary code, so it is always correct:
@echo off
echo d=new Date();d.setTime(d.getTime()+13000);WScript.Echo(d.toString().split(' ')[3]);> test.js
for /F %%a in ('cscript //nologo test.js') do set timeAhead=%%a
echo Time plus 13 seconds: %timeAhead%
I need a current time +13 seconds. Adding 13 and seconds seems to be easiest task, but there are some intricacies:
set /A
that start with zeros are treated as
octal but 08 and 09 are not valid octal digits. Cf. next workaround with
%%
modulus operator: set /A "val=100%lav%%%100"
The script:
@ECHO OFF >NUL
@SETLOCAL
set "myTIME=%TIME%"
call :plus13 "%myTIME%" 6 13 60 second carry
call :plus13 "%myTIME%" 3 %carry% 60 minute carry
call :plus13 "%myTIME%" 0 %carry% 24 hour carry
echo "%myTIME%"
echo "%hour% %minute% %second%"
@ENDLOCAL
@goto :eof
:: plus13 procedure
:: %1 = time value
:: %2 = position in time
:: %3 = value to add
:: %4 = threshold
:: %5 = name of time variable
:: %6 = name of carry variable
:plus13
@SETLOCAL enableextensions enabledelayedexpansion
set "lav=%~1"
set "lav=!lav:~%2,2!"
set /A "val=100%lav%%%100"
set /A "val+=%3"
if %val% GEQ %4 (
set /A "val-=%4"
set /A "car=1"
) else (set /a "car=0")
if %val% LSS 10 set "val=0%val%"
@ENDLOCAL&set %5=%val%&set %6=%car%&goto :eof
Output:
C:\...>time13s
"11:51:49,50"
"11 52 02"