in reply to RE: RE: RE: RE: Timezones in Perl
in thread Timezones in Perl

Time-zone conversion is not a trivial problem. If the from/to timezones are constant and you don't have any foolishness like "daylight savings time" to consider you could theoretically do a conversion by adding or subtracting n hours. However, what if that rolls over a day boundary? What about month boundaries? You have to consider years too. You also need to keep track of leap-years to be accurate. By the time you're done you've impelmented a good portion of a full calendar system just to do a timezone conversion. IMHO, much better to use a module.

Replies are listed 'Best First'.
RE: RE: RE: RE: RE: RE: Timezones in Perl
by Shendal (Hermit) on Jun 23, 2000 at 01:05 UTC
    Correct me if I'm wrong, but localtime does all that conversion/daylight savings/boundary work for you. Wouldn't this work?
    use strict; use warnings; sub myGetTime { my($timezone) = shift; $timezone = 0 unless $timezone; # convert to seconds $timezone = $timezone * 3600 if ($timezone); my($time) = time + $timezone; return (gmtime($time)); } print "Local : " . localtime(time) . "\n"; print "London : " . &myGetTime . "\n"; print "Houston: " . &myGetTime(-5) . "\n";
      Your example works fine if you want to add or subtract hours from the current local time. However, the date the original question is not accessible from the time call. It must be parsed, converted to an apropriate timestamp value and de-timezoned before you can start using localtime or gmtime to manipulate it. And what if the output format the person wants isn't what gmtime or localtime produces? It must then be manipulated again into the proper format. All these functions are provided for you by the Date and Time modules.
        Oh. *Smacks forehead* Now I'm with you. I need to read posts more closely. I understand why you'd want to use Date::Manip.

        Good discussion!