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

Dear PerlMonks, Please help me. This is my first post on SOPM so beg you forgive any indiscretions.
The perl code I have managed to "put together" is not giving me the right answers. Here is a data sample that I want it to process:
20050526~18:15:06 GMT 20040401~01:12:02 GMT 20060201~00:59:01 GMT 20060401~01:01:01 EST 20040201~10:10:01 -0600 20010901~04:23:05 -0400 20050526~18:15:06 GMT 20040401~01:12:02 GMT 20000101~00:59:01 -0200 19841223~04:22:06 MST
And here is the perl code cleaned up a bit with PerlTidy
#!/usr/bin/perl -w use strict; use Date::Manip; Date_Init("TZ=EST"); #set TimeZone to Eastern Daylight Savings Time { my $input = $ARGV[0]; #returns filename from command line my $thetmz; #time zone for that line my ( $date, $time); #from file chomp $input; #strip the carriage return sub convertthetmz { $datetime = ( "$date" . "$time" ) ; #$date and #time from subroutine call and concats them $converteddate = ParseDate("$datetime"); #date-time strng $converteddate =Date_ConvTZ("$converteddate", $thetmz, "EST" ) ; #converts from any tmz to EST print("$converteddate~$from~$to\n"); } open( DATAFILE, "$input" ) || die("Can not open $input:!\n"); #access the file while (<DATAFILE>) { chomp $_; ( $date, $time) = split( "~", $_ ); #split date/time my $testfortmz = substr $time, -3; #last 4 chars of str if ( "$testfortmz" == "-0400" ) { $thetmz = "AST"; convertthetmz( $thetmz, $date, $time, $from, $to ); } elsif ( "$testfortmz" == "-0600" ) { $thetmz = "CST"; convertthetmz( $thetmz, $date, $time, $from, $to ); } elsif ( "$testfortmz" =~ " GMT" ) { $thetmz = "GMT"; convertthetmz( $thetmz, $date, $time, $from, $to ); } else #this is need to work for some reason { my $nochangedate; $nochangedate = ( ParseDate("$date.$time") ); print("$nochangedate~$from~$to\n"); } } close(DATAFILE); }
The result that I get with the above data sample is the following:
2005052613:15:06 2004033120:12:02 2006013119:59:01 2006040101:01:01 2004020112:10:01 2001090102:23:05 2005052613:15:06 2004033120:12:02 1999123121:59:01 1984122306:22:06
Some of these are correct but others are I believe wrong.
Shouldn't 20040201~10:10:01 -0600 be 2004020111:10:01 EST since there is only 1 hour difference between -600 and -0500?
Obviously I did something wrong. I would greatly appreciate your assistance! Thank you kindly!

Replies are listed 'Best First'.
Re: TimeZone Assistance Requested
by almut (Canon) on Jul 28, 2007 at 03:07 UTC

    First, welcome at the Monastery.  There are a couple of issues with your code, but as it's time to go to bed in my local TZ, I hope some other kind monks will point them out :)

    Anyhow, if the idea is to convert those dates (with any TZ) into EST, the whole thing can be simplified considerably, because Date::Manip already does most of the magic while parsing the input string...

    #!/usr/bin/perl -w use strict; use Date::Manip; Date_Init("TZ=EST"); # target TZ # use your filehandle here instead of DATA while (my $date_anyTZ = <DATA>) { chomp $date_anyTZ; # get rid of '~' to make date parseable by Date::Manip $date_anyTZ =~ tr/~/ /; my $date_EST = UnixDate(ParseDate($date_anyTZ), "%Y-%m-%d %H:%M:%S + %Z"); printf "%-24s --> %s\n", $date_anyTZ, $date_EST; } __DATA__ 20050526~18:15:06 GMT 20040401~01:12:02 GMT 20060201~00:59:01 GMT 20060401~01:01:01 EST 20040201~10:10:01 -0600 20010901~04:23:05 -0400 20050526~18:15:06 GMT 20040401~01:12:02 GMT 20000101~00:59:01 -0200 19841223~04:22:06 MST

    Prints

    20050526 18:15:06 GMT --> 2005-05-26 13:15:06 EST 20040401 01:12:02 GMT --> 2004-03-31 20:12:02 EST 20060201 00:59:01 GMT --> 2006-01-31 19:59:01 EST 20060401 01:01:01 EST --> 2006-04-01 01:01:01 EST 20040201 10:10:01 -0600 --> 2004-02-01 11:10:01 EST 20010901 04:23:05 -0400 --> 2001-09-01 03:23:05 EST 20050526 18:15:06 GMT --> 2005-05-26 13:15:06 EST 20040401 01:12:02 GMT --> 2004-03-31 20:12:02 EST 20000101 00:59:01 -0200 --> 1999-12-31 21:59:01 EST 19841223 04:22:06 MST --> 1984-12-23 06:22:06 EST

    which looks OK to me.

      Thank you Almut for the code and everyone else for your assistance. I worked on it and it works good. Here is the code:
      #!/usr/bin/perl -w use strict; use Date::Manip; Date_Init("TZ=EST"); # { my $input =$ARGV[0]; #returns filename from command line my $date_anyTZ; chomp $input; #strip the carriage return open (DATAFILE, "$input")|| die ("Can not open $input:!\n"); #access the file while (my $date_anyTZ=<DATAFILE>) { chomp $date_anyTZ; # get rid of '~' to make date parseable by Date::Manip $date_anyTZ =~ tr/~/ /; my $date_EST = UnixDate(ParseDate($date_anyTZ), "%Y-%m-%d %H:%M:%S %Z"); printf "%-24s --> %s\n", $date_anyTZ, $date_EST; } close (DATAFILE); }
Re: TimeZone Assistance Requested
by Anonymous Monk on Jul 28, 2007 at 03:05 UTC
    http://en.wikipedia.org/wiki/Eastern_Standard_Time
    * Eastern Standard Time - three separate time zones use this name: o the Australian time zone at UTC+10 o the Brazilian Eastern time zone at UTC-3 o the North American time zone at UTC-5 * European Summer Time - the daylight-saving time used in Europe.
Re: TimeZone Assistance Requested
by Anonymous Monk on Jul 28, 2007 at 02:44 UTC
    You're parsing your timestamps wrong
    perl -e ' die substr q!012345678!,-3 ' 678 at -e line 1