user6467981
user6467981

Reputation:

Using ISO8601 dates in Ecto Timestamps

Browsing through Ecto.DateTime, it seems to be deprecated in favor of :naive_datetime. I'd like to store the timestamp in the most accurate representation possible, and I can't seem to find what :naive_datetime provides me. From what I can tell, Ecto.DateTime is convertible to ISO8601, but it doesn't give any indication on how a naive datetime is stored in the database.

Is there other possible options to specify when creating timestamps like so:

schema "test" do 
  field :name
  timestamps type: :iso8601 # This doesn't work 
end 

I haven't been able to find a list of other possible options. Am I stuck with naive datetime?

Upvotes: 0

Views: 481

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

What’s wrong with NaiveDateTime? How might the time be more accurate? Just make sure your servers run in "Etc/UTC" and you are all set.

It sounds like you misunderstand what iso8601 is: it’s just a string representation. Also, using NaiveDateTime.to_iso8601/2 one always might get ISO8601 representation of NaiveDateTime:

NaiveDateTime.utc_now |> NaiveDateTime.to_iso8601
#⇒ "2017-10-27T05:15:42.355701"

Don’t be afraid of the term “naïve.” In this context it means “it does not store the timezone.” Nothing more.

Upvotes: 2

Related Questions