Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/whatyouhide/ecto_unix_timestamp
Ecto type for datetimes stored and cast as Unix timestamps. 🕰️
https://github.com/whatyouhide/ecto_unix_timestamp
ecto ecto-extension ecto-types elixir elixir-ecto elixir-lang elixir-library
Last synced: about 2 months ago
JSON representation
Ecto type for datetimes stored and cast as Unix timestamps. 🕰️
- Host: GitHub
- URL: https://github.com/whatyouhide/ecto_unix_timestamp
- Owner: whatyouhide
- License: mit
- Created: 2023-12-12T17:06:32.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-12T19:11:39.000Z (6 months ago)
- Last Synced: 2024-10-19T13:19:15.784Z (2 months ago)
- Topics: ecto, ecto-extension, ecto-types, elixir, elixir-ecto, elixir-lang, elixir-library
- Language: Elixir
- Homepage:
- Size: 29.3 KB
- Stars: 12
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
Awesome Lists containing this project
README
# EctoUnixTimestamp
[![hex.pm badge](https://img.shields.io/badge/Package%20on%20hex.pm-informational)](https://hex.pm/packages/ecto_unix_timestamp)
[![Documentation badge](https://img.shields.io/badge/Documentation-ff69b4)][docs]
[![CI](https://github.com/whatyouhide/ecto_unix_timestamp/actions/workflows/main.yml/badge.svg)](https://github.com/whatyouhide/ecto_unix_timestamp/actions/workflows/main.yml)
[![Coverage Status](https://coveralls.io/repos/github/whatyouhide/ecto_unix_timestamp/badge.svg?branch=main)](https://coveralls.io/github/whatyouhide/ecto_unix_timestamp?branch=main)A nimble Elixir library that provides a [`Ecto.Type`][ecto-type] for fields that come in as [**Unix timestamps**][unix-timestamp].
## Installation
```elixir
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.```elixir
defmodule User do
use Ecto.Schemaschema "users" do
field :created_at, EctoUnixTimestamp, unit: :second, underlying_type: :utc_datetime
end
end
```Once you have this, you can cast Unix timestamps:
```elixir
import Ecto.Changesetchangeset = 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](LICENSE.txt).
[unix-timestamp]: https://en.wikipedia.org/wiki/Unix_time
[ecto-type]: https://hexdocs.pm/ecto/Ecto.Type.html
[docs]: https://hexdocs.pm/ecto_unix_timestamp