Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

extract month and year from localtime

by oikool (Novice)
on Feb 21, 2014 at 06:51 UTC ( [id://1075699]=perlquestion: print w/replies, xml ) Need Help??

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

how to extract month and year from localtime? in perl

$stimm = localtime($^T); print ((split/ /,$stimm)[1]);

Replies are listed 'Best First'.
Re: extract month and year from localtime
by davido (Cardinal) on Feb 21, 2014 at 08:09 UTC

    My favorite quote from the Inline::C-Cookbook: "A concept is valid in Perl only if it can be shown to work in one line."

    $ perl -E 'say join( ", ", sub{ 1+shift, 1900+shift }->((localtime tim +e)[4,5]) );'

    This calls localtime(time) in list context, and takes a slice of the list consisting of elements #4 and #5, which are month and year. The month is zero-based, so we have to add 1. The year is 1900 based, so we have to add 1900. We do this by passing the two values into an anonymous sub that exists only for the purpose of applying some math to its inputs. We immediately dereference the sub (ie, invoke it) and return the results (still in list context). Then join the results (2 and 2014) with a comma, and print them.

    Inline::C has nothing to do with the solution, of course, but somehow the quote seems appropriate. ;)


    Dave

      This kind of construct

      sub{ 1+shift, 1900+shift }->((localtime time)[4,5])

      I have been missing for a long time. Many many thanks!

        Yeah, I was kind of glad I remembered it. It seems too often we end up creating some temporary variables to receive and decorate values, when we can do our decorating inline with an immediate-use anonymous sub.


        Dave

Re: extract month and year from localtime
by hdb (Monsignor) on Feb 21, 2014 at 07:08 UTC

    If you use localtime in list context (for example by assigning it to an array or a list), you get all parts separately. For example it says in the documentation:

    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time +);
Re: extract month and year from localtime
by kcott (Archbishop) on Feb 21, 2014 at 07:06 UTC

    G'day oikool,

    You can use the built-in module Time::Piece for this task. Check the documentation for different representations of year (e.g. 2-digit or 4-digit) and month (e.g. number: starting at 0 or 1; or name: full or abbreviated). Here's the basic code:

    #!/usr/bin/env perl -l use strict; use warnings; use Time::Piece; my $t = localtime($^T); print 'Month: ', $t->month; print 'Year: ', $t->year;

    Output:

    Month: Feb Year: 2014

    -- Ken

Re: extract month and year from localtime
by atcroft (Abbot) on Feb 21, 2014 at 07:15 UTC

    From perldoc -f localtime,

    # 0 1 2 3 4 5 6 7 8 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    and
    $mday is the day of the month and $mon the month in the range 0..11, with 0 indicating January and 11 indicating December. This makes it easy to get a month name from a list:
    my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); print "$abbr[$mon] $mday"; # $mon=9, $mday=18 gives "Oct 18"
    $year contains the number of years since 1900. To get a 4-digit year write:
    $year += 1900;
    Given this, then:
    # 2014-02 perl -le 'my (@ym) = (localtime($^T))[5,4]; $ym[0] += 1900; $ym[1]++; print sprintf qq{%4d-%02d}, @ym;'
    or
    # 2014-Feb perl -le 'my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my (@ym) = (localtime)[5,4]; $ym[0]+= 1900; print sprintf qq{%4d-%3s}, $ym[0], $abbr[$ym[1]];'

    Hope that helps.

Re: extract month and year from localtime
by Anonymous Monk on Feb 21, 2014 at 08:43 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-19 16:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found