Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: DateCalc using Date::Manip

by SBECK (Hermit)
on Jan 10, 2017 at 15:26 UTC ( #1179324=note: print w/replies, xml ) Need Help??

in reply to DateCalc using Date::Manip

As others have noted, there are several ways to do calculations (exact, approximate, semi-approximate). Unfortunately, Date::Manip::DM6 (which is basically just an interface that is backward compatible with the older versions of Date::Manip) didn't currently support all of them (since they didn't even exist in older versions of Date::Manip).

The was no reason NOT to support them... I just had never thought to add them (and nobody had requested that). So I just did, and in the next version of Date::Manip, you'll be able to do:

my $dtopt= DateCalc($dt1,$dt2,'semi');

and get what you want. However, that won't work in the current version.

Getting what you want with the current version of Date::Manip::DM6 is harder because in the older module, there were only exact and approximate, and in newer versions there are exact, semi-approximate, and approximate, and it's challenging to get what you want. Really, the best way would be to do that calculation using the OO interface:

use Date::Manip::Date; my $date1 = new Date::Manip::Date; my $date2 = $date1->new_date(); $date1->parse($dt1); $date2->parse($dt2); my $delt = $date1->calc($date2,'semi'); my $delv = $delt->value(); print "$delv\n";

Replies are listed 'Best First'.
Re^2: DateCalc using Date::Manip
by huck (Parson) on Jan 10, 2017 at 18:03 UTC

    Wow, thanks

    But there still is a problem, that is semi-approx, and he wants semi-exact

    use Date::Manip; my $dt1="2016080100:00:00"; my $dt2='2016123100:00:00'; my $date1 = new Date::Manip::Date; my $date2 = $date1->new_date(); $date1->parse($dt1); $date2->parse($dt2); my $delts = $date1->calc($date2,'semi'); my $delte = $date1->calc($date2,'exact'); my $delve=$delte->value(); my $delvs=$delts->value(); sub semi_exact { my $delve=shift; my @parts=split(':',$delve); my $h0=$parts[4]+$parts[3]*24+$parts[2]*7*24; my $w=sprintf('%d',$h0/(7*24)); $h0=$h0-$w*7*24; if ($w<0) {$h0=$h0*-1;} my $d=sprintf('%d',$h0/24); $h0=$h0-$d*24; my $delvse="0:0:$w:$d:$h0:$parts[5]:$parts[6]"; return $delvse; } # semi-exact my $delvse=semi_exact($delve); print 'exact :'.$delve."\n"; print 'semi-approx:'.$delvs."\n"; print 'semi-exact :'.$delvse."\n"; my $fe=DateCalc($dt1,$dt2,0); my $fse=semi_exact($fe); print 'functional semi-exact :'.$fse."\n"; exit;
    exact :0:0:0:0:3649:0:0 semi-approx:0:0:21:5:0:0:0 semi-exact :0:0:21:5:1:0:0 functional semi-exact :0:0:21:5:1:0:0

      I don't have a 'semi-exact' mode in Date::Manip... but what you're really saying is: convert an exact delta to a semi-approximate one.

      This is of course different from coming up with an semi-approximate delta from two dates (which is what I was doing... I should have noticed that it wasn't exactly what the OP was doing, but I got ahead of myself after noticing I couldn't use 'semi' mode in the older DateCalc).

      In any case, you can do the following:

      use Date::Manip; my $dt1='2016080100:00:00'; my $dt2='2016123100:00:00'; my $exact= DateCalc($dt1,$dt2); my $semi = DateCalc($dt1,$dt2,'semi'); print "Exact : $exact\n"; print "Semi : $semi\n"; my $delta = ParseDateDelta($exact,'semi'); print "Exact->Semi: $delta\n"; print "\n"; use Date::Manip::Date; my $date1 = new Date::Manip::Date; my $date2 = $date1->new_date(); $date1->parse($dt1); $date2->parse($dt2); my $delt_x = $date1->calc($date2); my $delt_s = $date1->calc($date2,'semi'); my $val = $delt_x->value(); print "New Exact : $val\n"; $val = $delt_s->value(); print "New Semi : $val\n"; $delt_x->convert('semi'); $val = $delt_x->value(); print "New Ex->Se: $val\n";

      which yields:

      Exact : 0:0:0:0:3649:0:0 Semi : 0:0:21:5:0:0:0 Exact->Semi: 0:0:21:5:1:0:0 New Exact : 0:0:0:0:3649:0:0 New Semi : 0:0:21:5:0:0:0 New Ex->Se: 0:0:21:5:1:0:0

      So you can see how to convert an exact delta to a semi-approximate one using either the old or new interfaces.

      A BIG THANK YOU!!!!! I was able to sort out my problem with your code. Although, I need to make some modification to my code after it does the date calculation(with the code you have given) . I will keep you updated. MANY THANKS!!!!!!:)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1179324]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2020-10-24 18:07 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (246 votes). Check out past polls.