6

I'm building on the Actions On Google API using API.ai. In order to fulfil one of my actions I need to know the user's location and local time.

I have been able to successfully ask for and receive back the user's location.

However, I cannot see any way of getting the user's local time or timezone offset. Is there a way to do this?

(I realise that I could use the location to find out the offset but that would require another API call or use of another library, which I'd rather avoid.)

Thanks in advance.

-- Update --

As per Leon's answer below and as of 18th Jan 2017 this info isn't currently provided by the API. In order to get the required info I requested permission for the user's precise location, which returns lat and lng. I then used the GeoNames timezone API to get the timezone offset.

Prags
  • 2,457
  • 2
  • 21
  • 38
strttn
  • 2,320
  • 1
  • 18
  • 17
  • Would it be an option to use `@sys.date-time` as an Entity in the request? – Tim Büthe Feb 08 '17 at 21:14
  • Mmm, that could be an option to get it but as I understand it the user would first have to be encouraged to say a date and time that would be matched by the entity. I don't think the entity slot would be filled without this... Unless you know differently? – strttn Feb 09 '17 at 13:49

2 Answers2

3

Whilst the timezone isn't included natively yet to my knowledge. You can use location permission alongside libraries to get the local time dynamically.

The libraries I have used here are momentTz and timezone. The latter gets the timezone interpolated from lat/long supplied by location permission. The resolved timezone is then used to convert the standard time in UTC which momentTz provides natively to the local time sought.

const startTz = momentTz(); //Returns standard UTC time, independent of locality
const timestamp = 1402629305; // Any random timestamp will work, it wouldn't unnecessarily impede your results 

timezone.data(latitudeValue, longitudeValue, timestamp, function(err, tz) {
  if (err) {
    console.log(err);
  }

var zoneHolder = tz.raw_response.timeZoneId; //Appropriate timezone is returned here based on latitudeValue and longitudeValue passed
const localTime = startTz.tz(zoneHolder).format('LLL'); //local time which you seek;
});
Ade
  • 363
  • 3
  • 13
  • Thanks for the answer. Looks similar to what I ended up doing except that I do the timzone interpolation via the Geonames API. Does the timezone library you reference do the interpolation offline? Or does it also require a call out to a 3rd party API? Can you please add a link to the timezone library as from a quick Google search, I'm not sure which you are using. – strttn Apr 03 '18 at 12:54
  • You are welcome. I am not certain the interpolation is offline..., In my case though, I am running other requests online simultaneously whenever I utilise the call, so it might be difficult to exactly tell. See the link here: (https://npmjs.com/package/node-google-timezone) – Ade Apr 03 '18 at 22:50
0

The device's timezone isn't provided to the action yet.

Leon Nicholls
  • 4,623
  • 2
  • 16
  • 17
  • 1
    Thanks for answering, Leon. It seems to me that it'd be a useful and not particularly privacy-invasive piece of info to provide so hopefully it'll be provided soon and I'll be able to add an addendum here. In the meantime, I guess that is the answer :) – strttn Jan 18 '17 at 19:12