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: GitHub - bitwalker/timex: A complete date/time library for Elixir projects. · GitHub

> 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

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
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
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 47930 226
New

We're in Beta

About us Mission Statement