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

I need to convert time from GMT to Chicago local time. I tried the following but it looks broken.
sub fromGMT { my ($date, $hms) = @_; my ($Y, $M, $D) = unpack('A4A2A2',$date); my ($H, $M, $S) = $hms =~ m/^(\d+):(\d+):?(\d+)?/; $S ||= 0; $dt=DateTime->new(year=>$Y, month=>$M, day=>$D, hour=>$H,minute=>$M +,second=>$S,time_zone=>'GMT'); $dt->set_time_zone('America/Chicago'); $dt->is_dst() and $dt->add( hours=>1 ); # Unneeded return $dt->hms; }

Replies are listed 'Best First'.
Re: GMT to Chicago TIme
by ikegami (Patriarch) on May 21, 2010 at 21:06 UTC
    Use use strict; use warnings;. You are hiding valuable error messages if you don't. Specifically,
    "my" variable $M masks earlier declaration in same scope at a.pl line +8. Global symbol "$dt" requires explicit package name at a.pl line 9. Global symbol "$dt" requires explicit package name at a.pl line 10. Global symbol "$dt" requires explicit package name at a.pl line 12. Execution of a.pl aborted due to compilation errors.

    Fix:

    sub fromGMT { my ($date, $hms) = @_; my ($Y, $m, $d) = unpack('A4A2A2',$date); my ($H, $M, $S) = $hms =~ m/^(\d+):(\d+)(?::(\d+))?/; $S ||= 0; my $dt = DateTime->new( year=>$Y, month=>$m, day=>$d, hour=>$H, minute=>$M, second=>$S, time_zone=>'GMT' ); $dt->set_time_zone('America/Chicago'); return $dt->hms; }
Re: GMT to Chicago TIme
by Marshall (Canon) on May 22, 2010 at 01:11 UTC
    I need to convert time from GMT to Chicago local time.

    Well, right now in CDST (Central Daylight Standard Time), Add 5 hours to local time to get the time in Greenwich England. When Chicago "goes off daylight savings time", (like the saying: "fall back"), the time difference will be 6 hours instead of 5 hours.

    There are various Perl modules that calculate date/times. But basically they all involve a "trip through GMT". GMT in a Unix sense, is a continuously incrementing number of seconds. CST or CDST is a presentation of GMT. 1:30 AM local time could possibly mean 2 different GMT times for the same local day/time ( the "spring forward, fall back" idea). What "local time" is, is a political decision.

    There is a boundary condition. But essentially the date/time module will know whether to subtract 5hrs*60min*60sec or 6hrs*60min*60sec from Unix "GMT epoch seconds" to get local time. Hard code this or ask a module that has a continuously updated database.

      I don't understand your point. The module he is using is quite aware as to when Chicago uses DST and when it doesn't.
      print(fromGMT('20090101', '12:00:00'), "\n"); # 06:00:00 print(fromGMT('20090701', '12:00:00'), "\n"); # 07:00:00
        My brain was elsewhere on this one...burning a 7 DVD set for one project while working on another coding project. Shouldn't have even been adding a 3rd thing to the multi-tasking list by answering a Perl question. I tried to do too much at once and quality of the result is less than I would have desired. Sorry. My oops.