From bb5a2c1e0ef6ea063a32f02e91fbe3006a3eb3ec Mon Sep 17 00:00:00 2001 From: Caine Tighe Date: Thu, 24 Dec 2015 10:37:27 -0500 Subject: [PATCH 1/5] Time: Improve Generic Time handling --- lib/DDG/Spice/Time.pm | 5 ++++- share/spice/time/time.js | 35 +++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/DDG/Spice/Time.pm b/lib/DDG/Spice/Time.pm index 601badbc36..8b75fc2284 100644 --- a/lib/DDG/Spice/Time.pm +++ b/lib/DDG/Spice/Time.pm @@ -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; diff --git a/share/spice/time/time.js b/share/spice/time/time.js index 67660e3f98..0936895271 100644 --- a/share/spice/time/time.js +++ b/share/spice/time/time.js @@ -6,21 +6,36 @@ 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(','), + isGeneric = /\/generic\//.test(source), + callParameters = decodeURIComponent(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; + 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; + } } } @@ -50,7 +65,11 @@ day: dateObj.getDate(), monthName: months[dateObj.getMonth()], year: dateObj.getFullYear(), - placeName: chosen.geo.state ? (chosen.geo.name + ", " + chosen.geo.state) : chosen.geo.name, + placeName: chosen.overridePlaceName ? + chosen.overridePlaceName : + chosen.geo.state ? + (chosen.geo.name + ", " + chosen.geo.state) : + chosen.geo.name, offset: chosen.time.timezone.offset.replace(/0|:/g, ""), zone: chosen.time.timezone.zonename, country: chosen.geo.country.name From 2a30d1061bfff809827031c6abd34e0ad874414d Mon Sep 17 00:00:00 2001 From: Caine Tighe Date: Thu, 24 Dec 2015 12:14:46 -0500 Subject: [PATCH 2/5] share/spice/time/time.js: use decodeURIComponentSafe. --- share/spice/time/time.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/share/spice/time/time.js b/share/spice/time/time.js index 0936895271..86174ae96d 100644 --- a/share/spice/time/time.js +++ b/share/spice/time/time.js @@ -12,9 +12,9 @@ // 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 = decodeURIComponent(source).split('/'), + callParameters = decodeURIComponentSafe(source).split('/'), chosen; if (isGeneric && callParameters.length === 7) { From 826533382faa8151b92afab3c263411ad4946fc1 Mon Sep 17 00:00:00 2001 From: Caine Tighe Date: Thu, 24 Dec 2015 12:23:39 -0500 Subject: [PATCH 3/5] share/spice/time/time.js: Add comment for generic time support. --- share/spice/time/time.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/share/spice/time/time.js b/share/spice/time/time.js index 86174ae96d..df34e27e43 100644 --- a/share/spice/time/time.js +++ b/share/spice/time/time.js @@ -17,6 +17,32 @@ callParameters = decodeURIComponentSafe(source).split('/'), chosen; + // 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]; From d00972d542a6e1211867427882fa6a69bc21db79 Mon Sep 17 00:00:00 2001 From: Caine Tighe Date: Thu, 24 Dec 2015 12:27:05 -0500 Subject: [PATCH 4/5] share/spice/time/time.js: Simplify placeName ternary. --- share/spice/time/time.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/share/spice/time/time.js b/share/spice/time/time.js index df34e27e43..83695316ac 100644 --- a/share/spice/time/time.js +++ b/share/spice/time/time.js @@ -85,17 +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.overridePlaceName ? - chosen.overridePlaceName : - 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 From bd690052296816314a4944a4f15e89e810abf89c Mon Sep 17 00:00:00 2001 From: Caine Tighe Date: Thu, 24 Dec 2015 12:31:07 -0500 Subject: [PATCH 5/5] t/Time.t: update tests for new URI struct. --- t/Time.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/Time.t b/t/Time.t index b50178c712..fabe1154b2 100644 --- a/t/Time.t +++ b/t/Time.t @@ -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' );