TimeZoneConverter: move to named captures.

This makes it easier to decipher what matches what and how the defaults
come to be set, in my opinion.
master
Matt Miller 2014-09-12 10:12:00 -04:00
parent ae0ae15115
commit 8e22daee13
1 changed files with 17 additions and 21 deletions

View File

@ -96,36 +96,31 @@ sub to_time {
sprintf "%i:%02.0f$seconds_format$pm", $hours, $minutes, $seconds; sprintf "%i:%02.0f$seconds_format$pm", $hours, $minutes, $seconds;
} }
handle query => sub { my $timezone_re = qr/(?:\w+(?:\s*[+-]0*[0-9]{1,5}(?::[0-5][0-9])?)?)?/;
my $timezone = qr/(\w+(?:\s*[+-]0*[0-9]{1,5}(?::[0-5][0-9])?)?)?/;
my (
# Time
$hour, $minutes, $seconds, $american, $pm,
# Timezones handle query => sub {
$input_timezone, $output_timezone, my $query = $_;
) $query =~ m{
= uc =~ m{
\A \s* \A \s*
# Time # Time
# Hours # Hours
([01]?[0-9] | 2[0-3]) (?<h>[01]?[0-9] | 2[0-3])
(?: (?:
# Minutes # Minutes
:([0-5] [0-9]) :(?<m>[0-5] [0-9])
(?: (?:
# Seconds # Seconds
:([0-5] [0-9]) :(?<s>[0-5] [0-9])
)? )?
)? )?
# Optional spaces between tokens # Optional spaces between tokens
\s* \s*
# AM/PM # AM/PM
((?:A|(P))\.?M\.?)? (?<american>(?:A|(?<pm>P))\.?M\.?)?
# Spaces between tokens # Spaces between tokens
\s* \b \s* \b
# Optional input timezone # Optional input timezone
$timezone (?<from_tz>$timezone_re)
# Spaces # Spaces
\s+ \s+
# in keywords # in keywords
@ -133,14 +128,15 @@ handle query => sub {
# Spaces # Spaces
\s+ \s+
# Output timezone # Output timezone
$timezone (?<to_tz>$timezone_re)
\s* \z \s* \z
}x or return; }ix or return;
$pm = $pm ? 12 : 0; my ($hours, $minutes, $seconds) = map { $_ // 0 } ($+{'h'}, $+{'m'}, $+{'s'});
$input_timezone //= 'UTC'; my $american = $+{'american'};
$minutes //= 0; my $pm = $+{'pm'} ? 12 : 0;
$seconds //= 0; my $input_timezone = $+{'from_tz'} || 'UTC';
my $output_timezone = $+{'to_tz'};
my $modifier = 0; my $modifier = 0;
for ( $input_timezone, $output_timezone ) { for ( $input_timezone, $output_timezone ) {
@ -161,7 +157,7 @@ handle query => sub {
s/:00\z//; s/:00\z//;
} }
my $input_time = $hour + $minutes / 60 + $seconds / 3600 + $pm; my $input_time = $hours + $minutes / 60 + $seconds / 3600 + $pm;
my $output_time = $input_time + $modifier; my $output_time = $input_time + $modifier;
for ( $input_time, $output_time ) { for ( $input_time, $output_time ) {
my $days = ""; my $days = "";