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...
In reply to Re: gmtime
by grinder
in thread gmtime
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |