Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Months of the year

by Anonymous Monk
on Mar 31, 2014 at 18:54 UTC ( [id://1080429]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!
I am trying to build a list with all the name of the months of the year using "Time::Piece". I also noticed in the docs this "Time::Piece::month_names(@months);", any idea how to use that to get the name of all the months? Trying with this, but its not working:
#!/usr/bin/perl use strict; use warnings; use Time::Piece; use Time::Piece::month_names(@months); my $t = Time::Piece->new(); my $mon = $t->mon; my $month = $t->monthname; my $year = $t->year; my @all_months = map sprintf( "%s", $_, $month ), 1 .. 12; # $mon foreach my $month ( @all_months ) { print "$month\n"; }
Thanks!

Replies are listed 'Best First'.
Re: Months of the year
by kennethk (Abbot) on Mar 31, 2014 at 19:02 UTC
    The line from the docs
    Time::Piece::mon_list(@months);
    Means that if you want to change the months list (for perhaps localization or internationalization), this is the API. If you just call the method with no arguments, you get the list of months:
    my @months = Time::Piece::mon_list;
    or
    my $t = Time::Piece->new(); my @months = $t->mon_list;

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thats a lot less code, now is trying to get the full name of the months with:
      my @months = $t->fullmonth;
      Not that easy I guess. It only gives the full name of the current month.
        use Time::Piece; use feature 'say'; my $t = Time::Piece->strptime('1', '%m'); for ( 0..11 ) { say $t->add_months($_)->fullmonth }
Re: Months of the year
by toolic (Bishop) on Mar 31, 2014 at 20:04 UTC
    Copied from the module source code:
    my @months = qw(January February March April May June July August September October November December);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-29 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found