nabrown737 has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently working with an older version of DateManip and for various reasons I can't upgrade it at the moment. I'm trying to calculate the number of days in the Delta between two dates. I need to be able to calculate this delta in both business and standard days. The issue arises that the code can't seem to convert months into days. Example:

$start = &DateManip::ParseDate('2013-10-03 00:00:00'); $end = &DateManip::ParseDate('2013-12-09 00:00:00'); #Standard Days my $standard_delta = &DateManip::DateCalc($start,$end,1); my $standard_days = &DateManip::Delta_Format($standard_delta,2,'%dt'); print "$standard_days\n"; #Business Days my $business_delta = &DateManip::DateCalc($start,$end,2); my $business_days = &DateManip::Delta_Format($business_delta,2,'%dt'); print "$business_days\n";

So I would expect this to print out the number of standard and business days somewhere in the 60s. Instead it ignores the difference of 2 months and prints out just the difference in days (6 standard days and 4 business days). This seems to be because it doesn't know with certainty how many weeks are in each month and thus can't convert from months to weeks and weeks to days. Any ideas on how I can get it to convert from months to days in both business and standard?

Replies are listed 'Best First'.
Re: DateManip Delta_Format Usage
by wjw (Priest) on Jan 11, 2014 at 17:52 UTC
    I played around with this a bit and (without actually verifying the math) ended up with the following:
    #!/usr/bin/perl use strict; use Date::Manip; my $start = Date::Manip::ParseDate('2013-10-03 00:00:00'); my $end = Date::Manip::ParseDate('2013-12-09 00:00:00'); print "$start\n"; print "$end\n"; #Standard Days my $standard_delta = Date::Manip::DateCalc($start,$end,1); print "$standard_delta\n"; my $standard_days = Date::Manip::Delta_Format($standard_delta,2,%dyd') +; print "$standard_days\n"; #Business Days my $business_delta = Date::Manip::DateCalc($start,$end,2); print "$business_delta\n"; my $business_days = Date::Manip::Delta_Format($business_delta,2,'%dyd' +); print "$business_days\n";
    With output that looks like:
    2013100300:00:00 2013120900:00:00 +0:2:+0:6:0:0:0 66.87375 +0:2:+0:+4:0:0:0 64.87375
    Note: that I am using a newer version than you state that you are, but as close as I can tell from a cursory glance through the CPAN docs, the functionality has not changed. (I may be wrong about that, but it will only take a second to copy the above and try it. If I am wrong, you waste a copy/paste ->save->run amount of time, not much more...).

    Hope that is helpful... Update: Changed format directive from '%dydt' to '%dyd', thus changing the output to exclude the 't' from the end of the calculated days.

    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results.

      Sorry for the tardy reply... I don't get on perl monks more often than every few days.

      The original post doesn't include the version they are using which is unfortunate. I'm assuming that it's from the pre 6.00 era. The '%dyd' style formats are from the newer version of Date::Manip, so they won't work with a 5.xx era script.

      Also, the original post said he expected (not sure if it was good or bad) around 60+ days for both types of delta, and that shouldn't be a desired result since a business delta by definition is excluding a bunch of days (weekends) so it should be smaller by a factor of approximately 5/7.

      If I wanted to know how many days were in the deltas, I'd do the following:

      #Standard Days
      my $standard_delta = DateCalc($start,$end,0);
      my $standard_days  = Delta_Format($standard_delta,2,'%dt');
      print "$standard_delta :: $standard_days\n";
      
      #Business Days
      my $business_delta = DateCalc($start,$end,3);
      my $business_days  = Delta_Format($business_delta,2,'%dt');
      print "$business_delta :: $business_days\n";
      

      and the results were:

      +0:0:9:4:0:0:0 :: 67.00
      +0:0:0:47:0:0:0 :: 47.00
      

      Note that I changed the business day calc mode to 3 (which is an exact business delta). By treating both exactly, you get accurate numbers which you won't get with approximate deltas.

        That is informative! Thanks!
        ...the majority is always wrong, and always the last to know about it...
        Insanity: Doing the same thing over and over again and expecting different results.

        Your reply is greatly appreciated, tardy or not! I wish I knew the version I was using, however that information seems to have been lost. I wasn't event aware that there was a 3rd mode for exact business delta. That is exactly what I needed! Thanks, Nathan