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

Dear Monks

I'm trying to convert something like
2005-01-28 10:23:59
My OS uses the CET time zone, so the perl-script convert the date to UTC ( -1 hour). But the input date is already UTC.......
Any suggestions how I change the timezone inside this script using Time::Local ?

Thanks a lot
Luca

Replies are listed 'Best First'.
Re: Convert dates with Time::Local
by davorg (Chancellor) on Feb 13, 2006 at 14:57 UTC
      Thnx, Do you know how to use timegm when you have a julian day and not a month and day ?

      Luca

        Like so:

        use Time::Local qw/timegm_nocheck/; my $date_string = "2005301"; # The 301st day of 2005 my ($year,$julian_day) = $date_string =~ /^(\d\d\d\d)(\d\d\d)$/; my $gmt = timegm_nocheck 0,0,0,$julian_day,0,$year; # ... do something with the gmt value

        Way back in the days of perl4, timelocal.pl (from whence Time::Local is derived) did no validation of the numbers since it is a straight numeric calculation that does not depend on how many days there are in a given month or how many hours in a day, etc. timegm_nocheck and timelocal_nocheck implement the modern-day equivalents of this idea.

        But, you'd know this if you'd read the documentation

        I don't think I'd use timegm with a Julian date.

        Jan 1st 1970 has a Julian date of 2440587. Surely you can just subtract that from the Julian date you have and then multiply by the number of seconds in a day to get the number of seconds since the epoch. This, of course, assumes that your computer uses 1970-01-01 00:00:00 as its epoch.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg