Database Reference
In-Depth Information
values in the range from 1970 to 2069. If your values lie outside this range, add the
proper century yourself before storing them into MySQL.
To use a different transition point, convert years to four-digit form yourself. Here's a
general-purpose routine that converts two-digit years to four digits and supports an
arbitrary transition point:
sub yy_to_ccyy
{
my ( $year , $transition_point ) = @_ ;
$transition_point = 70 unless defined ( $transition_point );
$year += ( $year >= $transition_point ? 1900 : 2000 ) if $year < 100 ;
return $year ;
}
The function uses MySQL's transition point (70) by default. An optional second argu‐
ment may be given to provide a different transition point. yy_to_ccyy() also verifies
that the year actually is less than 100 and needs converting before modifying it. That
way you can pass year values regardless of whether they include the century. Some
sample invocations using the default transition point have the following results:
$val = yy_to_ccyy ( 60 ); # returns 2060
$val = yy_to_ccyy ( 1960 ); # returns 1960 (no conversion done)
Suppose that you want to convert year values as follows, using a transition point of 50:
00 .. 49 -> 2000 .. 2049
50 .. 99 -> 1950 .. 1999
To do this, pass an explicit transition point argument to yy_to_ccyy() :
$val = yy_to_ccyy ( 60 , 50 ); # returns 1960
$val = yy_to_ccyy ( 1960 , 50 ); # returns 1960 (no conversion done)
The yy_to_ccyy() function is included in the Cookbook_Utils.pm library file.
12.11. Performing Validity Checking on Date or
Time Subparts
Problem
A string passes a pattern test as a date or time, but you want to perform further validity
checking.
Solution
Break the value into parts and perform the appropriate range checking on each part.
Search WWH ::




Custom Search