Database Reference
In-Depth Information
return undef unless $s =~ /^(\d{1,2})\D(\d{2})\D(\d{2})(?:\s*(AM|PM))?$/i ;
my ( $hour , $min , $sec ) = ( $1 , $2 , $3 );
if ( $hour == 12 && ( ! defined ( $4 ) || uc ( $4 ) eq "AM" ))
{
$hour = "00" ; # 12:xx:xx AM times are 00:xx:xx
}
elsif ( $hour < 12 && defined ( $4 ) && uc ( $4 ) eq "PM" )
{
$hour += 12 ; # PM times other than 12:xx:xx
}
return [ $hour , $min , $sec ]; # return hour, minute, second
}
Both functions return undef for values that don't match the pattern. Otherwise, they
return a reference to a three-element array containing the hour, minute, and second
values.
After you obtain the time components, pass them to is_valid_time() , another utility
routine, to perform range checks.
12.12. Writing Date-Processing Utilities
Problem
There's a date-processing operation that you must perform frequently. You want to write
a utility that does it for you.
Solution
The utilities in this recipe provide some examples that show how to do that.
Discussion
Due to the idiosyncratic nature of dates, you might occasionally find it necessary to
write date converters. This section shows some sample converters that serve various
purposes:
isoize_date.pl reads a file looking for dates in US format ( MM-DD-YY ) and converts
them to ISO format.
cvt_date.pl converts dates to and from any of ISO, US, or British formats. It is more
general than isoize_date.pl , but requires that you tell it what kind of input to expect
and what kind of output to produce.
Search WWH ::




Custom Search