rschooley
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]}]
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 7 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
Yep, never compare Date/Time/DateTime/NaiveDateTime or any struct at all with
==equality testing. If they are designed to be comparable then either they should tell you that==works or they expose something like acomparefunction on their modules (which Date/Time/DateTime/NaiveDateTime do), so you need to compare Date/Time/DateTime/NaiveDateTime with the appropriatecomparecall on the appropriate module type.rschooley
OK, the last part here is still troublesome if someone has a solution.
My tests use
==to check the existing vs the fixture (generated code).I am passing in
~N[2018-02-18 13:31:08])thanks to the above custom type.But postgres has more precision than that, and returns
~N[2018-02-18 13:31:08.000000]So the tests fail on the 0s in the milli and microsecond parts.
Reading the docs on NaiveDateTime
==uses struct comparison, so the values are not strictly equivalent. Example:I am doing something awful, and only right in my one use case as I limit precision, but it works and I can’t spend more time on a single date field.
If there is a better way to solve this that would be awesome, but thanks to @OvermindDL1 for getting me this far.
I also have to wonder if I am off the beaten path here. Does no one else do this or do they convert values at a higher level in their apps?
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:
and my migration uses
naive_datetimeso the field is like the timestamps.Source:
At the bottomish of the second article it has an example of implementing the type.
rschooley
Right, I read those, but, “Ecto native type” doesn’t help me. It’s a DateTime, but
from_datewants an Ecto.Date not a DateTime. I guess cast, but that only takes binaries, maps, tuples, and Ecto.Dates.https://hexdocs.pm/ecto/Ecto.DateTime.html#from_date/1
OvermindDL1
For making your own type it’s documented at:
Specifically:
rschooley
Thanks for the reply.
I tried this both ways with varying degrees of success / unsuccess. I’ll leave what I did here in case it helps someone else / is worth figuring out.
#custom cast##
This way worked, except the assert on the original incoming data fails as the version from pg has a different precision.
#DateTime<2018-02-18 13:31:08.000000Zvs
#DateTime<2018-02-18 13:31:08Z#custom type##
I had less success this way, as I have no idea what
dumpwants in order to store the value correctly.OvermindDL1
You just need to
DateTime.from_unix!(or no!if you want to handle invalid input) before passing it to the database. Theutc_datetimetype is entirely the appropriate type to use but it needs a (Naive)DateTime structure.You can make a custom field type too (name it
UnixTimestampor so? ^.^).