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

Hi Monks!

I need help or directions on how to get in Perl 5 days prior or whatever number of days from a date.
Let say if the date passed is 09/10/2007, give me all info between 09/10/2007 -5 days, no weekends. It should be between 09/10/2007 and 09/03/2007.

I have the values from the dates but now I need to find a formula on how to do this, here is the code with the date values using Perl.

my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = localtime(ti +me); my $thishour = ('0','1','2','3','4','5','6','7','8','9','10','11','12' +,'1','2','3','4','5','6','7','8','9','10','11','12') [(localtime) [2] +]; my $thismonth = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep' +,'Oct','Nov','Dec')[(localtime)[4]]; my $thisday = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Fri +day','Saturday')[(localtime)[6]]; my $day_number = ('1','2','3','4','5','6','7')[(localtime)[6]]; $mon++; $year += 1900; #I'm having problems figuring out how to find the date five days from +today's date. print "Today is $thisday, $thismonth, $mday, $year";

Replies are listed 'Best First'.
Re: Prior Date
by suaveant (Parson) on Oct 02, 2007 at 20:08 UTC
    or use Date::Manip in Business Mode

                    - Ant
                    - Some of my best work - (1 2 3)

Re: Prior Date
by perlfan (Parson) on Oct 02, 2007 at 20:04 UTC
Re: Prior Date
by djp (Hermit) on Oct 03, 2007 at 05:52 UTC
    ++ to the suggestions above. The first rule about calculatiing dates is: Don't Do It. It's complex, and someone else has done all the work for you. Use the OS (e.g. via POSIX::mktime, which normalizes the supplied date info), or someone else's work via a CPAN module.
Re: Prior Date
by andreas1234567 (Vicar) on Oct 03, 2007 at 06:16 UTC
    I echo djp: Don't Reinvent the wheel. Date and time calculations (e.g. month changes, leap years, timezones) is surprisingly difficult to get right. Don't let that prevent you from doing the exercise though.
    $ perl -l use strict; use warnings; use DateTime; print DateTime->new( year => 2007, month => 9, day => 10)->subtract( d +ays => 5 )->ymd(); __END__ 2007-09-05
    --
    Andreas
      Except the OP specified no weekends... I looked briefly at DateTime but didn't see anything for business days... but there must be something.

                      - Ant
                      - Some of my best work - (1 2 3)

        I have this, but I can't figure it ou how to pass the values for the dates using a variable, can anyone try that?

        #!/usr/bin/perl use strict; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use DBI; use Date::Pcalc qw(:all); print header(); my $s_date="2007,09,27"; my $e_date="2007,10,3"; my @range = &Delta_Business_Days($s_date,$e_date); foreach my $date (@range) { print Date_to_Text(@{$date}), "\n"; # It breaks here... print "**$date**<br>"; } sub Delta_Business_Days { my(@date1) = (@_)[0,1,2]; my(@date2) = (@_)[3,4,5]; my($minus,$result,$dow1,$dow2,$diff,$temp); $minus = 0; $result = Delta_Days(@date1,@date2); if ($result != 0) { if ($result < 0) { $minus = 1; $result = -$result; $dow1 = Day_of_Week(@date2); $dow2 = Day_of_Week(@date1); } else { $dow1 = Day_of_Week(@date1); $dow2 = Day_of_Week(@date2); } $diff = $dow2 - $dow1; $temp = $result; if ($diff != 0) { if ($diff < 0) { $diff += 7; } $temp -= $diff; $dow1 += $diff; if ($dow1 > 6) { $result--; if ($dow1 > 7) { $result--; } } } if ($temp != 0) { $temp /= 7; $result -= ($temp << 1); } } if ($minus) { return -$result; } else { return $result; } }