Re: ksh TIME stamp compare (1116 Views)
Reply
Super Advisor
Ratzie
Posts: 855
Registered: ‎01-10-2002
Message 1 of 8 (1,116 Views)
Accepted Solution

ksh TIME stamp compare

I have ksh script that stores two dates, each different variables as:

$CURRENT 16-JAN-09-18:27:45
$APPLIED 16-JAN-09-18:24:41

I want to subtract $CURRENT from $APPLIED

All, there may be a case where APPLIED is NULL.
So I need to put an exception in that.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 8 (1,116 Views)

Re: ksh TIME stamp compare

Hi:

And I assume that you know how you want to "subtract" these dates :-) ?

You asked how to test if one of the variables is "null". By that, I assume you mean "empty" in the shell context.

# [ -z "${APPLIED}" ] && echo "APPLIED is empty" || echo "APPLIED is set"

Regards!

...JRF...
Super Advisor
Ratzie
Posts: 855
Registered: ‎01-10-2002
Message 3 of 8 (1,116 Views)

Re: ksh TIME stamp compare

So I changed the date format to:

This is current: 090116185045
This is applied: 090116183804

Just looking for the arithmetic to find difference
Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 4 of 8 (1,116 Views)

Re: ksh TIME stamp compare

[ Edited ]

>DIFF=$(($CURRENT - $APPLIED))

This will give you a difference with correct ordering but it isn't the normal distance metric for time in seconds. You can find differences but your distance metric is for some 5 dimensional phase space on your time units.

Super Advisor
Ratzie
Posts: 855
Registered: ‎01-10-2002
Message 5 of 8 (1,116 Views)

Re: ksh TIME stamp compare

DIFF=$(($CURRENT - $APPLIED))

I spoke too soon, I know it was too easy.
I have now changed it to just days & minutes that I am pulling from the oracle database into a file, so the output looks like:
DDMM

But, yikes, now I get into time conversion.
Because if I take:
CURRENT is: 162201
APPLY is: 162108
It comes back with 93 minutes difference.
Which is wrong.
Super Advisor
Ratzie
Posts: 855
Registered: ‎01-10-2002
Message 6 of 8 (1,116 Views)

Re: ksh TIME stamp compare

Sorry date format is:
DDHH24MI

Which gives me:
162215

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 7 of 8 (1,116 Views)

Re: ksh TIME stamp compare

Hi (again) Ratzie:

Perl can make your task painless.

# cat ./datediff
#!/usr/bin/perl
use strict;
use warnings;
use Date::Calc qw(Delta_DHMS);
my $date1 = shift or die "first YYYYMMDDHHMMSS expected\n";
my $date2 = shift or die "second YYYYMMDDHHMMSS expected\n";
my ( $year1, $month1, $day1, $hour1, $min1, $sec1 )
= unpack( "A4A2A2A2A2A2", $date1 );
my ( $year2, $month2, $day2, $hour2, $min2, $sec2 )
= unpack( "A4A2A2A2A2A2", $date2 );
my ( $Dd, $Dh, $Dm, $Ds ) = Delta_DHMS(
$year1, $month1, $day1, $hour1, $min1, $sec1,
$year2, $month2, $day2, $hour2, $min2, $sec2
);
print "difference is $Dd days $Dh hours $Dm minutes $Ds seconds\n";
1;

...run as:

# ./datediff 20090116185045 20090116183804
difference is 0 days 0 hours -12 minutes -41 seconds

# ./datediff 20090116183804 20090116185045
difference is 0 days 0 hours 12 minutes 41 seconds

That is, pass two date-time strings each in the format YYYMMDDHHMMSS

Regards!

...JRF...
Honored Contributor
Patrick Wallek
Posts: 13,784
Registered: ‎06-21-2000
Message 8 of 8 (1,116 Views)

Re: ksh TIME stamp compare

*******
But, yikes, now I get into time conversion.
Because if I take:
CURRENT is: 162201
APPLY is: 162108
It comes back with 93 minutes difference.
Which is wrong.
********

When you are just subtracting a number, yes the difference is 93.

The problem is that the shell has absolutely NO CLUE that what you have is a date/time.

Think about it like this:

162,201 - 162,108 = 93.

James' solution above should do what you require.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.