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
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
Elixir provides DateTime.add since 1.8, you can add seconds directly like this:
DateTime.utc_now() |> DateTime.add(3600, :second)
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
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
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
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








