简体   繁体   English

回声设置特定时间

[英]Echo set specific time

I use a code like this to get current time in Windows Bash: 我使用这样的代码来获取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. 但是我需要当前时间+13秒。 Is there any ways to get this? 有什么办法可以做到这一点?

Assumming a 24h time hh:mm:ss,cc format, 假设24小时为hh:mm:ss,cc格式,

@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 时间字符串已标记化(是的,子字符串操作同样有效,但我认为这是一种更简单的方法),因此以%%a检索小时,以%%b检索分钟,以%%c检索秒,并完成计算

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. 对于三个小时的元素,从%time%字符串中检索的值以100为前缀,然后执行mod 100操作,所有这些都避免了将0809值视为错误的八进制值的问题。 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. 我们以100-159秒,100-159分钟,100-123小时的范围结束。 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. 类似于其他解决方案,但我先计算一天中的秒数,然后再加上13。
And then format the resulting number back to a hh:mm:ss format. 然后将结果数字格式化回hh:mm:ss格式。

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: 此方法使用JScript辅助代码,因此始终正确:

@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 . 我需要当前时间+13秒 Adding 13 and seconds seems to be easiest task, but there are some intricacies: 加13秒似乎是最简单的任务,但有一些复杂之处:

  • all numeric values in set /A that start with zeros are treated as octal but 08 and 09 are not valid octal digits. set /A中所有以零开头的数字值都被视为八进制,但08和09不是有效的八进制数字。 Cf. 参看 next workaround with %% modulus operator: set /A "val=100%lav%%%100" 使用%% 模量运算符的下一个解决方法: 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... 超过59秒的值会影响分钟,超过59分钟的值会影响小时,超过23小时的值似乎也是不可能的...

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM