Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Calculate the difference betwwn two times

Calculate the difference betwwn two times

Calculate the difference betwwn two times

I'm trying to determine the time difference between the current time and when a print job was created. I'm using the 6th field in lpstat -o to collect the time the print job was created as follows: lpstat -o returns this:

LP07-48943 root 476 Sep 21 10:49

So $6 = 10:49

If the current time (date +%H) = 12:11, how can I determine the number of seconds between the two? I've been away from this type of programming for too long and I'm running in circles. Any help or suggestions will be greatly appreciated.

BTW - the OS is SCO. (I know, I know...)

RE: Calculate the difference betwwn two times


Does SCO's date have -d option similar to GNU date ?

Quote (man GNU date)

       -d, --date=STRING
              display time described by STRING, not 'now' 
If yes, then I would use it to parse the entire date part of lpstat into Unix time then calculate with that :


echo $(( $( date +'%s' ) - $( date -d "$( lpstat -o | awk '{$1=$2=$3=""}1' )" +'%s' ) )) 


RE: Calculate the difference betwwn two times


Quote (tviman)

So $6 = 10:49
If you are more into Awk and GNU awk is available on SCO, you can do it all with that :


lpstat -o | awk '
    BEGIN {
        split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", m)
        for (i in m)
            m[m[i]] = i
        print systime() - mktime(strftime("%Y") " " m[$4] " " $5 " " gensub(/:/, " ", 1, $6) " 0")


RE: Calculate the difference betwwn two times


Or you can just compose the formula and evaluate it :


    date +'x=%-H*60+%-M'
    lpstat -o | sed 's/.* /x-/; s/:/*60-/'
) | bc 
This is be the more portable one, so should work on SCO too. But this one will get confuse by date change ( I mean, fails on processes started in another day ) and calculates minutes ( lpstat does not seem to display seconds anyway ).


RE: Calculate the difference betwwn two times

Thank you for your prompt replies. SCO's date function doesn't recognize the -d but has a -u in it's place. However, I like the last two options give them a go this afternoon.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close