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

Venerable Monks,

I am using DateTime::Format::Strptime with a date in the format '%d%b%g' (ie. 05Jul08);
$date = "05Jul08"; my $strp = DateTime::Format::Strptime->new( pattern => '%d%b%g', locale => 'en_US', on_error => 'croak', ); my $dt = $strp->parse_datetime($date);
According to DateTime, %b should be the "abbreviated month name". I assume that with the 'en_US' locale set, that would mean 'Jul' in my case. However DateTime::Format::Strptime seems to be looking for 'July', or something with four characters: This is the error I get using the 'croak' parameter.
There is no use providing a month name (Jul0) without providing a year +. at parse_raw_reports.pl line 63

Am I assuming that DateTime::Format::Strptime does something that it doesn't do? Can someone see what I might be doing wrong?

Thanks,

mb

Replies are listed 'Best First'.
Re: DateTime::Format::Strptime - strftime formats not working?
by pc88mxer (Vicar) on Jul 10, 2008 at 17:27 UTC
    I haven't looked at the code, but testing indicates the following are probably true:

    1) %g and %G do not seems to be recognized as year formats; only %y and %Y seem to work. I think %g and %G are meant to be used in conjunction with ISO 8601 week numbers (e.g. %V).

    2) adding a space helps a lot - otherwise only a single year digit works:

    $date = "05Jul 08"; # work with pattern => "%d%b %y" $date = "05Jul8"; # works with pattern => "%d%b%y"
      I wonder why zero-filled date values are not accepted. I guess these formats come from old C libraries (and are therefore not easily improved upon)? Thanks for your help, I'm all set now!
Re: DateTime::Format::Strptime - strftime formats not working?
by moritz (Cardinal) on Jul 10, 2008 at 17:23 UTC
    The docs says
    # %b or %B or %h
    The month name according to the current locale, in abbreviated form or the full name.

    In context of your question I parse that as:

    %b => month name in current locale %B => abbreviated form %h => full name (whatever that may mean)

    Update: And it turns out that I'm wrong. Changing %b to %B doesn't work. The test suite uses this snippet:

    21: ['%b %d, %Y', 'Jan 24, 2003'], 22: ['%B %d, %Y', 'January 24, 2003'],

    So it seems that %b is really meant to be the short form, and %B is meant to be long form.

    Separating the month from the year with a blank in both the format and the input string still gives an error message though:

    There is no use providing a month name (Jul) without providing a year

    So maybe the problem is actually with the year?

      No, the tokens are defined in DateTime and %b really is the abbreviated form. But initself, it wouldn't hurt to try out %B and see if it changes anything.

        I did. It doesn't change anything.