shahryarjb

shahryarjb

How to convert Date to DateTime which Ecto needs

Hello, I get Persian date from my client and convert it to normal calendar like this:

  def jalali_string_to_miladi_english_number(persian_string_time_number) do
    [yy, mm, dd] = String.split(persian_string_time_number, "/")
    {:ok, jalaali_date} = Date.new(Numero.normalize_as_number!(yy), Numero.normalize_as_number!(mm), Numero.normalize_as_number!(dd), Jalaali.Calendar)
    {:ok, iso_date} = Date.convert(jalaali_date, Calendar.ISO)
    iso_date
  end

my output is:

~D[2019-05-21]

but I can’t save this in my database and have this error:

value `~D[2019-05-21]` for `TransactionSchema.purchase_time` in `insert` does not match type :utc_datetime

how can convert it to utc_datetime can be saved in db with ecto like this DateTime.utc_now ?

Thanks

Most Liked

davidalencar

davidalencar

  def convert_date_to_datetime(%DateTime{} = date), do: date
  def convert_date_to_datetime(%Date{} = date) do
    date
    |> Date.to_gregorian_days()
    |> Kernel.*(86400)
    |> Kernel.+(86399)
    |> DateTime.from_gregorian_seconds()
  end
peerreynders

peerreynders

value `~D[2019-05-21]` for `TransactionSchema.purchase_time` in `insert` does not match type :utc_datetime

Your processing suggests that you only want the date.

Your schema type and name suggests that you want the time as well.

If you only want the date then have a purchase_date of type :date rather than :utc_datetime.

If you want the time, then record the full date, time, and timezone.

Using a datetime type to store a date will only lead to confusion and bugs in the long run - this is especially true when timezones are involved. For example a thoughtless shift in timezone:

iex(1)> DateTime.from_iso8601("2019-05-21 00:00:00Z")
{:ok, #DateTime<2019-05-21 00:00:00Z>, 0}
iex(2)> DateTime.from_iso8601("2019-05-20 23:00:00-01:00")
{:ok, #DateTime<2019-05-21 00:00:00Z>, -3600}
iex(3)>

can lead to a different date for the same “point in time”.

peerreynders

peerreynders

Given:

iex(1)> {:ok, datetime, offset} = DateTime.from_iso8601("2019-05-23 00:00:00+03:30")
{:ok, #DateTime<2019-05-22 20:30:00Z>, 12600}

“2019-05-22 20:30:00Z” is the time that is stored in the database.

:utc_datetime is a datetime which is implicitly understood to be with reference to UTC. There is no actual timezone being stored in :utc_datetime. So when it’s retrieved you would have to DateTime.shift_zone/3 the time to the IRST timezone - which only works if you have a custom Time zone database configured. Only after the shift will it convert to the correct local date.

Otherwise:

iex(1)> {:ok, persisted_time, offset} = DateTime.from_iso8601("2019-05-22 20:30:00Z")
{:ok, #DateTime<2019-05-22 20:30:00Z>, 0}
iex(2)> DateTime.to_string(persisted_time)
"2019-05-22 20:30:00Z"
iex(3)> DateTime.to_date(persisted_time)
~D[2019-05-22]
iex(4)> 

I expect that the Date type (:date) is understood to represent a local date.

Where Next?

Popular in Questions Top

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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

We're in Beta

About us Mission Statement