Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How to get last month date period

by roborat (Novice)
on Mar 04, 2010 at 16:04 UTC ( #826730=perlquestion: print w/ replies, xml ) Need Help??
roborat has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, i'm here again :)

I need help to get the last month period.

For example if current month is March 2010, I need to get the last month date with format as:

01 Feb 2010 - 28 Feb 2010

I'll use this date for my monthly reports of my internet customers. I'm trying to learn perl as much as I can :)
Thanks again in advance for all your help honorable monks.

Comment on How to get last month date period
Re: How to get last month date period
by Anonymous Monk on Mar 04, 2010 at 16:14 UTC
    What have you tried?
Re: How to get last month date period
by almut (Canon) on Mar 04, 2010 at 16:20 UTC

    As this is mostly static info (except for leap years), you might just want to use a simple lookup table.

    my %period = ( Jan => ['01 Dec','31 Dec'], Feb => ['01 Jan','31 Jan'], Mar => ['01 Feb','28 Feb'], #... Dec => ['01 Nov','30 Nov'], ); my $curr_month = "Mar"; my $curr_year = 2010; my $p = $period{$curr_month}; print "$p->[0] $curr_year - $p->[1] $curr_year\n"; # 01 Feb 2010 - 2 +8 Feb 2010

    To figure out when the "28 Feb" isn't correct, you could use one of the many date modules on CPAN (this is left as an exercise for... :).  Or simply hardcode a list of those years up until the time you'll retire ;)

    Update: the following should also work for up to 2099

    ... my $p = $period{$curr_month}; if ($curr_month eq "Mar") { $p->[1] = '29 Feb' unless $curr_year % 4; } ...
      Hi Almut, thanks a lot for the help! I'll find out how to check it 28 Feb isn't correct. :)

      Hope to find the solution to this soon. :)
      Thanks you very much!
      This will break in January.

      As for leap years, the rule is:

      • Assume 28 days, except:
      • if the current year divisible by 4: 29 days, except
      • if the current year is divisible by 100: 28 days except:
      • if the current year is divisible by 400: 29 days
        This will break in January.

        You're right.  To fix, just add:

        $curr_year-- if $curr_month eq "Jan";
Re: How to get last month date period
by bart (Canon) on Mar 04, 2010 at 16:29 UTC

    You can use strftime from POSIX to format the date, and timelocal from Time::Local to do basic time manipulation. Both are core modules, i.e. they come with Perl.

    So, what I propose is the following steps:

    • with localtime, get the list of date and time values for today/now into an array
    • set the day of the month to the first, so you have the date of the first of this month. You may set the time to (around) noon to avoid problems with the switch of Daylight Saving Time, where a day is not exactly 24 hours, depending on what time of day it is now.
    • convert to seconds since epoch with timelocal
    • subtract 24*60*60 seconds to get a time in the last day of the past month as an integer value
    • with localtime, convert to the list of date and time values
    • use strftime to get the last day, properly formatted
    • in the array, set the day to the first.
    • use strftime to get the first day, properly formatted

    Code:

    use POSIX; use Time::Local; print daterange(); sub daterange { my @dt = localtime; $dt[3] = 1; $dt[2] = 12; my $time = timelocal @dt; $time -= 24*60*60; @dt = localtime $time; my $ret = strftime("%d %b %Y", @dt); $dt[3] = 1; return strftime("%d %b %Y", @dt) . " - $ret"; }
Re: How to get last month date period
by ambrus (Abbot) on Mar 04, 2010 at 17:13 UTC

    As with most time and date manipulation, I recommend the Date::Manip module.

    use Date::Manip; # this is the current month my $current = ParseDate("today"); # or = ParseDate("january 2010") or +whatever # now let's find the last day of the previous month $last_month_last_day = DateCalc(UnixDate($current, "%Y-%m"), "-1 day") +; # then we print the interval print UnixDate($last_month_last_day, "01 %b %Y - %d %b %Y\n");
Re: How to get last month date period
by youlose (Scribe) on Mar 04, 2010 at 18:32 UTC
    use DateTime;
    use Modern::Perl;
    my $fdm = DateTime->now->subtract(months=>1)->truncate(to=>'month');
    my $ldm = DateTime->last_day_of_month(year=>$fdm->year,month=>$fdm->month);
    my $stf_pat = '%d %b %Y';
    say $fdm->strftime($stf_pat),' - ',$ldm->strftime($stf_pat);
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-08-30 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls