sergio

sergio

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?

First 7 of 7 Posts Switch mode

kokolegorille

kokolegorille

tz can do it for You…

You provide a naive_datetime (virtual attribute), a timezone, and tz can handle offset and utc itself.

UPDATE: Ouups, I think it’s tzdata that does this

In fact, it’s tz_datetime, sorry

LostKobrakai

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>}
sergio

sergio OP

That’s interesting and makes sense. Are you saying Elixirs core will keep up to date with time zone changes so I should rely on them?

LostKobrakai

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.

LostKobrakai

LostKobrakai

To get a little more concrete than my last post:

  • Timex.now might already return a different date than the current date of the user (australia is long in the new year before midnight in the usa)
  • Shifting a day from an already potentially incorrect base won’t make things more correct.
    • Do you know if this is actually adding a day to the date or plainly adding 24h? That’s quite the difference with timezones in play.
  • Shifting the hours expects that the mapping between hours in UTC to the users timezone are fixed. That’s not the case most obviously around DST switches.
  • Manually applying any offset should probably be considered a problem on its own. Always let a library do that for you, it probably knows better where to apply which offset.
tfwright

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.

10
Post #6
dimitarvp

dimitarvp

Ha, didn’t know the part about Postgres, thank you.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement