Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Timezones #4460

Closed
wants to merge 19 commits into from
Closed
62 changes: 27 additions & 35 deletions lib/DDG/Goodie/Timezonetime.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use DDG::Goodie;
use strict;
use warnings;

use YAML::XS qw/LoadFile/;

use DateTime;
use DateTime::TimeZone;

Expand All @@ -15,51 +17,41 @@ triggers start => ("what time in", "what time is it in", "time in");
triggers startend => ("time", "now time", "time now");

# Mapping short timezone names to one used by DateTime:Timezone module
my $timezoneMapping = {
"IST" => "Asia/Kolkata",
"EST" => "EST",
"UTC" => "UTC",
"GMT" => "GMT",
"BST" => "Europe/London",
"PST" => "PST8PDT",
"CST" => "CST6CDT"
};

my $timezones = join('|', keys(%$timezoneMapping));
my $timezoneMapping = LoadFile(share('abbreviations.yaml'));

handle remainder => sub {
my $query = $_;

my $daylightStatus = "";

my $timezone = uc($query);
my $mappedTimezone = $timezoneMapping->{$timezone} // 0;
return unless $mappedTimezone;

# Get time for desired timezone
my $tz = DateTime::TimeZone->new( name => $mappedTimezone );
my $dt = DateTime->now();
my $offset = $tz->offset_for_datetime($dt);
$dt->add(seconds => $offset);
my $time = $dt->hms(':');

# Check if timezone is in daylight saving or not
if ($tz->is_dst_for_datetime( $dt )) {
$daylightStatus = "$timezone is in daylight saving";
}
else {
$daylightStatus = "$timezone is not in daylight saving";
}
my $mappedTimezones = $timezoneMapping->{$timezone} // 0;
return unless $mappedTimezones;

# Get time for desired timezones
my $dt = DateTime->now(time_zone => 'UTC');
my @times = map {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be clearer as a for loop rather than a map, with the multiple statements within it's pretty difficult to read

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm also it might be worth building this outside of the handle so we can just just lookup the answer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rewrite it using a for loop with a push inside.

Yes, we can convert offsets only once for each timezone. I'll fix this too.

my $offset = DateTime::TimeZone->offset_as_seconds($_->{offset});
my $dt_clone = $dt->clone;
$dt_clone->add(seconds => $offset);
{ name => $_->{name},
time => $dt_clone->hms(':'),
offset => $_->{offset} };
} @{$mappedTimezones};

return "$time $timezone $daylightStatus",
return "times in $timezone",
structured_answer => {

meta => {
sourceName => 'timeanddate',
sourceUrl => 'https://www.timeanddate.com/time/zones/'
},
data => {
title => "$time $timezone",
subtitle => "$daylightStatus",
title => "Timezone $timezone",
list => \@times
},
templates => {
group => 'text',
group => 'list',
options => {
list_content => 'DDH.timezonetime.content'
}
}
};
};
Expand Down