Operating System - HP-UX
1752468 Members
6473 Online
108788 Solutions
New Discussion юеВ

Re: ksh TIME stamp compare

 
SOLVED
Go to solution
Ratzie
Super Advisor

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.
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

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...
Ratzie
Super Advisor

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
Dennis Handly
Acclaimed Contributor

Re: ksh TIME stamp compare

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

Ratzie
Super Advisor

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.
Ratzie
Super Advisor

Re: ksh TIME stamp compare

Sorry date format is:
DDHH24MI

Which gives me:
162215

James R. Ferguson
Acclaimed Contributor
Solution

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...
Patrick Wallek
Honored Contributor

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.