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

Hi all, Given 26/Aug/2005:01:05:43 -0400, how do I convert it to 20050826050543Z Let me know thx mike

Replies are listed 'Best First'.
Re: date conversion
by saintmike (Vicar) on Aug 27, 2005 at 16:30 UTC
    use Date::Parse; use DateTime; my $time = str2time("26/Aug/2005:01:05:43 -0400"); my $dt = DateTime->from_epoch( epoch => $time ); print $dt->ymd(''), $dt->hms(''), "Z\n";

      Z is actually a military time zone. I am wondering whether the time zone conversion has been handled.

        The from_epoch method defaults to the UTC time zone, which is the same as "Z".

        When I just tried to install DateTime on my Windows system, there seemed to be a circular installation dependency between DateTime and DateTime::TimeZone. Just FYI.

        As an alternative to the second part of saintmike's solution, one could do:

        use Date::Parse; use POSIX; my $time = str2time("26/Aug/2005:01:05:43 -0400"); print POSIX::strftime("%Y%m%d%H%M%SZ", gmtime($time)), "\n";
        'Z' is actually, in the days when such zones were in common use - GMT. It is used by the military, but it is not a military time zone.

        jdtoronto