[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl-beginners
Subject:    Re: Date::Manip question
From:       Gerhard <enteiser () gmail ! com>
Date:       2012-11-08 21:17:23
Message-ID: 20121108211723.GA5948 () nuts
[Download RAW message or body]

On Thu, Nov 08, 2012 at 10:56:00AM +0000, Marco van Kammen wrote:
> my $current_month =                    (should be Nov)
> my $current_mont_num =           (should be 11)
> my $previous_month =                 (should be Oct)
> my $previous_month_num =     (should be 10)

There is Time::Piece and Time::Seconds in core since perl 5.10, so
I encourage using them:

    use 5.010;
    use strict;
    use Time::Piece;
    use Time::Seconds;

    my $now = localtime;
    my $current_month_num = $now->mon;
    my $current_month = $now->month;
    # substract 3 extra days to avoid thinking about month boundaries
    my $previous_month_num = ($now - ($now->mday + 3) * ONE_DAY)->mon;
    my $previous_month = ($now - ($now->mday + 3) * ONE_DAY)->month;

    say "Current:  $current_month_num $current_month";
    say "Previous: $previous_month_num $previous_month";

Cheers,

Gerhard

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic