If you want to do it in a module-free manner without using pesky intermediate variables, you could do worse than wrapping up sprintf in an anonymous subroutine, which gives something like:

my $date = sprintf '%04d/%02d/%02d-%02d:%02d:%02d', sub { $_[5]+1900, $_[4]+1, reverse @_[0..3] }->(gmtime($output));

I write this snippet so often I have often toyed with the idea of uploading as a module to CPAN so I can use it where ever I work, but it seems a bit frivolous since I can type it so quickly. What is happening is that the output from gmtime is being passed to an anonymous sub, which modifies two elements, reverses the first few elements taken as a hash slice and throws the rest away. The resulting elements are returned in list context from the sub to sprintf, which, by a miraculous turn of events, is waiting with a format string which will process those values in just the right way.

I really dig this idiom.

oops: I just realised I coded this up to emit the standard ISO date format. Talk about habit. I would avoid using the M/D/Y format: it looks to me as if you're talking about the 7th of January. Which indeed you might be. But others will think you're talking about the 1st of July. I mean, if you're going to the effort of representing times in GMT...


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

In reply to Re: gmtime by grinder
in thread gmtime by Anonymous Monk

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.