sergio
How do you store your user's timezone information?
Curious how you store your user’s timezone information to be able to send them an email at 12PM local time for example.
Assuming I default a dropdown with getTimezoneOffset clientside and allow the user to pick their timezone, what would be the best way to store this information?
Ideally whatever I save should be easy to use in Timex functions to calculate “schedule this Oban worker at 12PM user’s local time.”
Curious what you’ve guys done for this kind of issue in the past.
I could save the offset like:
timezone_offset: 360 # UTC-6 hours
Then schedule the job for tomorrow at 12pm local time:
timezone_offset = user.timezone_offset # 360
schedule_time =
Timex.now()
|> Timex.shift(days: 1)
|> Timex.beginning_of_day()
|> Timex.shift(hours: 12)
|> Timex.shift(minutes: timezone_offset)
Oban.insert!(%Oban.Job{
worker: MyWorker,
scheduled_at: schedule_time
})
Any sharp edges I’m not considering before going this route?
Most Liked
tfwright
I store the timezone name (e.g. America/Chicago) in users.time_zone and use that when using Elixir DateTime functions, and they also work with postgres’s AT TIME ZONE in case you want to query users against a given time or datetime.
LostKobrakai
Timezone offsets are not timezones. It tells you almost nothing about the future. Sure the offset might be X today for the user, but how do you know the same still applies tomorrow?
You need the timezone, not just the offset, to do this stuff reliably. I’d also suggest to stick to elixir core APIs nowadays, as besides parsing they can do everything you need. There’s no need to attempt to build your own timezone handling.
tomorrow = "Europe/Berlin" |> DateTime.now!() |> Date.add(1)
DateTime.new(tomorrow, ~T[12:00:00], "Europe/Berlin")
# {:ok, #DateTime<2023-08-25 12:00:00+02:00 CEST Europe/Berlin>}
LostKobrakai
No, you need a library like tz or tzdata to bring in a database of timezones, but elixir itself brings all the necessary APIs to make use of those databases.
Popular in Discussions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








