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

Hi all,
I'm trying to convert the following formatted atom XML time stamp to something a bit more friendly that I'd like to send to a string so it can be used in a SQL DateTime field. Here are the formats:

XML Format: 2012-02-25T18:48:34.835-05:00
Desired Format: 2012-02-25 18:48:34

I've gone through some formatting through Date::Format but nothing is correctly converting this into the proper format. Any ideas?

Replies are listed 'Best First'.
Re: Format XML Atom DateTime
by tobyink (Canon) on Feb 26, 2012 at 08:19 UTC

    This does the trick:

    use 5.010; use DateTime::Format::ISO8601; say DateTime::Format::ISO8601 -> parse_datetime('2012-02-25T18:48:34.835-05:00') -> strftime('%F %T');

    However, note that you're dropping the time zone altogether! Unless you know that all the Atom entries have been posted from the same time zone, this is a bad idea.

    An entry dated 2012-02-25T12:00:00+12:00 was written a full day earlier than one at 2012-02-25T12:00:00-12:00, yet using the conversion above, they'd both get formatted as 2012-02-25 12:00:00.

    To solve that you could normalise them all to the same time zone before formatting. UTC is the time zone most commonly used for that purpose.

    use 5.010; use DateTime::Format::ISO8601; say DateTime::Format::ISO8601 -> parse_datetime('2012-02-25T18:48:34.835-05:00') -> set_time_zone('UTC') -> strftime('%F %T');

      local would be another obvious time zone choice, but there is information loss in using it. (Some times occur twice on some days in some time zones.)

      '%F %T' might be clearer as '%Y-%m-%d %H:%M:%S'.

Re: Format XML Atom DateTime
by ikegami (Patriarch) on Feb 26, 2012 at 08:06 UTC
    $src =~ /^(....-..-..)T(..:..:..)/ or die; my $dst = "$1 $2";
Re: Format XML Atom DateTime
by Anonymous Monk on Feb 25, 2012 at 23:59 UTC

    my $xmljunk = "2012-02-25T18:48:34.835-05:00 "; my $desired = $xmljunk; $desired =~ s/T/ /; $desired =~ s/\..*$//; print "$xmljunk\n$desired\n\n"; __END__ 2012-02-25T18:48:34.835-05:00 2012-02-25 18:48:34
      Ha, thanks. Regular expressions are a sneaky way of doing this. Occasionally this will not end in a decimal value, so the -0500 of EST will still show up, but this is a minor annoyance that can be fixed. Just for reference, is the xml date format I mentioned able to be identified and re-formatted using Date::Format or another perl module?

        Just for reference, is the xml date format I mentioned able to be identified and re-formatted using Date::Format or another perl module?

        Never used Date::Format before, but a minute of looking shows its should be able to handle it without any problem

        #!/usr/bin/perl -- use strict; use warnings; use Date::Format(); use Date::Parse(); use feature qw/ say /; my @t = Date::Parse::strptime( q/2012-02-25T18:48:34.835-05:00/); say Date::Format::strftime( q/%Y-%m-%d %H:%M:%S/, @t, ); __END__ 2012-02-25 18:48:34