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

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

Replies are listed 'Best First'.
Re: Formatting Time
by Maclir (Curate) on Sep 10, 2001 at 01:11 UTC
    To answer part (d) "Does GMT use daylight savings time?", the answer is no.

    To be slightly more pedantic, GMT stands for "Greenwich Mean Time", and refers back to when Brittania ruled the waves - at least some of them. In order to navigate the oceans, one needed to know how far east or west of your home port you were. Greenwich, just east of London, was (is?) the site of the Royal Observatory, and given that navigation and astronomy have been linked, Grennwich was taken as the origin east or west. Hence, 0 degrees of latitude refers to the imaginary line running from north to south pole passing through Greenwich.

    Every 15 degrees west of Greenwich corresponds to an hour later in local time, and each 15 degrees east is an hour earlier. (360 / 24 = 15).

    By definition, GMT cannot have any daylight savings time adjustment applied to it. Now if the jolly citizens of England want to have daylight savings time, then they can - but then that are not on GMT - but probaby something called British Summer Time.

    And to be truly pedantic, GMT no longer exists - the standard for the prime meridian is "UTC" Universal Time Coordinated (we can thank the French influence on the standards bodies for the strange word order). It is sometimes referred to in military circles as "Zulu Time", since each 15 degree time zone has a letter associated with it, and omitting "I" and "O" (since they get confused with the digits one and zero), you start wiht Z = zulu in the international phonetic alphabet, and then A , B, C . . . as you move east. When I use to live in eastern Australia was Kilo (K) time at 10 hours ahead of UTC.

    Gasp. Didn't mean to stretch that out into a history of time zones.

      In reply to footpad, he might want to look at ISO8601 an overview of which is here. The supposed standard date/time rules. (ha!) (Actually I found something interesting that im going to use at work. EU rule EN 28601 specifies that dates in all EU countries should be represented as YYYY-MM-DD not the DD.MM.YY they love in Germany, and not the DD/MM/YY they love in the UK, certainly NOT the dumb US standard MM-DD-YY. Cool.)

      Maclir, I dont know why you're apologising. Time is pretty interesting once you've had to program it, or if you are interested in watches. The reason GMT won over the PMT (Paris Meridian Time, obviusely 'not' what the French called it) was that the British government funded the production of better maps than the French at the time when precision navigation was being opened up by the invention of the Naval Chronometer (a British Invention). Apparently it was not unheard of to have French Warships with translated or even untranslated copies of British maps on board.

      As the British made the best maps, with all measurements based on GMT, they became the standard and so did GMT. When there finally was an international congress on the matter, (along with time zones), a number of neutral meridians were proposed but eventually it was decided more financially expedient to formalize the defacto standard, ie GMT. (No need to replace the bulk of the existstant maps.)

      UPDATE
      Added link to ISO8061 page and a little commentary on dates in EU.
      Yves
      --
      You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

Re: Formatting Time
by kschwab (Vicar) on Sep 10, 2001 at 03:45 UTC
    I must be missing something, but this looks like a job for POSIX::strftime. I couldn't figure out what the last %s in your sprintf was for, so that's not handled.

    use POSIX qw(strftime); sub formatDTV { return strftime("%a., %d %b. %Y, %H:%M",@_); }
Re: Formatting Time
by mitd (Curate) on Sep 10, 2001 at 04:44 UTC
    You might want to check out Date::Format it could be your friend.

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

Re: Formatting Time
by tachyon (Chancellor) on Sep 10, 2001 at 02:17 UTC

    Is there something inherently wrong with this (it gives the same result, albeit in a slightly different format)?

    print scalar localtime, ' - (GMT: ', scalar gmtime, ')';

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print