Timestamp

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Timestamp

#1 Post by Adrianvdh » 11 Aug 2013 04:40

Hello everyone...

I think this has been posted before, if so I am sorry.
I am batch file hat has to process big chunks of operations. So when it does with the processing, I would like to say duration: 54 seconds. So I would need a time stamp on that start of the program and the end and show the result.

Any help would be great.
Thanks for your time :).

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Timestamp

#2 Post by foxidrive » 11 Aug 2013 04:54

getTimestamp.bat is a helper batch file to manipulate and measure all sorts of time and date variables and can be found here - viewtopic.php?p=27422#p27422

Here is how to calculate time elapsed:

Code: Select all

@echo off
setlocal

call getTimestamp -f {ums} -r t1
:: Some long running process here
call getTimestamp -f {ums} -r t2

:: This computes the elapsed time as decimal hours.
:: It supports both positive and negative intervals.
call getTimestamp -d %t2%-%t1% -f "{uhd} hours"

:: This computes the elapsed time as days, hours, mins, secs, ms
:: It only supports positive intervals
call getTimestamp -d %t2%-%t1% -f "{ud} days {hh}:{nn}:{ss}.{fff}" -u

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Timestamp

#3 Post by Adrianvdh » 11 Aug 2013 05:23

All that code just for a simple function? No thanks. Correct me if I am wrong but hell no< I cannot have that overflow my batch file. :) :(

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Timestamp

#4 Post by foxidrive » 11 Aug 2013 05:50

All that code does a hell of a lot more. :D

You put it on your path and you call it. That's simple.

It's like having sed, awk, gawk, and even your text editor on your path.
There are two other very helpful batch programs called repl.bat and findrepl.bat too.

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Timestamp

#5 Post by Adrianvdh » 11 Aug 2013 06:38

Isn't there a few liner way of doing it, could narrow it down for me?

If you don't want to, it's fine I don't need the Duration label then :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Timestamp

#6 Post by foxidrive » 11 Aug 2013 06:47

Not a one liner... you have to store the start time, store the end time, and then calculate the difference and adjust for minutes overflowing, hours overflowing, crossing midnight etc.

To simply list the two times you can use this:

Code: Select all

echo %time%
call batchroutine.bat
echo %time%



You will find prebuilt routines if you look for them in google though, also on alt.msdos.batch.nt the Usenet newsgroup.

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: Timestamp

#7 Post by Squashman » 11 Aug 2013 06:53

Adrianvdh wrote:All that code just for a simple function? No thanks. Correct me if I am wrong but hell no< I cannot have that overflow my batch file. :) :(

Two thirds of it is comments explaining the options it can do.

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: Timestamp

#8 Post by brinda » 11 Aug 2013 07:12

foxidrive wrote:All that code does a hell of a lot more. :D

You put it on your path and you call it. That's simple.

It's like having sed, awk, gawk, and even your text editor on your path.
There are two other very helpful batch programs called repl.bat and findrepl.bat too.



:idea: it would be nice if there is a pin-it function so all these wonderful script could be displayed :D

otherwise they disappear into the forum

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: Timestamp

#9 Post by Squashman » 11 Aug 2013 07:15

some of them do make it into the library. I don't know the last time it has been updated with anything new.

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: Timestamp

#10 Post by brinda » 11 Aug 2013 07:22

squashman,
that would be a great addition indeed. thanks. :D

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Timestamp

#11 Post by Adrianvdh » 11 Aug 2013 07:29

Hey everyone...

I have this code as a function...

:GetInternational
for /f "tokens=1,2*" %%a in ('reg query "HKCU\Control Panel\International"^|find "REG_SZ" ') do set "%%a=%%c"
exit /b
:GetSecs "dateIn" "timeIn" secondsOut
setlocal
set "dateIn=%~1"
for /f "tokens=2" %%i in ("%dateIn%") do set "dateIn=%%i"
for /f "tokens=1-3 delims=%sDate%" %%a in ("%dateIn%") do (
if %iDate%==0 set /a mm=100%%a%%100,dd=100%%b%%100,yy=10000%%c%%10000
if %iDate%==1 set /a dd=100%%a%%100,mm=100%%b%%100,yy=10000%%c%%10000
if %iDate%==2 set /a yy=10000%%a%%10000,mm=100%%b%%100,dd=100%%c%%100
)
for /f "tokens=1-3 delims=%sTime%%sDecimal% " %%a in ("%~2") do (
set "hh=%%a"
set "nn=%%b"
set "ss=%%c"
)
if 1%hh% lss 20 set hh=0%hh%
if "%nn:~2,1%" equ "p" if "%hh%" neq "12" (set "hh=1%hh%" &set /a hh-=88)
if "%nn:~2,1%" equ "a" if "%hh%" equ "12" set "hh=00"
if "%nn:~2,1%" geq "a" set "nn=%nn:~0,2%"
set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2,j=j/5+dd+y*365+y/4-y/100+y/400-2472633,j=j*86400+hh*3600+nn*60+ss
endlocal &set "%~3=%j%"
exit /b

and you would use it like this...

set "versionreleasedate1=2013/07/28"
set "versionreleasetime1=00:00:00.00"
call :GetInternational
call :GetSecs "%versionreleasedate1%" "%versionreleasetime1%" startsec
call :GetSecs "%date%" "%time%" stopsec
set /a versionday=(%stopsec%-%startsec%)/86400

Since I am not advanced with batch files like this, I do not know how to modify this code to help me with...

Code: Select all

set startTime=%time%
:: a big process
set endTime=%time%


Then a function to calculate the difference. Any help?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Timestamp

#12 Post by foxidrive » 11 Aug 2013 07:42

There's also this utility from microsoft, to get elapsed time.

Code: Select all

---------------------------------------------------------------
TIMETHIS  :  Command Timing Utility  :  by kevina@microsoft.com
---------------------------------------------------------------

Usage : TIMETHIS "command"

  TimeThis executes the command specified by its arguments, then reports its
  run time in HH:MM:SS.TTT format.  Quotes around the command are required only
  when the command involves redirection via <, >, >>, or |, etc.  Quotes ensure
  that the redirection is applied to the command being timed, rather than the
  TimeThis command itself.

Examples :

  TimeThis DIR C:\WINNT
  TimeThis "DIR C:\WINNT | MORE"

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Timestamp

#13 Post by penpen » 11 Aug 2013 09:04

If you want to use the difference of timestamps with the procedures you gave this is done by:

Code: Select all

call :GetSecs "%date%" "%time%" startTime
:: a big process
call :GetSecs "%date%" "%time%" endTime
set /a diff=(%endTime%-%startTime%)
echo Duration: %diff% seconds

penpen

PS: If you want to measure how good or bad your code is in seconds then you should use timethis.

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Timestamp

#14 Post by Adrianvdh » 11 Aug 2013 09:19

Thanks man, you made my day...

I just realised after a few minutes try to figure out why it wouldn't work is, you need to add this code...

Code: Select all

call :GetInternational
call :GetSecs "%date%" "%time%" startTime
:: a big process
call :GetSecs "%date%" "%time%" endTime
set /a diff=(%endTime%-%startTime%)
echo Duration: %diff% seconds

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: Timestamp

#15 Post by brinda » 12 Aug 2013 13:08

a much shorter version by penpen from http://www.dostips.com/forum/viewtopic.php?f=3&t=4759

this will give

Code: Select all

D:\Documents\Downloads>timers
2013-07-13 03:04:27:953


script

Code: Select all

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment

::************ Batch portion ***********
@echo off
cscript //E:JScript //nologo "%~f0"
exit /b 0

*** JScript part ***/
var n   = new Date ();

WScript.echo ("" + ("" + (10000 + n.getYear ())).substring (1)
   + "-"
   + ("" + (100 + (n.getMonth () + 1))).substring (1)
   + "-"
   + ("" + (100 + n.getDate ())).substring (1)
   + " "
   + ("" + (100 + n.getHours ())).substring (1)
   + ":"
   + ("" + (100 + n.getMinutes ())).substring (1)
   + ":"
   + ("" + (100 + n.getSeconds ())).substring (1)
   + ":"
   + ("" + (1000 + n.getMilliseconds ())).substring (1)
   + " "
);


or in milliseconds

Code: Select all

D:\Documents\Downloads>timerss
1376371433421


script

Code: Select all

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment

::************ Batch portion ***********
@echo off
cscript //E:JScript //nologo "%~f0"
exit /b 0

*** JScript part ***/
WScript.echo (new Date().getTime());


in pure batch would be here
http://stackoverflow.com/questions/4313897/timer-in-dos-batch-file.

the script below within the same url above is probably corrected answer -accurate version. The same reference made by dave in

Code: Select all

https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/FvirC1hzNRQ


script

Code: Select all

@echo off
:loop
set TA=%time% & echo %time%--A

echo Put Your Code Here...

set TB=%time% & echo %time%--B

call:Timediff %TA% %TB% Tab
for /f "tokens=1-3 delims=:" %%a in ("%TAB%")do echo Time difference: %%a hours %%b minutes and %%c seconds
pause
goto:loop

:: /* ---------- Timediff ---------------
:Timediff [%t1%] [%t2%] [par|0]
for %%a in (+%1 +%2 +%3)do if "%%a"=="+" echo No Parameters!!&exit/b
setlocal enabledelayedexpansion
:timediff_1
set P2=%~1&set "P2=!P2::=!"
set/a P2=%P2:.=%-4000*(%P2:~,4%+60*%P2:~,2%)
if not "%3"=="" set P1=!P2!&shift&goto:timediff_1
if !P2! geq !P1! (set/a df=!P2!-!P1!) else set/a df=!P2!-!P1!+8640000
set/a h=df/360000,m=df%%360000/6000,s=df%%6000/100,pt=df%%100
if %pt% leq 9 set pt=0%pt%
endlocal&if %2.==0. (echo\%h%:%m%:%s%.%pt%) else set %2=%h%:%m%:%s%.%pt%&goto:eof
:: ------------- Timediff ------------- */

Post Reply