SunInfo: Adding parsing for arc seconds

master
Rob Emery 2015-01-08 01:46:04 +00:00
parent 6b438acd47
commit 3bbb179ef2
2 changed files with 28 additions and 11 deletions

View File

@ -6,7 +6,7 @@ with 'DDG::GoodieRole::Dates';
with 'DDG::GoodieRole::ImageLoader';
use DateTime::Event::Sunrise;
use Data::Dump qw(dump);
zci answer_type => "sun_info";
zci is_cached => 0;
@ -40,23 +40,38 @@ my $lat_lon_regex = qr/[\+\-]?[0-9]+(?:
(?:[0-9]{1,2}')?
(?:[0-9]{1,2}(?:''|"))?
)
)/x;
)?/x;
sub parse_arc {
my ($arc_string) = @_;
return unless $arc_string =~ qr/
(?<sign>[\+\-])?(?<deg>[0-9]+)(?:
((?<dec_deg>\.[0-9]+)[°]?)
|(?:°?
(?:(?<min>[0-9]{1,2})')?
(?:(?<sec>[0-9]{1,2})(?:''|"))?
)
)?(?<dir>[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 // '';
$remainder =~ s/\?//g; # Strip question marks.
return unless $remainder =~ qr/^
(?:at\s
(?<lat>$lat_lon_regex)[NS]\s
(?<lon>$lat_lon_regex)[EW]\s
(?<lat>$lat_lon_regex[NS])\s
(?<lon>$lat_lon_regex[EW])\s?
)?
(?:on|for)?\s?
(?<when>$datestring_regex)?
$/xi;
# print STDERR $remainder.$/.dump($+).$/;
# die;
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.
@ -69,7 +84,9 @@ handle remainder => sub {
}
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'});
my $sun_at_loc = DateTime::Event::Sunrise->new(
longitude => $lon,
latitude => $lat,

View File

@ -30,10 +30,10 @@ ddg_goodie_test(
'sunset on 2015-01-01' => test_zci(@exact),
'what time is sunrise on 2015-01-01?' => test_zci(@exact),
'January 1st, 2015 sunrise' => test_zci(@exact),
# q{sunrise at 39°57'N 75°10'W} => test_zci(@exact),
# 'sunset at 53N 10E' => test_zci(@exact),
# 'sunset at 53S 15W' => test_zci(@exact),
# 'sunset at 53N 10E on 2014-01-01' => test_zci(@exact),
q{sunrise at 39°57'N 75°10'W} => test_zci(""),
'sunset at 53N 10E' => test_zci(""),
'sunset at 53S 15W' => test_zci(""),
'sunset at 53N 10E on 2014-01-01' => test_zci(""),
'sunset for philly' => undef,
'sunrise on mars' => undef,
'sunset boulevard' => undef,