Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get requester's local time (or offset) from Actions On Google?

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.

like image 959
strttn Avatar asked Jan 18 '17 11:01

strttn


1 Answers

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;
});
like image 74
Ade Avatar answered Oct 09 '22 03:10

Ade