Page 1 of 1

Time difference

Posted: 17 Feb 2015 09:28
by miskox
Hi all!

I found this excelent code at SO by Aacini (http://stackoverflow.com/a/9935540):

Code: Select all

@echo off

rem Get start time:
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
   set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

rem Any process here...

rem Get end time:
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
   set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

rem Get elapsed time:
set /A elapsed=end-start

rem Show elapsed time:
set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %cc% lss 10 set cc=0%cc%
echo %hh%:%mm%:%ss%,%cc%


I modified it slightly to fit my needs (removed hundrendths of a second becuase I don't need that) and then I added a 'Time left' info to my Progress bar (viewtopic.php?p=33603#p33603) (image hosting site is down at the moment).

Image
(this image is uploaded to a different server from the same company)

Looks nice.

Another question:

What would be the best solution if I start the process before midnight and when time changes from 23:xx:xx to 00:xx:00 to have a correct time difference? Maybe adding 24 to hours? Or maybe adding dates to start time and end time?

Saso

Re: Time difference

Posted: 17 Feb 2015 09:54
by dbenham
Yes, assuming your process takes less than 24 hours to complete, then all you need to do is conditionally at 24 hours if elapsed is less than 0.

Code: Select all

if %elapsed% lss 0 set /a elapsed+=24*60*60*100


Dave Benham

Re: Time difference

Posted: 18 Feb 2015 07:22
by miskox
dbenham wrote:Yes, assuming your process takes less than 24 hours to complete, then all you need to do is conditionally at 24 hours if elapsed is less than 0.

Code: Select all

if %elapsed% < 0 set /a elapsed+=24*60*60*100


Dave Benham


Thanks.

The process will not take more than 24 hours - the question was regarding the situation when time changes from 11PM to 12AM:

Example: process starts at 11:55 PM and finishes next day at 00:05 AM so the time left (ETA) will be calculated correctly.

Saso

Re: Time difference

Posted: 18 Feb 2015 07:50
by dbenham
That is what my code attempted to address, except I mistakenly used < instead of LSS. (The post is now fixed).

If midnight has not passed since the start, then elapsed will be positive. If midnight has passed, then elapsed will be negative, in which case you must add 24 hours.


Dave Benham

Re: Time difference

Posted: 19 Feb 2015 06:40
by miskox
dbenham wrote:That is what my code attempted to address...


Yes, of course. Again it has happened that I did not read the answer carefully enough. Sorry for that, my mistake.

Saso