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

Commit

Permalink
Merge pull request #2357 from duckduckgo/nilnilnil/better-generic-time
Browse files Browse the repository at this point in the history
Time: Improve Generic Time handling
  • Loading branch information
Jag Talon committed Dec 25, 2015
2 parents 1c14504 + bd69005 commit 3519b43
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
5 changes: 4 additions & 1 deletion lib/DDG/Spice/Time.pm
Expand Up @@ -31,7 +31,10 @@ handle query_lc => sub {
my $q_loc = trim $+{loc};

# if no location is given, current user location is returned
return join ' ', (lc $loc->city, lc $loc->country_name) unless $q_loc;
my $tz_string = $loc->time_zone;
$tz_string =~ s/[\/_]/ /g;
my $location = join(', ', ($loc->city, $loc->region_name));
return ($tz_string, 'generic', $location) unless $q_loc;

$q_loc =~ s/,//g;

Expand Down
65 changes: 56 additions & 9 deletions share/spice/time/time.js
Expand Up @@ -6,21 +6,62 @@
return Spice.failed('time');
}


var script = $('[src*="/js/spice/time/"]')[0],
source = $(script).attr("src"),
// Query is normalized as we'll normalize the generated strings.
// if we have a comma separated query it is in the form:
// "town, state, country" but state is optional
query = decodeURIComponent(source.match(/time\/([^\/]+)/)[1]).toLowerCase().split(','),
query = decodeURIComponentSafe(source.match(/time\/([^\/]+)/)[1]).toLowerCase().split(','),
isGeneric = /\/generic\//.test(source),
callParameters = decodeURIComponentSafe(source).split('/'),
chosen;

for(var i = 0; i < api.locations.length; i++) {
// query[0] = state
// query[len-1] = country
if(DDG.stringsRelevant(query[0], api.locations[i].geo.name) &&
DDG.stringsRelevant(query[query.length-1], api.locations[i].geo.country.name)) {
chosen = api.locations[i];
break;
// Generic queries are when the user is looking
// for local time, but never specified a location
// in the query string. Since the API doesn't
// have full coverage, we use user TimeZone info
// like America/New_York to get time for places
// in the EST time zone. We then need to replace
// New York (the looked up time), with the users
// location.
//
// This information is passed through the DDH
// system through the API call URL using / as a
// delimeter.
// A generic time request from Phoenixville, PA
// looks like:
//
// /js/spice/time/America%20New%20York/generic/Phoenixville%2C%20Pennsylvania
//
// * We use America New York, to do a relevancy
// check on the data we get back from the API.
// * We use 'generic' as a means to know it's
// a generic time query like 'current time'.
// * We use Phoenixville, Pennsylvania, to make
// sure we show the user the location they are in
// That way people looking for the current time
// in Phoenxivilla, PA see Phoenixville PA instead
// of New York, NY.
if (isGeneric && callParameters.length === 7) {
var lookupLocation = callParameters[4],
displayLocation = callParameters[6];

if (api.locations.length && api.locations[0].geo) {
if (lookupLocation.indexOf(api.locations[0].geo.name) > -1) {
chosen = api.locations[0];
chosen.overridePlaceName = displayLocation;
}
}
} else {
for(var i = 0; i < api.locations.length; i++) {
// query[0] = state
// query[len-1] = country
if(DDG.stringsRelevant(query[0], api.locations[i].geo.name) &&
DDG.stringsRelevant(query[query.length-1], api.locations[i].geo.country.name)) {
chosen = api.locations[i];
break;
}
}
}

Expand All @@ -44,13 +85,19 @@
};
}

// holds display location
var placeName = chosen.geo.state ? (chosen.geo.name + ", " + chosen.geo.state) : chosen.geo.name;
if (chosen.overridePlaceName) {
placeName = chosen.overridePlaceName;
}

var dateTime = {
time: toPrettyTime(dateObj),
dayName: days[dateObj.getDay()],
day: dateObj.getDate(),
monthName: months[dateObj.getMonth()],
year: dateObj.getFullYear(),
placeName: chosen.geo.state ? (chosen.geo.name + ", " + chosen.geo.state) : chosen.geo.name,
placeName: placeName,
offset: chosen.time.timezone.offset.replace(/0|:/g, ""),
zone: chosen.time.timezone.zonename,
country: chosen.geo.country.name
Expand Down
2 changes: 1 addition & 1 deletion t/Time.t
Expand Up @@ -14,7 +14,7 @@ my @kingston_town = (
);

my @phoenixville = (
'/js/spice/time/phoenixville%20united%20states',
'/js/spice/time/America%20New%20York/generic/Phoenixville%2C%20Pennsylvania',
call_type => 'include',
caller => 'DDG::Spice::Time'
);
Expand Down

0 comments on commit 3519b43

Please sign in to comment.