How to get the time difference in a script

  • Follow


I am trying to the followings on Solaris 9:

step_start
time1=date


process some logic


time2=date (this could be next day)


time_diff=time1 - time2


I want to get hours and minutes from time_diff


Can anyone tell me how to get this please?

I have an answer for this for Linux machine using GNU date but I don't
think GNU date will work on Sun Solaris. 

Many Thanks

0
Reply kang_uni (8) 4/26/2006 4:45:59 PM

On 26 Apr 2006 09:45:59 -0700, Herbert wrote:
> I am trying to the followings on Solaris 9:
>
> step_start
> time1=date
>
>
> process some logic
>
>
> time2=date (this could be next day)
>
>
> time_diff=time1 - time2
>
>
> I want to get hours and minutes from time_diff
>
>
> Can anyone tell me how to get this please?
>
> I have an answer for this for Linux machine using GNU date but I don't
> think GNU date will work on Sun Solaris. 
[...]

#! /usr/xpg4/bin/sh -
PATH=$(getconf PATH):$PATH
export PATH

date1=$(awk 'BEGIN{srand();print srand()}')
# ... processing ...
date2=$(awk 'BEGIN{srand();print srand()}')

time_diff_in_sec=$(($date2 - $date1))
min=$((time_diff_in_sec / 60))
hour=$(($min / 60))
min=$(($min - $hour * 60))

printf '%s\n' "I spent ${hour}:$min processing' 

-- 
Stephane
0
Reply Stephane 4/26/2006 4:56:06 PM


If you have GNU date installed (often it's called gdate) you can use

start=`gdate +"%s"`
stuff....
finish=`gdate +"%s`
duration=$(( $finish - $start ))
echo "It took $duration seconds"

0
Reply News 4/26/2006 5:22:02 PM


I keep a small C program that does gte following:

main()
{
time_t     t;

      return time(NULL);
}


Something like that. Call the program getsecs. Then just call it form
your shell program.

start=getsecs
.....
elapsedtime=getsecs-$start


something like that.


Jim

0
Reply balson 4/27/2006 2:41:22 AM

On 26 Apr 2006 17:22:02 GMT, News wrote:
> If you have GNU date installed (often it's called gdate) you can use
>
> start=`gdate +"%s"`
> stuff....
> finish=`gdate +"%s`
> duration=$(( $finish - $start ))
> echo "It took $duration seconds"

gdate +%s

does the same thing as:

/usr/xpg4/bin/awk 'BEGIN{srand();print srand()}'

gdate may do it less efficiently though as it will waste some
time figuring out the current timezone.

(compare with truss -u:: for both).

The OP mentionned he couldn't use GNU date.

-- 
Stephane
0
Reply Stephane 4/27/2006 2:19:14 PM

On 27 Apr 2006 14:19:14 GMT Stephane Chazelas <stephane_chazelas@yahoo.fr> wrote:
> /usr/xpg4/bin/awk 'BEGIN{srand();print srand()}'

What a great trick!  Shame this isn't documented in Sun's man page.

-frank
0
Reply Frank 4/27/2006 6:32:17 PM

In article <m2hd4euaxa.fsf@maguro.local>,
Frank Cusack  <fcusack@fcusack.com> wrote:
>On 27 Apr 2006 14:19:14 GMT Stephane Chazelas <stephane_chazelas@yahoo.fr> wrote:
>> /usr/xpg4/bin/awk 'BEGIN{srand();print srand()}'
>
>What a great trick!  Shame this isn't documented in Sun's man page.

Is it part of the POSIX spec of awk?   :-|

BTW, 

  nawk 'BEGIN{print srand()}' 

saves a few characters and has the same effect. Don't omit the "n".

-- 
Chris Thompson
Email: cet1 [at] cam.ac.uk
1
Reply cet1 4/28/2006 9:49:03 AM

On 28 Apr 2006 09:49:03 GMT, Chris Thompson wrote:
> In article <m2hd4euaxa.fsf@maguro.local>,
> Frank Cusack  <fcusack@fcusack.com> wrote:
>>On 27 Apr 2006 14:19:14 GMT Stephane Chazelas <stephane_chazelas@yahoo.fr> wrote:
>>> /usr/xpg4/bin/awk 'BEGIN{srand();print srand()}'
>>
>>What a great trick!  Shame this isn't documented in Sun's man page.
>
> Is it part of the POSIX spec of awk?   :-|

Well, POSIX doesn't explicitely say that srand() initialises the
random generator with the number of seconds since the epoch, it
says it does it based on the current time, it could be anything
such as the number of miliseconds since midnight. But every awk
implemention I know use the result of time(0) for that.

>
> BTW, 
>
>   nawk 'BEGIN{print srand()}' 
>
> saves a few characters and has the same effect. Don't omit the "n".

But is neither standard nor portable nor documented, so should
not be used.

-- 
Stephane
0
Reply Stephane 4/28/2006 11:26:20 AM

Herbert wrote:
> I am trying to the followings on Solaris 9:

Korn shell has SECONDS so

start=$SECONDS

process_logic

end=$SECONDS

time_diff=$(( ($end - $start)/60))

echo "ran for  $(( $time_diff/60 )):$((time_diff%60))"

--chris

> 
> step_start
> time1=date
> 
> 
> process some logic
> 
> 
> time2=date (this could be next day)
> 
> 
> time_diff=time1 - time2
> 
> 
> I want to get hours and minutes from time_diff
> 
> 
> Can anyone tell me how to get this please?
> 
> I have an answer for this for Linux machine using GNU date but I don't
> think GNU date will work on Sun Solaris. 
> 
> Many Thanks
> 
0
Reply Chris 4/28/2006 1:15:13 PM

Herbert wrote:

> I am trying to the followings on Solaris 9:
> 
> step_start
> time1=date
> 
> 
> process some logic
> 
> 
> time2=date (this could be next day)
> 
> 
> time_diff=time1 - time2
> 
> 
> I want to get hours and minutes from time_diff
> 
> 
> Can anyone tell me how to get this please?
> 
> I have an answer for this for Linux machine using GNU date but I don't
> think GNU date will work on Sun Solaris. 
> 
> Many Thanks
> 

The "time" command can be used to time things; e.g. a program or a 
script.   See man time.
0
Reply Richard 4/28/2006 4:53:20 PM

9 Replies
2166 Views

(page loaded in 0.137 seconds)

Similiar Articles:













7/20/2012 2:51:05 PM


Reply: