GMaps Time Zone API flow

On the backend (phoenix app) I have a situation where I am creating Events with a start_date. Every event will have one Location known in advance, with geo (lat lng) coordinates. A concert or sports event for example.

On the client side, rather than ask the user for a time zone directly, I decided that it would be cool to extract that info using the location coords and Google Maps Time Zone API (https://developers.google.com/maps/documentation/timezone/start).

You pass the coordinates of a location (lat, lng) and a unix timestamp, and it returns the tz name, utc offset, dst offset.

Here is my idea for how it would work:

The user submitted start_date is a NaiveDateTime representing the local/wall time of the event. However the Google API expects the timestamp as unix, which is essentially UTC if I’m correct. So my thinking is that could send the unix current time to the Time Zone API to get the locations timezone. Then use a library like Calendar or Timex to apply this timezone with the naive datetime, to generate a correct DateTime.

It seems a little like going round the houses though :confused: . I did think I could query the location after it’s first created and save the timezone alongside it, but not sure if that would be a bad idea when dealing with tz data.

Any feedback on this flow would be greatly appreciated!

You have a problem here, namely that asking the user for wallclock time and then finding out the timezone based on that time as a Unix timestamp means that you already need the timezone to convert said wallclock time to Unix.

POSIX/UNIX time is not at all the same as UTC (although they are somewhat related). Unix time measures the time since 00:00:00 1 January 1970 (UTC) in seconds, while UTC uses the proleptic gregorian calendar, with the addition of leap seconds.

It seems to me that what you actually need, is something that converts local time to ‘server time’ (which might be UTC or any timezone offset based on that), so your application accurately knows when an event starts. The Google Maps Time Zone API endpoint isn’t going to help you here, as you actually need the inverse functionality (user picks location + wall clock time, API returns time in UTC or Unix).

1 Like

Yes you’re right about UTC and Unix differing. Thanks for the confirmation!

Could I not do something roughly along the lines of this (below) if I just used the Google API to give me the timezone name from the location, and then do the actual time manipulation locally:

tz = 
  DateTime.utc_now
  |> DateTime.to_unix
  |> GoogleAPI.get_timezone(location.lat, location.lng) # E.g "America/New_York"
  
dt = DateTime.from_naive(used_provided_date, tz) # or equivalent using Calendar/Timex
  
# Save in db at utc and tz, or equivilent

Thanks

The problem you’ll face when you do that, is that the user_provided_datetime might be at a moment where there are other daylight savings time rules than there are at the current time at that particular location, meaning that you’ll plan in the event an hour too early or late.

I follow. But isn’t that always going to be a problem for any system scheduling things into the future, before timezone changes are made?