Calculating the difference between 2 times
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > Calculating the difference between 2 times




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Calculating the difference between 2 times  
sony.antony@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-06 01:49 AM

I have 2 k-shell variables each holding a date in the form "MM/DD/YYY
hh-mm-ss".
I want to find out how many seconds elapsed between these dates.
Other than k-shell, I can use sed, awk, perl
Can somebody tell me how I can go about with this problem.

Thanks
--sony






[ Post a follow-up to this message ]



    Re: Calculating the difference between 2 times  
Keith Thompson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-06 07:50 AM

sony.antony@gmail.com writes:
> I have 2 k-shell variables each holding a date in the form "MM/DD/YYY
> hh-mm-ss".
> I want to find out how many seconds elapsed between these dates.
> Other than k-shell, I can use sed, awk, perl
> Can somebody tell me how I can go about with this problem.

If you have GNU date, you can convert each date to a number of seconds
since 1970, *if* it's in a format that GNU date recognizes:

date +%s -d '01/17/2006 18:53:17'

If the format is really "MM/DD/YYY hh-mm-ss", then you have a couple
of problems: you'll need 4 digits for the year (I assume that was a
typo), and you'll want "hh:mm:ss" rather than "hh-mm-ss" (easily done
with sed).

Note that GNU date assumes 01/02/2003 is January 2, not February 1;
that matches your assumption, but it's not universal.

Or you could do the whole thing fairly easily in Perl.  Just parse the
format using a regular expression, extract the fields, adjust as
needed, and use the Time::Local module to convert to a raw timestamp.
For more information, see the PERL documentation; failing that, try
comp.lang.perl.misc.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.





[ Post a follow-up to this message ]



    Re: Calculating the difference between 2 times  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-06 07:50 AM

On 2006-01-18, Keith Thompson wrote:
> sony.antony@gmail.com writes: 
>
> If you have GNU date, you can convert each date to a number of seconds
> since 1970, *if* it's in a format that GNU date recognizes:
>
>     date +%s -d '01/17/2006 18:53:17'
>
> If the format is really "MM/DD/YYY hh-mm-ss", then you have a couple
> of problems: you'll need 4 digits for the year (I assume that was a
> typo), and you'll want "hh:mm:ss" rather than "hh-mm-ss" (easily done
> with sed).

Or with the shell:

DATEVAR="01/11/2006 12-06-44"
IFS=' /:-'
set -f
set -- $DATEVAR

DV="$3-$1-$2 $4:$5:$6"
IFS=$' \t\n' ## bash/ksh93; sh: IFS=`printf " \t\n"`

## Using GNU date:
SECS=`date -d "$DV" +%s`


> Note that GNU date assumes 01/02/2003 is January 2, not February 1;
> that matches your assumption, but it's not universal.
>
> Or you could do the whole thing fairly easily in Perl.  Just parse the
> format using a regular expression, extract the fields, adjust as
> needed, and use the Time::Local module to convert to a raw timestamp.
> For more information, see the PERL documentation; failing that, try
> comp.lang.perl.misc.

For more information on date arithmetic, see the comp.unix.shell
FAQ <http://home.comcast.net/~j.p.h/cus-faq.html#6>, or see Chapter
8 of my book (see the URL in my .sig; that chapter is available
on-line).

--
Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
Shell Scripting Recipes:     |  My code in this post, if any,
A Problem-Solution Approach  |          is released under the
2005, Apress                 |     GNU General Public Licence





[ Post a follow-up to this message ]



    Re: Calculating the difference between 2 times  
sony.antony@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-06 07:50 AM

Thanks for the prompt responses.
Unfortunately Im on Solaris and the date does not have the -d option.
Im almost certain that I will have to use perl. But just wanted to ask
you guys before I start on that.
--sony






[ Post a follow-up to this message ]



    Re: Calculating the difference between 2 times  
William James


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-06 07:50 AM

sony.antony@gmail.com wrote:
> I have 2 k-shell variables each holding a date in the form "MM/DD/YYY
> hh-mm-ss".
> I want to find out how many seconds elapsed between these dates.
> Other than k-shell, I can use sed, awk, perl
> Can somebody tell me how I can go about with this problem.
>
> Thanks
> --sony

echo '01/13/2006 18-50-50 01/13/2006 18-50-55'|ruby -F'[ /-]' -nae'
p Time.local(*$F.values_at(8,6,7,9,10,11)) -
Time.local(*$F.values_at(2,0,1,3,4,5))'






[ Post a follow-up to this message ]



    Re: Calculating the difference between 2 times  
Keith Thompson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-06 07:50 AM

sony.antony@gmail.com writes:
> Thanks for the prompt responses.
> Unfortunately Im on Solaris and the date does not have the -d option.
> Im almost certain that I will have to use perl. But just wanted to ask
> you guys before I start on that.

Or you can install GNU date; it's part of the "coreutils" package.

I don't remember whether awk can do this.  (For Solaris, use "nawk";
the default "awk" is still an ancient version.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.





[ Post a follow-up to this message ]



    Re: Calculating the difference between 2 times  
Ed Morton


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-06 10:55 PM

Keith Thompson wrote:

> sony.antony@gmail.com writes:
> 
>
>
> Or you can install GNU date; it's part of the "coreutils" package.
>
> I don't remember whether awk can do this.

GNU awk can, but if he could get GNU awk, then you wouldn't think
getting GNU date would be a challenge either. Personally, I'd install
GNU awk since it'll be useful for future problems too.

(For Solaris, use "nawk";

or /usr/xpg4/bin/awk

> the default "awk" is still an ancient version.)
>

Right - affectionately known as "old, broken awk".

For the OP, I think you already got a couple of answers to your
question, but if not you might want to look at question 6 in the FAQ
(http://home.comcast.net/~j.p.h/cus-faq.html#6).

Ed.





[ Post a follow-up to this message ]



    Re: Calculating the difference between 2 times  
John DuBois


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-06 10:55 PM

In article <1137548734.668168.268940@z14g2000cwz.googlegroups.com>,
<sony.antony@gmail.com> wrote:
>I have 2 k-shell variables each holding a date in the form "MM/DD/YYY
>hh-mm-ss".
>I want to find out how many seconds elapsed between these dates.
>Other than k-shell, I can use sed, awk, perl
>Can somebody tell me how I can go about with this problem.

If your ksh is recent enough, it understands dates pretty close to that form
:

$ printf "%T\n" "06/01/1995 11:35:20"
Thu Jun  1 11:35:20 1995

So:

$ newdate="01/18/2006 13-28-27" olddate="06/01/1995 11-35-20"
$ print $(( $(printf "%(%s)T" "${newdate//-/:}") - $(printf "%(%s)T" "$
{olddate//-/:}") ))
335587987

John
--
John DuBois  spcecdt@armory.com  KC6QKZ/AE  http://www.armory.com/~spcecdt/





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 07:15 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register