The best way to parse a date in a known format

Discussion in 'Perl Misc' started by Leif Wessman, Aug 29, 2004.

  1. Leif Wessman

    Leif Wessman Guest

    What's the best way to parse a date in a, for me, known format?

    In my case the months are spelled a little different (not matching a
    specific locale). But I know than "janu" means "january" and that the
    format of the date is "date year month".

    First of all I need a way to check if the datetext has the format that
    I think it should have.

    Then I would like to change this datetext into a common format that a
    regular date parser would understand.

    use strict;
    use warnings;

    my @months =
    ("janu","febr","mars","apri","maj","juni","juli","augu","sept","okto","nove","dece");
    my $dateformat = "dd yy/@months";
    my $datetext = "05 04/janu";

    # Now I need to create a regular expression
    # to see if $datetext matches $dateformat

    my $regexp = ...; # need help here

    if ($datetext =~ /$regexp/) {

    # Now I need to modify it into a common
    # format, like: "05-01-2004"
    } else {
    print "wrong format";
    }

    Thanks for any input!

    Leif
     
    Leif Wessman, Aug 29, 2004
    #1
    1. Advertising

  2. Leif Wessman wrote:
    > What's the best way to parse a date in a, for me, known format?
    >
    > In my case the months are spelled a little different (not matching
    > a specific locale). But I know than "janu" means "january" and that
    > the format of the date is "date year month".
    >
    > First of all I need a way to check if the datetext has the format
    > that I think it should have.
    >
    > Then I would like to change this datetext into a common format that
    > a regular date parser would understand.


    Why would you involve a regular date parser at all?

    > use strict;
    > use warnings;
    >
    > my @months =
    > ("janu","febr","mars","apri","maj","juni","juli","augu","sept","okto","nove","dece");


    From here you can for instance do:

    my $datetext = '05 04/janu';
    my $regexp = qr!^(\d\d) (\d\d)/(@{ [ join '|', @months ] })$!;
    my ($day, $month, $year);

    if ( $datetext =~ /$regexp/ ) {
    my %months;
    @months{ @months } = 1 .. 12;
    $day = $1;
    $month = $months{$3};
    $year = $2 + 2000;
    printf "ISO 8601 format: %d-%02d-%02d\n",
    $year, $month, $day;
    } else {
    print "wrong format\n";
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 29, 2004
    #2
    1. Advertising

  3. Leif Wessman

    Anno Siegel Guest

    Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
    > Leif Wessman wrote:
    > > What's the best way to parse a date in a, for me, known format?


    [...]^

    > Why would you involve a regular date parser at all?
    >
    > > use strict;
    > > use warnings;
    > >
    > > my @months =
    > >

    > ("janu","febr","mars","apri","maj","juni","juli","augu","sept","okto","nove","dece");
    >
    > From here you can for instance do:
    >
    > my $datetext = '05 04/janu';
    > my $regexp = qr!^(\d\d) (\d\d)/(@{ [ join '|', @months ] })$!;


    It would be more robust to sort the month names into descending order by
    length before building the month-parsing part:

    join '|' sort { length( $b) <=> length( $a) } @months

    It doesn't make a difference with this set of month names, but
    who knows...

    Anno
     
    Anno Siegel, Aug 29, 2004
    #3
  4. Anno Siegel wrote:
    > Gunnar Hjalmarsson wrote:
    >> Leif Wessman wrote:
    >>>
    >>> my @months = ("janu","febr","mars","apri","maj","juni",
    >>> "juli","augu","sept","okto","nove","dece");

    >>
    >> From here you can for instance do:
    >>
    >> my $datetext = '05 04/janu';
    >> my $regexp = qr!^(\d\d) (\d\d)/(@{ [ join '|', @months ] })$!;

    >
    > It would be more robust to sort the month names into descending
    > order by length before building the month-parsing part:
    >
    > join '|' sort { length( $b) <=> length( $a) } @months
    >
    > It doesn't make a difference with this set of month names, but who
    > knows...


    It doesn't make a difference with any set of month names, since the
    pattern is anchored to the end of the string.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 29, 2004
    #4
  5. Leif Wessman

    Anno Siegel Guest

    Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    > > Gunnar Hjalmarsson wrote:
    > >> Leif Wessman wrote:
    > >>>
    > >>> my @months = ("janu","febr","mars","apri","maj","juni",
    > >>> "juli","augu","sept","okto","nove","dece");
    > >>
    > >> From here you can for instance do:
    > >>
    > >> my $datetext = '05 04/janu';
    > >> my $regexp = qr!^(\d\d) (\d\d)/(@{ [ join '|', @months ] })$!;

    > >
    > > It would be more robust to sort the month names into descending
    > > order by length before building the month-parsing part:
    > >
    > > join '|' sort { length( $b) <=> length( $a) } @months
    > >
    > > It doesn't make a difference with this set of month names, but who
    > > knows...

    >
    > It doesn't make a difference with any set of month names, since the
    > pattern is anchored to the end of the string.


    Ah, okay. I didn't see that.

    Anno
     
    Anno Siegel, Aug 29, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,169
    Janaka
    Feb 18, 2004
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,271
    Michael Borgwardt
    May 30, 2004
  3. Jacob Gorban
    Replies:
    12
    Views:
    244
    Reid Thompson
    Nov 2, 2009
  4. Josh Sharpe
    Replies:
    1
    Views:
    210
    Brian Candler
    Sep 21, 2010
  5. Matteo
    Replies:
    4
    Views:
    155
    Dr John Stockton
    Jun 15, 2004
Loading...

Share This Page