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

Dear Monks,
I've been trying to convert a date into a datetime object for later use but keep coming across the error:
Can't Locate object method "parse_datetime" via package DateTime:Format:Builder:Parser
but its used in the DateTime tutorial.
use strict; use warnings; use DateTime::Format::Builder; my $date = "Thu, 7 Aug 2008 08:35:36 +01:00"; $date =~ s/Thu\, //; $date =~ s/\+01:00$//; $date =~ s/Aug/08/; my $parser = DateTime::Format::Builder->new(); $parser = DateTime::Format::Builder->create_parser( regex => qr/^(\d\d)(\d\d)(\d\d\d\d)T(\d\d)(\d\d)(\d\d)$/, params => [ qw( day month year hour minute second ) ], ); my $dt = $parser->parse_datetime($date); print $parser->format_datetime($dt);
I'd be grateful for any pointers in converting this date.

Replies are listed 'Best First'.
Re: DateTime conversion
by davorg (Chancellor) on Aug 07, 2008 at 10:49 UTC

    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

      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

Re: DateTime conversion
by wfsp (Abbot) on Aug 07, 2008 at 11:28 UTC
    For this sort of thing I use Date::Format and Date::Parse which give you str2time and time2str functions.
    #!/usr/local/bin/perl use warnings; use strict; use Date::Format; use Date::Parse; my $str = "Thu, 7 Aug 2008 08:35:36 +01:00"; my $time = str2time($str); print qq{time: $time\n}; my $datekey = time2str(q{%Y%m%d}, $time); print qq{datekey: $datekey\n}; __DATA__ ouputs: time: 1218094536 datekey: 20080807
Re: DateTime conversion
by Bloodnok (Vicar) on Aug 07, 2008 at 10:19 UTC
    Out of interest, why not use Date::Manip ?

    If no other reason, apart from ease of use, it involves far less typing ;-)

    A user level that continues to overstate my experience :-))
      from Date::Manip:
      If you look in CPAN, you'll find that there are a number of Date and Time packages. Is Date::Manip the one you should be using? In my opinion, the answer is no most of the time. This sounds odd coming from the author of the software, but read on.