From 903c7cf6102a4e7356a9f72e1f247a1b0f5d0c2c Mon Sep 17 00:00:00 2001 From: Rob Emery Date: Thu, 8 Jan 2015 01:50:15 +0000 Subject: [PATCH] SunInfo: Moving handling around --- lib/DDG/Goodie/SunInfo.pm | 46 +++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/DDG/Goodie/SunInfo.pm b/lib/DDG/Goodie/SunInfo.pm index 2dc25e5ef..c30a21a79 100644 --- a/lib/DDG/Goodie/SunInfo.pm +++ b/lib/DDG/Goodie/SunInfo.pm @@ -42,23 +42,6 @@ my $lat_lon_regex = qr/[\+\-]?[0-9]+(?: ) )?/x; -sub parse_arc { - my ($arc_string) = @_; - return unless $arc_string =~ qr/ - (?[\+\-])?(?[0-9]+)(?: - ((?\.[0-9]+)[°]?) - |(?:°? - (?:(?[0-9]{1,2})')? - (?:(?[0-9]{1,2})(?:''|"))? - ) - )?(?[NSEW])/x; - my $decimal_degrees = $+{'deg'}; - $decimal_degrees += $+{'dec_deg'} if $+{'dec_deg'}; - $decimal_degrees += $+{'min'}/60 if $+{'min'}; - $decimal_degrees += $+{'sec'}/3600 if $+{'sec'}; - $decimal_degrees *= -1 if $+{'sign'} && $+{'sign'} eq '-' || $+{'dir'} =~ /[SW]/; - return $decimal_degrees; -} handle remainder => sub { my $remainder = shift // ''; @@ -75,15 +58,12 @@ handle remainder => sub { my ($lat, $lon, $tz) = ($loc->latitude, $loc->longitude, $loc->time_zone); my $where = where_string(); return unless (($lat || $lon) && $tz && $where); # We'll need a real location and time zone. - my $dt; - if($+{'when'}) { - $dt = parse_datestring_to_date($+{'when'}); - } - else { - $dt = DateTime->now; - } + my $dt = DateTime->now;; + $dt = parse_datestring_to_date($+{'when'}) if($+{'when'}); + return unless $dt; # Also going to need to know which day. $dt->set_time_zone($tz); + $lon = parse_arc($+{'lon'}) if ($+{'lon'}); $lat = parse_arc($+{'lat'}) if ($+{'lat'}); @@ -119,6 +99,24 @@ sub where_string { return join(', ', @where_bits); } +sub parse_arc { + my ($arc_string) = @_; + return unless $arc_string =~ qr/ + (?[\+\-])?(?[0-9]+)(?: + ((?\.[0-9]+)[°]?) + |(?:°? + (?:(?[0-9]{1,2})')? + (?:(?[0-9]{1,2})(?:''|"))? + ) + )?(?[NSEW])/x; + my $decimal_degrees = $+{'deg'}; + $decimal_degrees += $+{'dec_deg'} if $+{'dec_deg'}; + $decimal_degrees += $+{'min'}/60 if $+{'min'}; + $decimal_degrees += $+{'sec'}/3600 if $+{'sec'}; + $decimal_degrees *= -1 if $+{'sign'} && $+{'sign'} eq '-' || $+{'dir'} =~ /[SW]/; + return $decimal_degrees; +} + sub pretty_output { my ($where, $when, $rise, $set) = @_;