Gen_auth: Change from NativeTime to DateTime?

Dear all,


Should I make any changes to the code generated by gen_auth for it to use UTC?

  • The User schema has a field field :confirmed_at, :naive_datetime – change this to :utc_datetime_usec?
  • Same module, function confirm_changeset has now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) – change to DateTimeand remove the truncation?
  • Change the migration for :users to add :confirmed_at, :utc_datetime?

I’m asking because I feel I’m missing something obvious and I haven’t found this covered somewhere. Feels like I’ve been wasting a whole lot of time on this.

The Setup

I’m starting a brand new project and I want to set it up as neatly as possible from the very beginning.

I have MyApp.Schema module that sets up binary IDs and options for timestamps as :utc_datetime_usec.

defmodule MyApp.Schema do
  defmacro __using__(_) do
    quote do
      use Ecto.Schema
      import Ecto.Changeset
      @primary_key {:id, :binary_id, autogenerate: true}
      @foreign_key_type :binary_id
      @timestamps_opts [type: :utc_datetime_usec]

Also, in config.exs I have set the option for migration_timestamps like this.

config :my_app,
  ecto_repos: [MyApp.Repo],
  generators: [binary_id: true],
  migration_timestamps: [type: :utc_datetime_usec]

If I understand things correctly, :naive_datetime is only the default in Ecto because of compatibility reasons. See this for example: Why use `utc_datetime` over `naive_datetime` for Ecto? - #4 by wojtekmach.

So yes, just go with :utc_datetime_usec!

1 Like