silverdr

silverdr

DateTime.to_unix() vs System.os_time()

DateTime documentation contains the following sentence:

If you want to get the current time in Unix seconds, do not do DateTime.utc_now() |> DateTime.to_unix() . Simply call System.os_time(:second) instead.

Nice, but… System documentation says about os_time/0 and os_time/1 as follows:

This time may be adjusted forwards or backwards in time with no limitation and is not monotonic.

Now, I DO “want to get the current time in Unix seconds” (or microseconds) but I want it to be the current time in UTC, God forbid in one of the tons of useless “time zones”, nor with / without equally useless DST changes. So the question - how is doing System.os_time(:second) supposed to be a better equivalent of doing DateTime.utc_now() |> DateTime.to_unix()? Am I missing something here?

P. S. Yes, I do run my computers (including the one I write these words on) on UTC so I believe in such case I expect those to be equivalent, right? Still more than once I had to work on servers configured… well… differently and had to deal with code that never expected to be run “in different timezone”, so I am really curious what’s the case here

Marked As Solved

hauleth

hauleth

NTPd adjustments. Simply your machine may have invalid time set and on the next check the time will be shifted to match the result returned from the NTP server.

Also Liked

al2o3cr

al2o3cr

Based on the implementation of DateTime.utc_now/1:

I’d guess the documentation’s suggestion is for efficiency; no point in building an intermediate DateTime just to turn it right back into an integer.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement