SUMMARY: RE: Comparing two date strings: any easy way?

From: Zaigui Wang (zaigui@yahoo.com)
Date: Wed Jan 22 2003 - 02:53:26 EST


My apology first for this really late summary. In the
quest for a solution, I have been flexible enough to
modify the problem itself so that it ceased to be much
of a problem any more.

Needless to say that all the replies have been of
great help to my modifying/solving my problem, without
my realizing it, and all the posts obviously were
compiled with a lot of thoughts and efforts on the
part of posters. Some sent me java code, some perl
code, some gnu tool/utilities. I am posting some of
the replies I've receive verbatim here:

----------
Tom Payerle:
I like the Perl Time::ParseDate module from CPAN. It
can handle a wide
variety
of input formats, plus specify what to do in
potentially ambiguous
cases
(e.g. should date without year be closest, last year
or next year).

Anyway, for a simple thing like you propose, the
script

#Boilerplate
#!/bin/perl -w
use strict;
use Time::ParseDate;

#Read 2 date strings from stdin
my $date1=<STDIN>;
my $date2=<STDIN>;
chomp $date1; chomp $date2;

#Convert to seconds since start of epoch
my $time1 = parsedate($date1);
my $time2 = parsedate($date2);

#Compute days difference
my $tdiff = $time2 - $time1;
my $ddiff = $tdiff/60/60/24;
print "Diff is $ddiff days\n";

Or if want to oneline it
perl -MTime::ParseDate -e '@d=<STDIN>;chomp @d;print
(parsedate($d[1])-parsedate($d[0]))/60/60/24,"\n"'
----------------
Dan Lorenzini
(use gnu date, most linux distributions have gnu date.
It is also available from gnu as part of the sh-utils
download)

        $ D1=`gdate +%s -d "Tue Nov 19 08:56:06 PST 2002"`
        $ D2=`gdate +%s -d "Mon Jan 6 15:43:59 PST 2003"`
        $ echo $D1 $D2
        1037724966 1041896639
        $ DAYS=`expr \( $D2 - $D1 \) / 86400`
        $ echo $DAYS
        48

expr will only give you integers. If you want
decimals, you can use
awk:

        $ echo $D1 $D2 | awk '{print ($2-$1)/86400}'
        48.2833

gdate is on the companion CD with solaris 8 and above.
 Hope this
helps.
------------------
Chris Keladis:
Not anything built-in, but you could use a GNU'ish
date command that
has
the '-r' option to take the Epoch and return a string
again (and `date
+%s` to get the time now as Epoch).

To do it in perl you would either need to roll your
own str2time() and
time2str() functions, or use the ones in the
Date::Parse, or
Date::Manip
CPAN modules.

Sorry, in my reply i was mixing perl and sh to do what
you asked, but
it
could also be done solely in either language.

------------------
Lupe Christoph
(perl module Date::Manip)

% perl -MDate::Manip -e 'print
Delta_Format(DateCalc(ParseDate(scalar
<>), \
    ParseDate(scalar <>)),0,"%dt"), "\n";'
Tue Nov 19 08:56:06 PST 2002
Mon Jan 6 15:43:59 PST 2003
48.283252

-- ----------
I also found this interesting post:
http://www.netsys.com/sunmgr/1997-06/msg00028.html

Zaigui
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 23:25:38 EDT