Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > The best way to parse a date in a known format

Reply
Thread Tools

The best way to parse a date in a known format

 
 
Leif Wessman
Guest
Posts: n/a
 
      08-29-2004
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

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-29-2004
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
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      08-29-2004
Gunnar Hjalmarsson <(E-Mail Removed)> 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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-29-2004
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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-29-2004
Gunnar Hjalmarsson <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date#parse assume a EU format - can it assume a US format? Josh Sharpe Ruby 1 09-21-2010 09:16 AM
Date.parse('17:26:33 Oct 31, 2009') returns invalid date Jacob Gorban Ruby 12 11-02-2009 02:29 PM
Which should be the correct date format for Date.parse ? Matteo Javascript 4 06-15-2004 09:15 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM



Advertisments