Time difference

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Time difference

#1 Post by miskox » 17 Feb 2015 09:28

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Time difference

#2 Post by dbenham » 17 Feb 2015 09:54

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
Last edited by dbenham on 18 Feb 2015 07:48, edited 1 time in total.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Time difference

#3 Post by miskox » 18 Feb 2015 07:22

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Time difference

#4 Post by dbenham » 18 Feb 2015 07:50

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

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Time difference

#5 Post by miskox » 19 Feb 2015 06:40

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

Post Reply