in reply to DateTime conversion

Hard to know where to start here. Firstly, you seem to have completely misunderstood the DateTime::Format::Builder tutorial. Please read it again. More carefully this time.

Secondly, even when I rewrote your example to use the module correctly, it still didn't work as your regex didn't come close to matching your date string. Having corrected that as well, the code I ended up with was:

#!/usr/bin/perl use strict; use warnings; package DateTime::Format::MyParser; use DateTime::Format::Builder ( parsers => { parse_datetime => { regex => qr/^(\d?\d)\s(\d\d)\s(\d\d\d\d) (\d\d):(\d\d):(\d\d)$/ +, params => [ qw( day month year hour minute second ) ], }, }, ); package main; my $date = "Thu, 7 Aug 2008 08:35:36 +01:00"; $date =~ s/Thu, //; $date =~ s/\s+\+01:00$//; $date =~ s/Aug/08/; my $parser = DateTime::Format::MyParser->new(); my $dt = $parser->parse_datetime($date); print $dt;

It still bothers me that you do all that munging of the input string before trying to use it. Surely the point of parsing a date string is to parse the string that you are given, not to have to munge your string into some intermediate format first.

Finally, I think that DateTime::Format::Builder might be completely the wrong tool for what you are doing here. DateTime::Format::Builder is for building DateTime::Format modules (there's a clue in the name). If you're just want to parse a date string and turn it into a DateTime object, then you probably want DateTime::Format::Strptime instead.

--

See the Copyright notice on my home node.

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

Replies are listed 'Best First'.
Re^2: DateTime conversion
by dHarry (Abbot) on Aug 07, 2008 at 11:35 UTC

    Thou art wise brother davorg.

    Your recommendation/advice is more than to the point.

    However your statement on Date::Manip is a bit harsh. With such a nice date format one can expect it to work fine (Except when you run Windows of course in case you'll have to set the TimeZone yourself).

    use strict; use warnings; use Date::Manip qw(ParseDate); our $TZ = "CET"; # I am running Windows;-) my $date = ParseDate("Thu, 7 Aug 2008 08:35:36 +01:00"); print $date;

      I didn't mean to imply that Date::Manip wouldn't work. I know that it will work fine. I will, however, admit that my advice could have been more detailed.

      Date::Manip is an old module. Even it's author says that it is usually the wrong module to use. The most common criticisms of it are that it is large and slow. Personally, my biggest criticism of it is that it doesn't play well with others. The DateTime family of modules all work together well. If you're doing any work with dates and times then I really think that they should be the first place that you look.

      --

      See the Copyright notice on my home node.

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