From 8e22daee1353264cd15ab512628d560c0c54c58e Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Fri, 12 Sep 2014 10:12:00 -0400 Subject: [PATCH] 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. --- lib/DDG/Goodie/TimezoneConverter.pm | 38 +++++++++++++---------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/DDG/Goodie/TimezoneConverter.pm b/lib/DDG/Goodie/TimezoneConverter.pm index 932f628f6..1ec3ebf77 100644 --- a/lib/DDG/Goodie/TimezoneConverter.pm +++ b/lib/DDG/Goodie/TimezoneConverter.pm @@ -96,36 +96,31 @@ sub to_time { sprintf "%i:%02.0f$seconds_format$pm", $hours, $minutes, $seconds; } -handle query => sub { - my $timezone = qr/(\w+(?:\s*[+-]0*[0-9]{1,5}(?::[0-5][0-9])?)?)?/; - my ( - # Time - $hour, $minutes, $seconds, $american, $pm, +my $timezone_re = qr/(?:\w+(?:\s*[+-]0*[0-9]{1,5}(?::[0-5][0-9])?)?)?/; - # Timezones - $input_timezone, $output_timezone, - ) - = uc =~ m{ +handle query => sub { + my $query = $_; + $query =~ m{ \A \s* # Time # Hours - ([01]?[0-9] | 2[0-3]) + (?[01]?[0-9] | 2[0-3]) (?: # Minutes - :([0-5] [0-9]) + :(?[0-5] [0-9]) (?: # Seconds - :([0-5] [0-9]) + :(?[0-5] [0-9]) )? )? # Optional spaces between tokens \s* # AM/PM - ((?:A|(P))\.?M\.?)? + (?(?:A|(?P))\.?M\.?)? # Spaces between tokens \s* \b # Optional input timezone - $timezone + (?$timezone_re) # Spaces \s+ # in keywords @@ -133,14 +128,15 @@ handle query => sub { # Spaces \s+ # Output timezone - $timezone + (?$timezone_re) \s* \z - }x or return; + }ix or return; - $pm = $pm ? 12 : 0; - $input_timezone //= 'UTC'; - $minutes //= 0; - $seconds //= 0; + my ($hours, $minutes, $seconds) = map { $_ // 0 } ($+{'h'}, $+{'m'}, $+{'s'}); + my $american = $+{'american'}; + my $pm = $+{'pm'} ? 12 : 0; + my $input_timezone = $+{'from_tz'} || 'UTC'; + my $output_timezone = $+{'to_tz'}; my $modifier = 0; for ( $input_timezone, $output_timezone ) { @@ -161,7 +157,7 @@ handle query => sub { 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; for ( $input_time, $output_time ) { my $days = "";