Problem with Date::Manip

T

Ted

I know the documentation for this module says the author should be
contacted in case a problem is encountered, but I want to know if my
expectations are the problem or the way I am using it before I trouble
such a busy person.

The situation ought to be rather simple, and I thought Date::Manip was
an obvious choice.

The version of Perl that I am using is given by Perl itself:

C:\>perl -v

This is perl, v5.8.8 built for MSWin32-x86-multi-thread
(with 25 registered patches, see perl -V for more detail)

Copyright 1987-2006, Larry Wall

Binary build 817 [257965] provided by ActiveState
http://www.ActiveState.com
Built Mar 20 2006 17:54:25
....

The portion of the documentation dealing with Date::Manip that guided
my effort is the following:


"It even works with business days:
$date = DateCalc("today","+ 3 business days",\$err);"

Here is my little test script:

use Date::Manip;

my $day = '20060720';
my $delta = -1;
my $calc_date = substr(DateCalc($day,ParseDateDelta("$delta business
days")),0,8);

print "day = $day\ndelta = $delta business days\ncalculated date =
$calc_date\n\n";

for ($i = 1 ; $i < 60 ; $i++ ) {
$delta = -$i;
$calc_date = substr(DateCalc($day,ParseDateDelta("$delta business
days")),0,8);
print "i = $i\nday = $day\ndelta = $delta business days\ncalculated
date = $calc_date\n\n";
}

I expected this to give me the dates of the $i business days prior to
2006-07-20; i.e. including days Monday through Friday, and excepting
Saturdays and Sundays and holidays (a holiday isn't a business day).
Instead I got every week day going back two months.

I thought maybe Perl needs to be told which days are week days and
which days are holidays. After all, in some places, businesses operate
seven days a week, and there are a number of holidays that are
different between Canada and the US, and the differences seem greater
as the distance gets greater. It makes sense that this should be
configurable, but I did not find information regarding how to configure
that.

Where I want to get to is a situation in which, if I give a date of a
Monday, such as 2006-07-17, I get the date of the previous Friday, if
the weekend was not a long weekend, or the previous Thursday, if the
Friday is a holiday like Good Friday.

Have I misunderstood what the functions from Date::Manip should do for
me, or how they ought to be used? Or is there a configuration issue I
need to address?

Thanks for your time.

Ted
 
U

usenet

Ted said:
I expected this to give me the dates of the $i business days prior to
I thought maybe Perl needs to be told which days are week days and
which days are holidays. After all, in some places, businesses operate
seven days a week, and there are a number of holidays that are
different between Canada and the US, and the differences seem greater
as the distance gets greater. It makes sense that this should be
configurable, but I did not find information regarding how to configure
that.

Really??? What goofy version of Date::Manip do you have that you
perldocs for the module do not include the major sub-heading "Holiday
Section"?

Maybe you want to read up on http://tinyurl.com/g99gj
 
T

Ted

Maybe you want to read up on http://tinyurl.com/g99gj
Thanks. That solves one part of the problem. However, the main part
of the problem remains.

I see the following in my Manip.pm:

===========================
# First day of the week (1=monday, 7=sunday). ISO 8601 says monday.
$Cnf{"FirstDay"}=1;

# First and last day of the work week (1=monday, 7=sunday)
$Cnf{"WorkWeekBeg"}=1;
$Cnf{"WorkWeekEnd"}=5;
===========================

So why does "substr(DateCalc($day,ParseDateDelta("$delta business
days")),0,8);" behave as if "$Cnf{"WorkWeekEnd"}=7;" when it is clearly
set to 5.

Thanks,

Ted
 
T

Ted

The problem must be in my use of the function DateCalc, or a bug in it.

The following code:

$date = DateCalc("today","+ 3 business days",\$err);
print "$date\n";
$date = DateCalc("today","- 3 business days",\$err);
print "$date\n";
$date = DateCalc("today","+ 7 business days",\$err);
print "$date\n";
$date = DateCalc("today","- 7 business days",\$err);
print "$date\n";
$date = "today";
print "\n$date\n";
$newdate = Date_NextWorkDay($date,1);
print "$newdate\n";
$newdate = Date_PrevWorkDay($date,1);
print "$newdate\n";
$newdate = Date_PrevWorkDay($date,5);
print "$newdate\n";
$newdate = Date_PrevWorkDay(20060717,1);
print "$newdate\n";

produces the following output:

2006072412:20:25
2006071812:20:25
2006072812:20:25
2006071412:20:25

today
2006072412:20:25
2006072012:20:25
2006071412:20:25
2006071400:00:00

Clearly, the result obtained from DateCalc is wrong in that it appears
to ignore the specification of the work week in Manip.pm. OTOH,
Date_PrevWorkDay is giving me the correct result.

Have I used DateCalc incorrectly or is there a bug in DateCalc?

Ted
 
P

Paul Lalli

Ted said:
The problem must be in my use of the function DateCalc, or a bug in it.

The following code:

$date = DateCalc("today","+ 3 business days",\$err);

I haven't analyzed this fully yet, so I have no answer for you.
However, I do note that if you change the above to:
$date = DateCalc("today","+ 3 days",\$err, 2);
then you do get the expected results. This is explicitly setting the
mode to Business mode. I don't know why the results are different when
trying to use "business" in the string, as the docs pretty clearly say
that that should trigger Business mode as well...

Paul Lalli
 
T

Ted

Paul said:
I haven't analyzed this fully yet, so I have no answer for you.
However, I do note that if you change the above to:
$date = DateCalc("today","+ 3 days",\$err, 2);
then you do get the expected results. This is explicitly setting the
mode to Business mode. I don't know why the results are different when
trying to use "business" in the string, as the docs pretty clearly say
that that should trigger Business mode as well...

Paul Lalli

Thanks Paul,

I can confirm that changing my code to explicitly set the mode to
business mode gets all my code using DateCalc working as expected. So
it seems I hit a bug of some sort in DateCalc that results in
"business" not triggering business mode. Perhaps now a note to the
fellow who developed it is warranted?

Thanks

Ted
 
P

Paul Lalli

Ted said:
I can confirm that changing my code to explicitly set the mode to
business mode gets all my code using DateCalc working as expected. So
it seems I hit a bug of some sort in DateCalc that results in
"business" not triggering business mode. Perhaps now a note to the
fellow who developed it is warranted?

I'm still about 70% convinced there's something not being done
correctly in the relevant configuration files, but I have no idea what
needs to be done. Therefore, I'd say sure, email the author. Let us
know if/how he replies.

Paul Lalli
 
T

Ted

Paul said:
I'm still about 70% convinced there's something not being done
correctly in the relevant configuration files, but I have no idea what
needs to be done. Therefore, I'd say sure, email the author. Let us
know if/how he replies.
I did email the author, but my email bounced. Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top