camcaine

camcaine

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 (Get Started  |  Time Zone API  |  Google for Developers).

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!

Showing Posts 1 to 4

Qqwy

Qqwy

TypeCheck Core Team

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).

camcaine

camcaine OP

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

Qqwy

Qqwy

TypeCheck Core Team

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.

camcaine

camcaine OP

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?

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews