is it possible to calculated time elipsed just using command line?

T

thinktwice

i only find "date","time" command,
set begintime=%time%
...
set endtime=%time%

want to know endtime-begintime, i don't know whether it's possible?
 
A

Al Dunbar

thinktwice said:
i only find "date","time" command,
set begintime=%time%
..
set endtime=%time%

want to know endtime-begintime, i don't know whether it's possible?

that's a pretty challenging goal given the computational limitations of
batch scripting, and the need to compensate for the different possible time
formats (24 hr vs AM/PM, leading zero suppression 09:12:34 vs 9:12:34,
hh:mm:ss vs hh:mm:ss.cc), and the possibility of the script running through
midnight. IMHO, there are .exe utilities available to do this kind of thing.
If you want to roll your own, I'd recommend doing it in some language that
has built-in date calculations.

/Al
 
T

thinktwice

thanks Al, what if i know the time format is 24hr? my batch file won't
run so long, i can think it'll finish in the same day.
 
A

Al Dunbar

thanks Al, what if i know the time format is 24hr? my batch file won't
run so long, i can think it'll finish in the same day.
that's a pretty challenging goal given the computational limitations of
batch scripting, and the need to compensate for the different possible
time
formats (24 hr vs AM/PM, leading zero suppression 09:12:34 vs 9:12:34,
hh:mm:ss vs hh:mm:ss.cc), and the possibility of the script running
through
midnight. IMHO, there are .exe utilities available to do this kind of
thing.
If you want to roll your own, I'd recommend doing it in some language that
has built-in date calculations.

/Al

OK, here's a demo of a batch routine (":HMS2S") that converts HH:MM:SS.CC
values into seconds, and subtracts the before and after values to show the
duration in seconds. it can handle the troublesome "08" and "09" values,
durations close to a full day, and will return a negative duration if the
start time is greater than the end time. Feel free to adapt to your
purposes.

/Al

@echo off

call:HMS2S _A %time%
ping -n 7 localhost
call:HMS2S _B %time%

set/a _D = _B - _A
echo/start %_A%
echo/stop %_B%
echo/duration %_D%

call:HMS2S _A 08:08:08
call:HMS2S _B 09:09:09

set/a _D = _B - _A
echo/start %_A%
echo/stop %_B%
echo/duration %_D%

call:HMS2S _A 09:09:09
call:HMS2S _B 08:08:08

set/a _D = _B - _A
echo/start %_A%
echo/stop %_B%
echo/duration %_D%

call:HMS2S _A 01:00:00
call:HMS2S _B 23:59:59

set/a _D = _B - _A
echo/start %_A%
echo/stop %_B%
echo/duration %_D%

pause

GOTO:EOF


:HMS2S
echo/time: %2

(set _time=%2)
(set _time=%_time:~0,8%)
for /f "tokens=1,2,3 delims=:" %%A in ("%_time%") do (
set __HH=1%%A
set __MM=1%%B
set __SS=1%%C
)

rem ::: octal fix for 08 and 09
set/a __HH = __HH - 100
set/a __MM = __MM - 100
set/a __SS = __SS - 100

set/a %1 = __SS + 60 * ( __MM + 24 * ( __HH ) )

GOTO:EOF
 
T

thinktwice

really appreciated your help , Al :)
thanks Al, whatifi know the time format is 24hr? my batch file won't
run so long, i can think it'll finish in the same day.




OK, here's a demo of a batch routine (":HMS2S") that converts HH:MM:SS.CC
values into seconds, and subtracts the before and after values to show the
duration in seconds. it can handle the troublesome "08" and "09" values,
durations close to a full day, and will return a negative durationifthe
start time is greater than the end time. Feel free to adapt to your
purposes.

/Al

@echo off

call:HMS2S _A %time%
ping -n 7 localhost
call:HMS2S _B %time%

set/a _D = _B - _A
echo/start %_A%
echo/stop %_B%
echo/duration %_D%

call:HMS2S _A 08:08:08
call:HMS2S _B 09:09:09

set/a _D = _B - _A
echo/start %_A%
echo/stop %_B%
echo/duration %_D%

call:HMS2S _A 09:09:09
call:HMS2S _B 08:08:08

set/a _D = _B - _A
echo/start %_A%
echo/stop %_B%
echo/duration %_D%

call:HMS2S _A 01:00:00
call:HMS2S _B 23:59:59

set/a _D = _B - _A
echo/start %_A%
echo/stop %_B%
echo/duration %_D%

pause

GOTO:EOF

:HMS2S
echo/time: %2

(set _time=%2)
(set _time=%_time:~0,8%)
for /f "tokens=1,2,3 delims=:" %%A in ("%_time%") do (
set __HH=1%%A
set __MM=1%%B
set __SS=1%%C
)

rem ::: octal fix for 08 and 09
set/a __HH = __HH - 100
set/a __MM = __MM - 100
set/a __SS = __SS - 100

set/a %1 = __SS + 60 * ( __MM + 24 * ( __HH ) )

GOTO:EOF
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top