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.

tty

tty

Add a bogus time to your Date to change it to DateTime. e.g. “2019-05-21 00:00:00Z”

shahryarjb

shahryarjb

Hmm…, I am confused, I need to save my user time zone I think, let me explain:
I have 2 user in the system Include (admin , user).

  1. admin can save a time that should Include a purchase_date, this field has to have (year, month, day, hour, min)
  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)
    {:ok, datetime, 0} = DateTime.from_iso8601("#{iso_date.year}-#{fix_month_and_day(iso_date.month)}-#{fix_month_and_day(iso_date.day)}T00:00:00Z")
    datetime
  end

In top code I have written I try to convert persian calendar to normal calendar like (2018), now I think I have a problem. where is my country Time zone (Iran/Tehran 3:30) , my country type is GMT ( UTC ) : (3:30 AM Tehran Time to Your Local Time Conversion -- TimeBie)

but when I test this it backs me (“Etc/UTC”)

{:ok, times, zone} = DateTime.from_iso8601("2019-05-23T23:50:07.123+03:30")
iex(13)> times.time_zone
"Etc/UTC"

I should change my table name or not , but am I right in this way ?

just one thing I should explain, if the time was stored in db I have no problem because I make it to my calendar but it is wrong when I want to create a datetime in db and write time like (13:24)

Where Next?

Popular in Questions Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New

We're in Beta

About us Mission Statement