0

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
Alexey Berezuev
  • 785
  • 9
  • 27

4 Answers4

2

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.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • 2
    What if the 13 seconds spans the transition from standard to daylight savings time (or vice versa)? - evil grin :-) – dbenham Dec 19 '14 at 14:36
  • @dbenham, ;) it can be [worse](http://stackoverflow.com/a/6841479/2861476). But anyway, a time plus 13 seconds operation is not affected by daylight savings, intercalar seconds, or UTC coordination. It is a pure only time operation, there is no date, so none of the indicated considerations are applicable. **BUT** of course this is *"probably"* something to take in consideration in the rest of the code that uses this calcs. Thank you. – MC ND Dec 19 '14 at 14:52
1

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%
jeb
  • 78,592
  • 17
  • 171
  • 225
1

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%
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

I need a current time +13 seconds. Adding 13 and seconds seems to be easiest task, but there are some intricacies:

  • all numeric values in 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"
  • more than 59 seconds value affects minutes, more than 59 minutes value influences hours, more than 23 hours value seems to be impossible as well...

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"
JosefZ
  • 28,460
  • 5
  • 44
  • 83