Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ Use this Ecto type in your schemas. You'll have to choose the **precision** of t
timestamp, and the underlying database data type you want to *store* the data as.

```elixir
schema "users" do
field :created_at, EctoUnixTimestamp, unit: :second, underlying_type: :utc_datetime_usec
defmodule User do
use Ecto.Schema

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

Expand All @@ -34,10 +38,10 @@ Once you have this, you can cast Unix timestamps:
```elixir
import Ecto.Changeset

changeset = cast(%User{}, %{created_at: System.system_time(:second)}, [:created_at])
changeset = cast(%User{}, %{created_at: 1672563600}, [:created_at])

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

## License
Expand Down
22 changes: 17 additions & 5 deletions lib/ecto_unix_timestamp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ defmodule EctoUnixTimestamp do
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.

schema "users" do
field :created_at, EctoUnixTimestamp, unit: :second, underlying_type: :utc_datetime_usec
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: System.system_time(:second)}, [:created_at])
changeset = cast(%User{}, %{created_at: 1672563600}, [:created_at])

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

## Options

Expand All @@ -35,7 +39,7 @@ defmodule EctoUnixTimestamp do
`:millisecond`, `:microsecond`, or `:nanosecond`. This option is **required**.

* `:underlying_type` - The underlying Ecto type to use for storing the data.
This option is required. It can be one of the native datetime types, that is,
This option is **required**. It can be one of the native datetime types, that is,
`:utc_datetime`, `:utc_datetime_usec`, `:naive_datetime`, or
`:naive_datetime_usec`.

Expand Down Expand Up @@ -123,9 +127,17 @@ defmodule EctoUnixTimestamp do
{:ok, data}
end

def load(_data, _loader, _params) do
:error
end

@impl true
def dump(data, _dumper, _params)
when is_nil(data) or is_struct(data, DateTime) or is_struct(data, NaiveDateTime) do
{:ok, data}
end

def dump(_data, _dumper, _params) do
:error
end
end