Ahhhhh!
So I now get the error, it is a bit subtle.
The old utc_datetime
supported a millisecond timestamp, e.g.:
#DateTime<2019-11-02 15:11:28.833Z>
Even though it was called microsecond
.
In Ecto.Type
, you are requiring either an empty :microsecond
field, or exactly 6 digits of precision, therefore this bombs:
check_usec!(%{microsecond: {_, 6}} = datetime, _kind), do: datetime
whereas this would work:
check_usec!(%{microsecond: {_, 3}} = datetime, _kind), do: datetime
This is actually how the data is currently stored in the DB (a DateTime with 3 digits of precision), and likewise is also the result of DateTime.from_iso8601 below. (Note that I’m getting the timestamp string from a JSON api)
iex > {:ok, datetime, _} = DateTime.from_iso8601("2019-11-02T15:11:28.833Z")
iex > %{microsecond: {_, 3}} = datetime
#DateTime<2019-11-02 15:11:28.833Z>
iex > %{microsecond: {_, 6}} = datetime
** (MatchError) no match of right hand side value: #DateTime<2019-11-02 15:11:28.833Z>
So, it seems like the gist of it is that there are no more millisecond
DateTimes allow in Ecto (even though it’s a perfectly valid DateTime
), just seconds (:utc_datetime
) or 6 digit microseconds (:utc_datetime_usec
).
Interesting. Not 100% sure I get the logic behind this decision to bomb on milliseconds. Why not just add three zeros at the end? Or allow 3 digits? Or have a :utc_datetime_msec
type?
Maybe I’m missing something?