wlminimal

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”

  1. 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}

  1. 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
  1. 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

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

ConnorRigby

Nerves Core Team

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

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

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.

apr

apr

You need to know what the utc_offset is for their given timezone. For instance, if a user is from San Francisco, they would be in the Pacific Time Zone, which has an offset of -08:00 from UTC. Then, the correct DateTime would be:

{_, time_in_utc, offset} = DateTime.from_iso8601("#{date} #{time}:00-08:00").

You can then store all input times in UTC, schedule using UTC time and convert it to the corresponding time in their time zone using offset (for instance, to display in the UI.)

Where Next?

Popular in Questions Top

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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
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
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement