Some time back, someone took me to task for not posting timestamps in UTC/GMT in certain areas of the Monastery. I assumed it was meant gently, seeing how the practice appears to be more popular in *nix circles than the ones I normally get paid to hang out in. Shortly after, the same person made a similar remark to someone else. So, seeing as I need to adapt to the local customs, I decided to play around with Perl's support for UTC/GMT times and see what it took to create something that would provide results for both sets of camps.
Here's what I came up with:
#!/usr/bin/perl -w use strict; print formatDTV( localtime ), ' - (GMT: ', formatDTV( gmtime ), ')'; exit 1; sub formatDTV # -------------------------------------------------------------- # When passed a datetime array (localtime or gmtime), returns a # string formatted for my preferences. # # Reminder: [0] [1] [2] [3] [4] [5] [6] [7] [8] # (gmtime) = ( $ss, $mm, $hh, $dd, $mo, $yr, $wd, $dy, $ds ) # -------------------------------------------------------------- { my @moy = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); my @dow = qw( Sun Mon Tue Wed Thu Fri Sat ); $_[4] = $moy[ $_[4] ]; $_[5] += 1900; return sprintf "%s., %02u %s. %u, %02u:%02u", $dow[ $_[6] ], @_[3..5], $_[2], $_[1] }
This seems to work and could, I think, be easily adapted for other uses, but I thought I'd see what, if anything the more experienced might be able to add. Note that I played with several things in this and just want to see if a) there is an easier, better way to do it, b) I'm using things appropriately, and c) this reinvents any wheels available in the standard distribution.
And, oh, yes...d) Does GMT use daylight savings time? In my limited tests, $_[8] was always false for gmtime. (Sorry if it's a stupid question, but you have to ask some when adapting to other cultural norms. Otherwise, there's no way to find out what the intolerant really expect.)
Yes, there is more than one way to do it...I'm just trying to make sure this way is an okay way to do it.
Update: Two fixes and assorted comments to replies so far:
--f
In reply to Formatting Time by footpad
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |