rschooley
How do I store a Unix timestamp in Ecto?
I have a timestamp coming back from a 3rd party package. I have read these articles and don’t see how to just store this value in my db and have it come back as some sort of date type in elixir.
- is there an Ecto type that should just take in the timestamp and convert it?
- do I need to convert it manually in the changeset? (that would seem strange)
The codes:
##My Timestamp##
DateTime.from_unix 1518960668
Yeah this is cool
##using :utc_datetime##
** (FunctionClauseError) no function clause matching in Ecto.Type.cast_naive_datetime/1
The following arguments were given to Ecto.Type.cast_naive_datetime/1:
# 1
1518960668
Attempted function clauses (showing 4 out of 4):
defp cast_naive_datetime(binary) when is_binary(binary)
defp cast_naive_datetime(%{"year" => empty, "month" => empty, "day" => empty, "hour" => empty, "minute" => empty}) when empty === nil or empty === ""
defp cast_naive_datetime(%{year: empty, month: empty, day: empty, hour: empty, minute: empty}) when empty === nil or empty === ""
defp cast_naive_datetime(%{} = map)
code: token = token_fixture()
##using :time##
changeset errors:
...
errors: [expires_at: {"is invalid", [type: :time, validation: :cast]}]
Most Liked
OvermindDL1
For making your own type it’s documented at:
Specifically:
![]()
2
rschooley
OK, I have both versions working, and will paste here in case someone else has this same issue.
Short of it, cast does work:
defmodule UnixTimestamp do
@behaviour Ecto.Type
def type, do: :naive_datetime
def cast(timestamp) when is_integer(timestamp) do
case DateTime.from_unix(timestamp) do
{:ok, date} -> {:ok, DateTime.to_naive(date)}
{:error, reason} -> {:error, reason}
end
end
def cast(_), do: :error
def dump(value), do: Ecto.Type.dump(:naive_datetime, value)
def load(value), do: Ecto.Type.load(:naive_datetime, value)
end
and my migration uses naive_datetime so the field is like the timestamps.
Source:
At the bottomish of the second article it has an example of implementing the type.
2
Popular in Questions
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
can someone please explain to me how Enum.reduce works with maps
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
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
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
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
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function:
-dialyzer...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Other popular topics
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
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
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








