Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate the difference betwwn two times 2

Status
Not open for further replies.

tviman

Programmer
Jul 25, 2002
2,123
0
0
US
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...)
 
Hi

Does SCO's [tt]date[/tt] have [tt]-d[/tt] option similar to GNU [tt]date[/tt] ?
man GNU date said:
[pre]
-d, --date=STRING
display time described by STRING, not 'now'[/pre]
If yes, then I would use it to parse the entire date part of [tt]lpstat[/tt] 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
 
Hi

tviman said:
So $6 = 10:49
If you are more into Awk and GNU [tt]awk[/tt] is available on SCO, you can do it all with that :
Code:
lpstat -o | awk '
    [b]BEGIN[/b] [teal]{[/teal]
        [b]split[/b][teal]([/teal][i][green]"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"[/green][/i][teal],[/teal] m[teal])[/teal]
        [b]for[/b] [teal]([/teal]i [b]in[/b] m[teal])[/teal]
            m[teal][[/teal]m[teal][[/teal]i[teal]]] =[/teal] i
    [teal]}[/teal]
    [teal]{[/teal]
        [b]print[/b] [COLOR=orange]systime()[/color] - [COLOR=orange]mktime[/color][teal]([/teal][COLOR=orange]strftime[/color][teal]([/teal][i][green]"%Y"[/green][/i][teal])[/teal] [i][green]" "[/green][/i] m[teal][[/teal][navy]$4[/navy][teal]][/teal] [i][green]" "[/green][/i] [navy]$5[/navy] [i][green]" "[/green][/i] [COLOR=orange]gensub[/color][teal]([/teal][fuchsia]/:/[/fuchsia][teal],[/teal] [i][green]" "[/green][/i][teal],[/teal] [purple]1[/purple][teal],[/teal] [navy]$6[/navy][teal])[/teal] [i][green]" 0"[/green][/i][teal])[/teal]
    [teal]}[/teal]
'

Feherke.
feherke.github.io
 
Hi

Or you can just compose the formula and evaluate it :
Code:
[teal]([/teal]
    date [teal]+[/teal][i][green]'x=%-H*60+%-M'[/green][/i]
    lpstat -o [teal]|[/teal] sed [i][green]'s/.* /x-/; s/:/*60-/'[/green][/i]
[teal]) |[/teal] 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 ( [tt]lpstat[/tt] does not seem to display seconds anyway ).


Feherke.
feherke.github.io
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top