- NAME
- SYNOPSIS
- DESCRIPTION
- Time::Format PATTERNS
- strftime PATTERNS
- ISO-8601 DATE/TIME MATCHING
- RFC 2822 MATCHING
- INFORMAL AMERICAN MATCHING
- FUZZY PATTERN OVERVIEW
- FUZZY PATTERN DETAILS
- Time pattern (Hour-minute-second)
- CAPTURES (-keep)
- EXAMPLES
- EXPORTS
- SEE ALSO
- REQUIREMENTS
- AUTHOR
- LICENSE AND COPYRIGHT
NAME
Regexp::Common::time - Date and time regexps.
SYNOPSIS} #.
DESCRIPTION,:
tfor
strftime.
If you are parsing RFC-2822 mail headers, use the
If you are parsing informal American dates, use the
americanpattern.
If there is some variability in your input data, use one of the fuzzy-matching patterns in the
dmy,
mdy, or
ymdfamilies.
If the data are wildly variable, such as raw user input, you should probably give up and use Date::Manip or Date::Parse.
Time values are generally much simpler to parse than date values. Only one fuzzy pattern is provided, and it should suffice for most needs.
Time::Format PATTERNS
The Time::Format module uses simple, intuitive strings for specifying date and time formats. You can use these patterns here as well. See Time::Format
strftime PATTERNS".'};
The patterns can contain more complex regexp expressions as well:
$str =~ $RE{time}{strftime}{-pat => '(%A|%a)? (%B|%b) ?%_d, %Y'};
ISO-8601 DATE/TIME MATCHING:
The date separator character may be a hyphen, slash (
/), period, or empty string (omitted). The two date separators must match.
The time separator character may be a colon, a period, a space, or empty string (omitted). The two time separators must match.
The date-time separator may be a
T, an underscore, a space, or empty string (omitted).
Either the date or the time may be omitted. But at least one must be there.
If the date is not omitted, all three of its components must be present.
If the time is not omitted, all three of its components must be present.
RFC 2822 MATCHING,
INFORMAL AMERICAN MATCHING PATTERN OVERVIEW
Fuzzy date patterns have the following properties in common:
The pattern names consist of the letters
y,
m, and
d, each optionally followed by a digit (
2for
mand
d;
2or
4for
y).
If a
yis followed by a 2 or a 4, it must match that many digits.
If a
yhas no trailing digit, it can match either 2 or 4 digits, trying 4 first.
If an
mis followed by a 2, then only two-digit matches for the month are considered, and month names are not matched.
If an
mis not followed by a 2, then the month may be 1 or 2 digits, or a spelled-out name.
Just like for months, if a
dis followed by a 2, then only two-digit matches for the day are considered.
Just like for months, if a
dhas no trailing digit, then the day may be 1 or 2 digits, and a 1-digit match may not have any adjacent digits.
The uppercase
DMY,
MDY, and
YMDpatterns are synonyms for the strict
d2m2y4,
m2d2y4, and
y4m2d2patterns, respectively.
If a one-digit match is considered for the month, then no adjacent digits are allowed. (e.g.: "1/23/45" in M/D/Y format has a valid one-digit month match, but "12345" does not. Nor does "91/23/45").
If a pattern begins with an digitless.
Likewise, a pattern ending with a digitless
dor
ywill not match if there are trailing digits in the string.
FUZZY PATTERN DETAILS
Year-Month-Day order
- $RE{time}{ymd}
.
- $RE{time}{y4md}
"2005/4/2" =~ $RE{time}{y4md}; "2005 APR 02" =~ $RE{time}{y4md};
This works as $RE{time}{ymd}, except that the year is restricted to be exactly 4 digits.
- $RE{time}{y4m2d2}
"2005/04/02" =~ $RE{time}{y4m2d2};
This works as $RE{time}{ymd}, except that the year is restricted to be exactly 4 digits, and the month and day must be exactly 2 digits each.
- $RE{time}{y2md}
"05/4/2" =~ $RE{time}{y2md}; "05.APR.02" =~ $RE{time}{y2md};
This works as $RE{time}{ymd}, except that the year is restricted to be exactly 2 digits.
- $RE{time}{y2m2d2}
"05/04/02" =~ $RE{time}{y2m2d2};
This works as $RE{time}{ymd}, except that the year is restricted to be exactly 2 digits, and the month and day must be exactly 2 digits each.
- $RE{time}{YMD}
"2005/04/02" =~ $RE{time}{YMD};
This is a shorthand for the "canonical" year/month/day format,
y4m2d2.
Month-Day-Year (American) order
- $RE{time}{mdy}
-
- $RE{time}{mdy4}
-
- $RE{time}{m2d2y4}
-
- $RE{time}{mdy2}
-
- $RE{time}{m2d2y2}
-
- $RE{time}{MDY}
These patterns function as the equivalent year/month/day patterns, above; the only difference is the order of the elements.
MDYis a synonym for
m2d2y4.
Day-Month-Year (European) order
- $RE{time}{dmy}
-
- $RE{time}{dmy4}
-
- $RE{time}{d2m2y4}
-
- $RE{time}{dmy2}
-
- $RE{time}{d2m2y2}
-
- $RE{time}{DMY}
These patterns function as the equivalent year/month/day patterns, above; the only difference is the order of the elements.
DMYis a synonym for
d2m2y4.
Time pattern (Hour-minute-second)
- $RE{time}{hms}
.
CAPTURES (-keep)
$1 - the entire match
$2 - the day
$3 - the month
$4 - the year
$5 - the hour
$6 - the minute
$7 - the second
$8 - the time zone
The
american pattern captures:
$1 - the entire match
$2 - the month
$3 - the day
$4 - the year)
EXAMPLES
# my $matched; foreach my $pattern (qw(mdy dmy ymd)) { @values = $record =~ /^$RE{time}{$pattern}{-keep}/ or next; $matched = $pattern; } if ($matched) { eval{ ($year, $month, $day) = normalize_rct($matched, @values) }; if ($@) { .... # handle erroneous data } } else { .... # no match } # # '
EXPORTS
This module exports no symbols to the caller's namespace.
SEE ALSO
It's not enough that the date regexps can match various formats. You then have to parse those matched data values and translate them into useful values. The Time::Normalize module is highly recommended for performing this repetitive, error-prone task.
REQUIREMENTS
Requires Regexp::Common, of course.
If POSIX and I18N::Langinfo are available, this module will use them; otherwise, it will use hardcoded English values for month and weekday names.
Test::More is required for the test suite.
AUTHOR
Eric J. Roode, ROODE -at- cpan -dot- org
LICENSE AND COPYRIGHT
Copyright (c) 2005-2008 by Eric J. Roode, ROODE -at- cpan -dot- org
To avoid my spam filter, please include "Perl", "module", or this module's name in the message's subject line, and/or GPG-sign your message.
This module is copyrighted only to ensure proper attribution of authorship and to ensure that it remains available to all. This module is free, open-source software. This module may be freely used for any purpose, commercial, public, or private, provided that proper credit is given, and that no more-restrictive license is applied to derivative (not dependent) works.
Substantial efforts have been made to ensure that this software meets high quality standards; however, no guarantee can be made that there are no undiscovered bugs, and no warranty is made as to suitability to any given use, including merchantability. Should this module cause your house to burn down, your dog to collapse, your heart-lung machine to fail, your spouse to desert you, or George Bush to be re-elected, I can offer only my sincere sympathy and apologies, and promise to endeavor to improve the software.