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

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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

We're in Beta

About us Mission Statement