### (ar0n: Date::Calc ) Re: Date difference?

by ar0n (Priest)
 on Apr 28, 2002 at 22:44 UTC ( #162723=note: print w/replies, xml ) Need Help??

in reply to Date difference?

At the risk of giving you a RTFM answer, see Date::Calc, and more specifically Delta_DHMS:
```use Date::Calc qw/Delta_DHMS/;

...

my @date_one = qw/2002 04 25 23 12 40/;
my @date_two = qw/2002 03 23 15 25 21/;

my (\$dd, \$dh, \$dm, \$ds) = Delta_DHMS(@date_one, @date_two);

Replies are listed 'Best First'.
Re: Re: Date difference?
by chicks (Scribe) on Apr 29, 2002 at 01:46 UTC
I'm fond of Date::Calc, so I played with your example a bit since I had never used it that way myself. I found something that seemed curious to me. Your example has the delta as negative, since @date_one is before @date_two. That makes sense. But Date::Calc seems to cause each and every element returned by Delta_DHMS to be negative. Should just the first element be negative?
```[chicks]\$ perl x
-33:-7:-47:-19
33:7:47:19
[chicks]\$ cat x
#!/usr/bin/perl -w

use Date::Calc qw/Delta_DHMS/;

my @date_one = qw/2002 04 25 23 12 40/;
my @date_two = qw/2002 03 23 15 25 21/;

my (\$dd, \$dh, \$dm, \$ds) = Delta_DHMS(@date_one, @date_two);
print "\$dd:\$dh:\$dm:\$ds\n";

(\$dd, \$dh, \$dm, \$ds) = Delta_DHMS(@date_two, @date_one);
print "\$dd:\$dh:\$dm:\$ds\n";
This is explained in  perldoc Date::Calc
```* "(\$Dd,\$Dh,\$Dm,\$Ds) = Delta_DHMS(\$year1,\$month1,\$day1,
\$hour1,\$min1,\$sec1, \$year2,\$month2,\$day2, \$hour2,\$min2,\$sec2);"

This function returns the difference in days, hours, minutes and
seconds between the two given dates with times.

All four return values will be positive if the two dates are in
chronological order, i.e., if date #1 comes chronologically BEFORE
date #2, and negative (in all four return values!) if the order of t
+he
two dates is reversed.

This is so that the two functions ""Delta_DHMS()"" and
""Add_Delta_DHMS()"" (description see further below) are
complementary, i.e., mutually inverse:

+me2))

yields ""(@date2,@time2)"" again, whereas

map(-\$_, Delta_DHMS(@date1,@time1, @date2,@time2)))

yields ""(@date1,@time1)"", and

yields ""@delta"" again.

The result is zero (in all four return values) if the two dates and
times are identical.

--Jim

I know it's explained in the docs, but it still seems counterintuitive.

Create A New User
Node Status?
node history
Node Type: note [id://162723]
help
Chatterbox?
 [Lotus1]: Cosmic37 if you undef \$/ in a local context to a block it won't affect the global version after the block finishes [Lotus1]: or you can just do local \$/ in a block [erix]: cool [Cosmic37]: thank you Lotus I understand [Cosmic37]: now I have tried another blunder - can anyone explain why I am such a dunderhead? [Cosmic37]: if ( \$line =~ /\$mydt/ ) { print \$line; }

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-06-29 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (673 votes). Check out past polls.