wlminimal

wlminimal

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?

First 7 of 7 Posts Switch mode

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.

wlminimal

wlminimal OP

I still don’t understand. :frowning:
Users select a date time in front end (it is their local time zone). Let’s say, they pick 2018-09-29 and time 16:00
How can I convert this time to their time zone? So I can schedule job at their time zone time.

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.

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

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.

wlminimal

wlminimal OP

That is a problem what I want to solve.

  1. user picks datetime from datetime picker ui.
  2. my app accepts it as string date - “2018-09-26”, time - “16:00”
  3. I need to find out users’ timezone info.
  4. convert it to utc time format with timezone info
  5. check if users’ datetime is in the future in utc time format
    6.if users’ datetime is in the future, make a schedule job.
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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement