×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• 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.

# Calculate the difference betwwn two times2

## 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

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.

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:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!