A search of the archives for the work 'business' yields the latest
thread as being from Aug 2004.

I have a module at work that wraps Date::Manip for regular dates. From
this, I derive a business date class. Being based on Date::Manip, it's
as slow as <fill in your favorite explitive>. Anyway, I have
determined that I don't need all of the date parsing routines of
Date::Manip, so I plan to rip out the guts of my module and replace it
with DateTime.

In order to do that, could just write a small routine to parse the few
input date formats I know I need to handle, slap in DateTime and be on
my merry way.

But I really feel like writing DateTime::Business. So, the following
is a proposal:

1) Add a convenience method called 'today' to DateTime, which is the
same as Date::Time->now->trunc(to=>day)

2) All business dates will have no time component. All decrements and
increments of business dates will be a number of days - I don't think
there is a concept of business weeks or months...

3) The call to
            use DateTime::Business
  would be initialized with a function ref that is called each time a
DateTime::Business is incremented or decremented. The function would
determine that the resulting date is a business date. This hook will
put the functionality of determination in the end programmers hands.
In my case, the function would be memoized, would read in a database
table of weekends and holidays to a static hash on the first call to
it and just look at the hash to determine the validity of the business
date.
Someone else could use the code from further along in the thread
http://marc.theaimsgroup.com/?l=perl-datetime&m=109299634113109&w=2 to
set up the definitions of what a business day is (or is not).

4) DateTime::Business itself would provide add and subtract methods to
call this programmer-provided function and if after a incr/decrement,
the result is not a business date, keep adjusting in the current
direction until you get a business date. Examples: Wed June 1 - four
business days is Wed May 25 (skip Memorial day.) Wed May 25 + 4
business days is June 1.

The psuedo-code algorithm for DateTime::Business functions looks like

sub add_business_days {
my ($self, $days) = @_;
return $self->crement_business_days(days);
}

sub subtract_business_days {
my ($self, $days) = @_;
return $self->crement_business_days(days*-1);
}

sub crement_business_days {
my ($self, $days) = @_;
## This duration could be a static class member to save time and space
my $duration=DateTime::Duration->new( days    => 1);

my $crement = $days/abs($days);
while ($days) {
    $crement > 0 ?  $self->add_duration($duration) :
$self->subtract_duration($duration);
    $days -= $crement;
    ## This is the part that skips non-business days by doing the
'crement w/o adjusting the $days_to_crement.
    while (!$self->is_a_business_day()) {
        $crement > 0 ?  $self->add_duration($duration) :
$self->subtract_duration($duration);
    }
return $self;
}

Ok, I have provided the dartboard. Anyone care to thow some darts?
--
Matthew O. Persico

Reply via email to