The best way to parse a date in a known format

L

Leif Wessman

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
 
G

Gunnar Hjalmarsson

Leif said:
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";
}
 
A

Anno Siegel

Gunnar Hjalmarsson said:
Leif said:
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
 
G

Gunnar Hjalmarsson

Anno said:
Gunnar said:
Leif said:
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.
 
A

Anno Siegel

Gunnar Hjalmarsson said:
Anno said:
Gunnar said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top