kostonstyle

kostonstyle

Create time with one hour plus

Hi all

I want to have a unix time, from the current time plus 1 hour.

DateTime.now + 1 hour

How to get it in elixir?

Thanks

Marked As Solved

h4cc

h4cc

Have a look at the library timex: https://github.com/bitwalker/timex

> Timex.shift(datetime, hours: 2, minutes: 13)

Also Liked

m31271n

m31271n

Elixir provides DateTime.add since 1.8, you can add seconds directly like this:

DateTime.utc_now() |> DateTime.add(3600, :second)
14
Post #5
aseigo

aseigo

Just to add some random thoughts: this specific thing can be done easily enough without a dependency if your needs are very simple ->

DateTime.to_unix(DateTime.utc_now()) + 60*60

… but yeah, just use Timex :wink: Dates and times are amazingly difficult to get Right™ when you are dealing with timezones, quirks like daylight savings, need better-than-second resolution etc. If you need a simple timestamp in a single arbitrary timezone where a second here or there doesn’t particularly matter, not a big deal. If you need anything remotely more than that, use the libraries available.

adamu

adamu

It’s worth mentioning that adding 3600 seconds might not get you what you want.

iex(1)> DateTime.new!(~D[2022-10-30], ~T[00:00:00], "Europe/London") |> DateTime.add(3600)
#DateTime<2022-10-30 01:00:00+01:00 BST Europe/London>
iex(2)> DateTime.new!(~D[2022-10-30], ~T[00:00:00], "Europe/London") |> DateTime.add(3600 * 2)
#DateTime<2022-10-30 01:00:00+00:00 GMT Europe/London>

In this case, 01:00 happens twice due to daylight savings.

If you were just trying to schedule something to happen one hour later, that’s fine, but if you wanted the semantic meaning of the next hour on the clock, you need to choose which one, and Elixir will tell you so:

iex(3)> DateTime.new(~D[2022-10-30], ~T[01:00:00], "Europe/London")
{:ambiguous, #DateTime<2022-10-30 01:00:00+01:00 BST Europe/London>,
 #DateTime<2022-10-30 01:00:00+00:00 GMT Europe/London>}

Even when calculating with seconds, there are leap seconds to worry about, which the default calendar does not handle. For example, there was a leap second on 2016-12-31 23:59:60Z but Calendar.ISO does not know about it:

iex(4)> ~U[2016-12-31 23:59:60Z]
** (ArgumentError) cannot parse "2016-12-31 23:59:60Z" as UTC DateTime for Calendar.ISO, reason: :invalid_time
mjs2600

mjs2600

:thumbsup: Timex is the answer here. If you want the Unix time from an Elixir DateTime, you can use DateTime.to_unix/1.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement