Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

perl script to calculate beg and end of current month

by Anonymous Monk
on May 01, 2006 at 17:47 UTC ( [id://546700]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,

I am wondering if anyone has a script that can do the following: (in a Unix environment)

> Check what month and year it is currently (For eg. Current Month = May) > Retrieve Last month's beginning and end dates (eg. 04/01/06 and 04/31/06)

I want these values to be passed into another file that will consist of only these values. Eg. I will have a properties file such as... Beg Date 04/01/06
End Date 04/31/06

The reason I need to do this is I have a customized program that queries the database for entries within a certain time period. Since we want to automate this program, we will not be feeding the dates manually each month...therefore, we are going to setup a cron job to automatically run this program with appropriate dates to be queried into the database.

If anyone has other ideas on how to approach this, please advise.

Thank you for your time.
  • Comment on perl script to calculate beg and end of current month

Replies are listed 'Best First'.
Re: perl script to calculate beg and end of current month
by saintmike (Vicar) on May 01, 2006 at 17:56 UTC
    Use DateTime:
    use DateTime; my $today = DateTime->today(); my $last_month = $today->subtract( months => 1 ); my $first = $last_month->clone()->set_day(1); my $last = $first->add( months => 1 )->subtract( days => 1 ); print "first=$first last=$last\n";
      Hi, I download this scirpt and ran it. It seems like set_day(1) is not working as expected. Currently, the first variable is showing as first=2008-06-30 instead of 2008-06-01. Let me know if there any workaround to fix this. Any help would be appreciated. -- Ace28
        This is how I managed to have the above code work.
        use DateTime; $dt = DateTime->today(); my $mnth = $dt->subtract(months => 1); my $first = $mnth->clone->set_day(1); my $last = $first->clone->add( months => 1 )->subtract( days => 1 ); print "first=$first last=$last\n";
Re: perl script to calculate beg and end of current month
by davidrw (Prior) on May 01, 2006 at 17:56 UTC
    What database? You could also just write a SQL solution..

    As for perl, here's a Date::Calc solution:
    use Date::Calc qw(Today Add_Delta_YMD Days_in_Month); my ($year, $month, undef) = Add_Delta_YMD( Today(), 0, -1, 0 ); printf "start: %04d-%02d-%02d\n", $year, $month, 1; printf " end: %04d-%02d-%02d\n", $year, $month, Days_in_Month($year, +$month);
Re: perl script to calculate beg and end of current month
by ikegami (Patriarch) on May 01, 2006 at 18:23 UTC

    You can also use Time::Local, as shown below. It's not the friendliest module, but it comes with perl.

    use Time::Local qw( timegm_nocheck ); my ($y, $m, $d) = (localtime)[5,4,3]; $y += 1900; $m += 1; my $ds = 1; my $de = (gmtime(timegm_nocheck(0,0,0, (1-1), ($m+1)-1, $y)))[3]; printf "start: %04d-%02d-%02d\n", $y, $m, $ds; printf " end: %04d-%02d-%02d\n", $y, $m, $de;

    Keep in mind the last day of the month is the day before first day of the next month.

Re: perl script to calculate beg and end of current month
by TedPride (Priest) on May 01, 2006 at 19:30 UTC
    Dates are easy. Either you want to find all the records relative to the current date (next 30 days, for instance), in which case you can just store a timestamp with each record and then check that timestamp against the current timestamp:
    my $mtime = time(); if ($rtime < $mtime + 86400 * 30) { ## Do whatever processing }
    Or you want to find all records within an exact date range, in which case you just store the dates in fixed-width, greatest significance first numerical format and test as necessary:
    use strict; use warnings; my ($sdate, $edate, $rdate); $sdate = sprintf('%04d%02d%02d', 2006, 11, 4); $edate = sprintf('%04d%02d%02d', 2006, 12, 4); while (<DATA>) { ($rdate) = m/(\d+)/; print if $sdate <= $rdate && $rdate <= $edate; } __DATA__ 20061203 Record 1 20061204 Record 2 20061206 Record 3
Re: perl script to calculate beg and end of current month
by TedPride (Priest) on May 01, 2006 at 19:57 UTC
    Here's some code to generate the date range for the previous month. You'll want it formatted differently if using it for queries, however.
    use strict; use warnings; my (@mon, $mon, $year); @mon = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); ($mon, $year) = (localtime(time))[4,5]; $year += 1900 if $year < 2000; if ($mon == 0) { $mon = 12; $year--; } if ($mon == 2) { if ($year % 4 != 0) { $mon[1] = 28; } elsif ($year % 400 == 0) { $mon[1] = 29; } elsif ($year % 100 == 0) { $mon[1] = 28; } else { $mon[1] = 29; } } $year = $year % 100; print sprintf('%02d/%02d/%02d', $mon, 1, $year), " through ", sprintf('%02d/%02d/%02d', $mon, $mon[$mon-1], $year);
      What about leap year? You hardcode the normal number of days in a month. This will cause rework as all cases are not coded for. Not sure of the answer just yet but came across this while researching it myself. Just saw the opportunities in the logic.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://546700]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-23 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found