in reply to Re: DateCalc using Date::Manip
in thread DateCalc using Date::Manip

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;
output
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

Replies are listed 'Best First'.
Re^3: DateCalc using Date::Manip
by SBECK (Chaplain) on Jan 11, 2017 at 16:50 UTC

    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.

Re^3: DateCalc using Date::Manip
by tsdesai (Acolyte) on Jan 11, 2017 at 10:25 UTC
    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!!!!!!:)