INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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!

*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.

Jobs

Calculate the difference betwwn two times

Calculate the difference betwwn two times

(OP)
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

Hi

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 :

CODE

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

Feherke.
feherke.github.io

RE: Calculate the difference betwwn two times

Hi

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 :

CODE

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")
    }
' 

Feherke.
feherke.github.io

RE: Calculate the difference betwwn two times

Hi

Or you can just compose the formula and evaluate it :

CODE

(
    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 ).

Feherke.
feherke.github.io

RE: Calculate the difference betwwn two times

(OP)
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!

Resources

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