Skip to content

Ecto type for datetimes stored and cast as Unix timestamps. 🕰️

License

Notifications You must be signed in to change notification settings

whatyouhide/ecto_unix_timestamp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EctoUnixTimestamp

hex.pm badge Documentation badge CI Coverage Status

A nimble Elixir library that provides a Ecto.Type for fields that come in as Unix timestamps.

Installation

defp deps do
  [
    # ...,
    {:ecto_unix_timestamp, "~> 0.1.0"}
  ]
end

Usage

Use this Ecto type in your schemas. You'll have to choose the precision of the Unix timestamp, and the underlying database data type you want to store the data as.

defmodule User do
  use Ecto.Schema

  schema "users" do
    field :created_at, EctoUnixTimestamp, unit: :second, underlying_type: :utc_datetime
  end
end

Once you have this, you can cast Unix timestamps:

import Ecto.Changeset

changeset = cast(%User{}, %{created_at: 1672563600}, [:created_at])

fetch_field!(changeset, :created_at)
#=> ~U[2023-01-01 09:00:00Z]

License

Released under the MIT license. See the license file.

About

Ecto type for datetimes stored and cast as Unix timestamps. 🕰️

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages