Chris Charley wrote:
Hello Andrew

I've enclosed a text file that demonstrates Date::Simple.

HTH
Chris

----- Original Message ----- From: "Andrew Gaffney" <[EMAIL PROTECTED]>
Newsgroups: perl.beginners
To: "beginners" <[EMAIL PROTECTED]>
Sent: Saturday, March 27, 2004 7:52 PM
Subject: date math




I'm looking for a way to do some date math. I'm working on a payroll

tracking system. I


want to provide a list of 2 week periods for the user to select. These

periods start from


a hard-coded known start of pay period date. I want it to display the 6

most recent 2 week


periods. For example, my known start of pay period date is '2004-03-21.'.

I want to count


off 2 week periods from that. So, my program should be able to give me

'2004-03-21 -


2004-04-03', '2004-04-04 - 2004-04-17', '2004-04-18 - 2004-05-01', etc.

Once I have my


list, I can then find out which ones are the most recent and display them

to the user. I


also need to be able to determine if a certain date is between 2 dates. I

really only need


help with the date math part. Everything else I can do. Is there a module

that can help me?


--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.
636-357-1548



------------------------------------------------------------------------

#!/usr/bin/perl
use strict;
use warnings;
use Date::Simple;

my $start = "2004-03-21";
my $start_date = Date::Simple->new($start);

my @output;

for (1..6) {
        push @output, [$start_date, $start_date + 13];
        $start_date += 14;
}


print map {join(" :: ", $_->[0], $_->[1]), "\n"} @output;


# Find if date between 2 dates
my $first  = Date::Simple->new("2004-06-01");
my $second = Date::Simple->new("2004-08-01");
my $fourth_of_july = Date::Simple->new("2004-07-04");

if ($first < $fourth_of_july && $fourth_of_july < $second) {
        print "$fourth_of_july falls between $first and $second\n";
}

My code ended up looking like:


my @payperiods;
my @finalpayperiods;
my $lastperiodend = Date::Simple->new('2004-01-10');
while() {
  my $newstart = $lastperiodend + 1;
  my $newend = $newstart + 13;
  my @lt = localtime;
  last if(Date::Simple->new($lt[5]+1900, $lt[4]+1, $lt[3]) < $newstart);
  push @payperiods, "$newstart to $newend";
  $lastperiodend = $newend;
}
my $periodcounter = 0;
foreach(reverse @payperiods) {
  $periodcounter++;
  last if($periodcounter > 6);
  push @finalpayperiods, $_;
}

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.
636-357-1548


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to