wlminimal
How to compare time in elixir
Hi!
I want to make a schedule a job for sending emails or texting messages.
And I found rihanna.
But before do that, I need a more understanding of DateTime..
I get an datetime input from customer in this format
date = “2019-09-25”
time = “16:00”
- How can I format this input to DateTime format? I tried.
def build_datetime(date, time) do with {:ok, datetime, 0} <- DateTime.from_iso8601("#{date} #{time}:00"), do: datetime end
But got an error
{:error, :missing_offset}
- I want to check user’s datetime is in future( 30 mins future from now on). So I would do this..
def schedule_datetime_is_future?(schedule_datetime) do
DateTime.diff(schedule_datetime, DateTime.utc_now) > 30 * 60
end
- in rihanna docs says
Rihanna.schedule(
{IO, :puts, ["Hello"]},
at: DateTime.from_naive!(~N[2018-07-01 12:00:00], "Etc/UTC")
)
So before I have to compare datetime, should I convert datetime to UTC datetime?
It works in different timezone?
Marked As Solved
sribe
This is a classic example of the shittiness of web browsers and Javascript library design. There has been no API for this. There are ways to get at it in some browsers, using the internationalization API. Your best bet is probably to use the moment.js timezone functions. Alternatively you can do what a lot of web-based apps do: ask the user to select their time zone in preferences.
Also Liked
ConnorRigby
I work on a project that heavily uses time/scheduling. I highly suggest only store UTC time in your application database, and convert it client side.
sribe
That is not sufficient. You need to know their time zone, because UTC offset at this moment in time does not tell you what their UTC offset would be at some other moment in time.
apr
You need to specify the timezone or add time offset from UTC. So you can do one of the following:
iex(1)> date = "2019-09-25"
"2019-09-25"
iex(2)> time = "16:00"
"16:00"
iex(3)> DateTime.from_iso8601("#{date} #{time}:00Z")
{:ok, #DateTime<2019-09-25 16:00:00Z>, 0}
iex(4)> DateTime.from_iso8601("#{date} #{time}:00+00:00")
{:ok, #DateTime<2019-09-25 16:00:00Z>, 0}
In the first example, you specify the timezone is UTC via Z and in the second example, you specify the offset from the UTC timezone. More info here.
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
- #code-sync
- #javascript
- #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








