peoj

peoj

How can I determine the current timezone of the local system (e.g. Etc/UTC)?

This approach would work for me, but seems like I must be missing a built-in API of some sort!

Showing Posts 1 to 10

outlog

outlog

of a client? or the server? (btw imho your server should always run in utc)

peoj

peoj OP

The local system where the elixir app is currently being run, so "server’ I guess.

Agreed but I can’t make that assumption.

outlog

outlog

app logic can/should be in a way that the server timezone is irrelevant.. aka always Start with DateTime.utc_now() or similar in the logic and go from there..

anyways, maybe something like

iex(20)> :calendar.time_difference(:calendar.universal_time, :calendar.local_time  )
{0, {2, 0, 0}}

(I’m utc+2 currently)

pretty sure elixir doesn’t expose it.. since it’s a very bad path to venture down of.. time is hard enough as it is..

peoj

peoj OP

Thanks for the help, that works nicely for the offset.

I understand the difficulties inherent in timezones in some applications. For the purposes of my app I still need to know the current timezone and offset of the system.

lud

lud

As a workaround you could call System.cmd("date", ["+%Z"]) or compare the output of System.cmd("date", ["+%H:%M:%S"]) with DateTime.utc_now() but that does not seem very reliable.

ericgray

ericgray

Why not just use the function in the Stackoverflow article

def get_timezone() do
  {zone, result} = System.cmd("date", ["+%Z"])
  if result == 0, do: String.trim(zone)
end

If you compare it with DateTime.utc_now() you would need to get the offset.

lud

lud

Well it seems that I read the topic not from the beginning. I completely overlooked the first post, sorry.

That being said, "+%Z" gives "CEST" on my machine, that is not very useful as it still requires to have a timezone database of some sort. And if you have one, I bet there is a function in the library that would give you your current timezone anyway.

ericgray

ericgray

Right didn’t realize he needed the offset. The only thing that comes to mind is Tzdata database. Elixir School has a nice article on how to use it. The only problem with this approach is you have to pass in the name of the zone, i.e. “America/Chicago”.

If there was some kind of way you could use System.cmd("date", ["+%Z"] to get the time zone and pass that in I think it would work.

billposters

billposters

The host OS, and packages available are going to determine what TZ info you can get from the system directly. For example, on Debian you can cat /etc/timezone and get Europe/London but it’s not guaranteed that the timezone packages will be installed on the host.

If you don’t control the host (and packages installed) then realistically you’re only going to be able to resolve the offset, not the named TZ e.g. “Etc/0” not “Europe/London” because there are more named TZ aliases in an offset, meaning it’s not a 1-1 relationship.

cmo

cmo

Use Timex’s Timezone.local()?

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
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
Stef-Verniers
Hello I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews