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

qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
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

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement