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


In reply to Re: extract month and year from localtime by davido
in thread extract month and year from localtime by oikool

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.