{"id":13508920,"url":"https://github.com/bitwalker/timex_ecto","last_synced_at":"2025-04-12T18:48:33.570Z","repository":{"id":34198303,"uuid":"38053610","full_name":"bitwalker/timex_ecto","owner":"bitwalker","description":"An adapter for using Timex DateTimes with Ecto","archived":false,"fork":false,"pushed_at":"2021-12-24T08:56:23.000Z","size":106,"stargazers_count":164,"open_issues_count":11,"forks_count":69,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-03T22:07:20.724Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bitwalker.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-25T14:16:27.000Z","updated_at":"2025-03-23T15:50:11.000Z","dependencies_parsed_at":"2022-09-04T09:32:54.745Z","dependency_job_id":null,"html_url":"https://github.com/bitwalker/timex_ecto","commit_stats":null,"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitwalker%2Ftimex_ecto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitwalker%2Ftimex_ecto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitwalker%2Ftimex_ecto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitwalker%2Ftimex_ecto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bitwalker","download_url":"https://codeload.github.com/bitwalker/timex_ecto/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248618074,"owners_count":21134197,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T02:01:00.474Z","updated_at":"2025-04-12T18:48:33.549Z","avatar_url":"https://github.com/bitwalker.png","language":"Elixir","readme":"## Timex Plugin for Ecto\n\n[![Master](https://travis-ci.org/bitwalker/timex_ecto.svg?branch=master)](https://travis-ci.org/bitwalker/timex_ecto)\n[![Hex.pm Version](http://img.shields.io/hexpm/v/timex_ecto.svg?style=flat)](https://hex.pm/packages/timex_ecto)\n\n## Getting Started\n\nLearn how to add `timex_ecto` to your Elixir project and start using it.\n\n**NOTE**: You must use Timex 3.0.2 or greater with timex_ecto 3.x!\n\n### Adding timex_ecto To Your Project\n\nTo use timex_ecto with your projects, edit your `mix.exs` file and add it as a dependency:\n\n```elixir\ndef application do\n [ applications: [:timex_ecto, ...], ...]\nend\n\ndefp deps do\n  [{:timex, \"~\u003e 3.0\"},\n   {:timex_ecto, \"~\u003e 3.0\"}]\nend\n```\n\n### Adding Timex types to your Ecto models\n\n```elixir\ndefmodule User do\n  use Ecto.Schema\n\n  schema \"users\" do\n    field :name, :string\n    # Stored as an ISO date (year-month-day), reified as Date\n    field :a_date,        Timex.Ecto.Date # Timex version of :date\n    # Stored as an ISO time (hour:minute:second.fractional), reified as Timex.Duration\n    field :a_time,        Timex.Ecto.Time # Timex version of :time\n    # Stored as an ISO 8601 datetime in UTC (year-month-day hour:minute:second.fractional),\n    # reified as DateTime in UTC\n    field :a_datetime,    Timex.Ecto.DateTime # Timex version of :datetime\n    # DateTimeWithTimezone is a special case, please see the `Using DateTimeWithTimezone` section!\n    # Stored as a tuple of ISO 8601 datetime and timezone name ((year-month-day hour:minute:second.fractional, timezone)),\n    # reified as DateTime in stored timezone\n    field :a_datetimetz,  Timex.Ecto.DateTimeWithTimezone # A custom datatype (:datetimetz) implemented by Timex\n    # Stored as an ISO 8601 datetime in UTC (year-month-day hour:minute:second.fractional),\n    # reified as DateTime in local timezone\n    field :a_timestamptz, Timex.Ecto.TimestampWithTimezone # Timex version of :timestamptz\n  end\nend\n```\n\n## Using DateTimeWithTimezone\n\nPlease see the documentation [here](https://hexdocs.pm/timex_ecto/Timex.Ecto.DateTimeWithTimezone.html#content).\n\n### Change the default timestamps type of Ecto's `timestamps` macro\n\nAccording to the documentation for [Ecto.Schema.timestamps/1](https://hexdocs.pm/ecto/Ecto.Schema.html#timestamps/1), it is simple to change the default timestamps type to others by setting `@timestamps_opts`. For example, you can use `Timex.Ecto.TimestampWithTimezone` with the following options.\n\n```elixir\ndefmodule User do\n  use Ecto.Schema\n\n  @timestamps_opts [type: Timex.Ecto.TimestampWithTimezone,\n                    autogenerate: {Timex.Ecto.TimestampWithTimezone, :autogenerate, []}]\n\n  schema \"users\" do\n    field :name, :string\n    timestamps()\n  end\nend\n```\n\n### Using Timex with Ecto's `timestamps` macro\n\nSuper simple! Your timestamps will now be `Timex.Ecto.DateTime` structs instead of `Ecto.DateTime` structs.\n\n```elixir\ndefmodule User do\n  use Ecto.Schema\n\n  @timestamps_opts [type: Timex.Ecto.DateTime]\n\n  schema \"users\" do\n    field :name, :string\n    timestamps()\n  end\nend\n```\n\n### Using with Phoenix\n\nPhoenix allows you to apply defaults globally to Ecto models via `web/web.ex` by changing the `model` function like so:\n\n```elixir\ndef model do\n  quote do\n    use Ecto.Schema\n\n    @timestamps_opts [type: Timex.Ecto.DateTime]\n  end\nend\n```\n\nBy doing this, you bring the Timex timestamps into scope in all your models.\n\n\n### Precision\n\nBy default Timex will generate a timestamp to the nearest second. If you would\nlike to generate a timestamp with more precision you can pass the option\n`usec: true` to the macro. This will configure Timex to generate timestamps\ndown to the microsecond level of precision.\n\n```elixir\n@timestamps_opts [type: Timex.Ecto.DateTime,\n                  autogenerate: {Timex.Ecto.DateTime, :autogenerate, [:usec]}]\n```\n\n\n## Example Usage\n\nThe following is a simple test app I built for vetting this plugin:\n\n```elixir\ndefmodule EctoTest.Repo do\n  use Ecto.Repo, otp_app: :timex_ecto_test\nend\n\ndefmodule EctoTest.User do\n  use Ecto.Schema\n\n  @timestamps_opts [type: Timex.Ecto.DateTime,\n                    autogenerate: {Timex.Ecto.DateTime, :autogenerate, []}]\n\n  schema \"users\" do\n    field :name, :string\n    field :date_test,        Timex.Ecto.Date\n    field :time_test,        Timex.Ecto.Time\n    field :datetime_test,    Timex.Ecto.DateTime\n    field :datetimetz_test,  Timex.Ecto.DateTimeWithTimezone\n    field :timestamptz_test, Timex.Ecto.TimestampWithTimezone\n\n    timestamps()\n  end\nend\n\ndefmodule EctoTest do\n  import Ecto.Query\n  use Timex\n\n  alias EctoTest.User\n  alias EctoTest.Repo\n\n  def seed do\n    time        = Duration.now\n    date        = Timex.today\n    datetime    = Timex.now\n    datetimetz  = Timezone.convert(datetime, \"Europe/Copenhagen\")\n    timestamptz = Timex.local\n    u = %User{name: \"Paul\", date_test: date, time_test: time, datetime_test: datetime, datetimetz_test: datetimetz, timestamptz_test: timestamptz}\n    Repo.insert!(u)\n  end\n\n  def all do\n    query = from u in User,\n            select: u\n    Repo.all(query)\n  end\nend\n\ndefmodule EctoTest.App do\n  use Application\n\n  def start(_type, _args) do\n    import Supervisor.Spec\n    tree = [worker(EctoTest.Repo, [])]\n    opts = [name: EctoTest.Sup, strategy: :one_for_one]\n    Supervisor.start_link(tree, opts)\n  end\nend\n```\n\nAnd the results:\n\n```elixir\niex(1)\u003e EctoTest.seed\n\n14:45:43.461 [debug] INSERT INTO \"users\" (\"date_test\", \"datetime_test\", \"datetimetz_test\", \"name\", \"time_test\") VALUES ($1, $2, $3, $4, $5) RETURNING \"id\" [{2015, 6, 25}, {{2015, 6, 25}, {19, 45, 43, 457000}}, {{{2015, 6, 25}, {21, 45, 43, 457000}}, \"Europe/Copenhagen\"}, \"Paul\", {19, 45, 43, 457000}] OK query=3.9ms\n%EctoTest.User{__meta__: %Ecto.Schema.Metadata{source: \"users\",\n  state: :loaded},\n date_test: ~D[2015-06-25],\n datetime_test: #\u003cDateTime(2015-06-25T21:45:43.457Z Etc/UTC)\u003e,\n datetimetz_test: #\u003cDateTime(2015-06-25T21:45:43.457+02:00 Europe/Copenhagen)\u003e,\n timestamptz_test: #\u003cDateTime(2015-06-25T13:45:43.457-06:00 America/Chicago)\u003e,\n name: \"Paul\", time_test: #\u003cDuration(P45Y6M6DT19H45M43.456856S)\u003e}\niex(2)\u003e EctoTest.all\n\n14:45:46.721 [debug] SELECT u0.\"id\", u0.\"name\", u0.\"date_test\", u0.\"time_test\", u0.\"datetime_test\", u0.\"datetimetz_test\" FROM \"users\" AS u0 [] OK query=0.7ms\n[%EctoTest.User{__meta__: %Ecto.Schema.Metadata{source: \"users\",\n   state: :loaded},\n  date_test: ~D[2015-06-25],\n  datetime_test: #\u003cDateTime(2015-06-25T21:45:43.457Z Etc/UTC)\u003e,\n  datetimetz_test: #\u003cDateTime(2015-06-25T21:45:43.457+02:00 Europe/Copenhagen)\u003e,\n  timestamptz_test: #\u003cDateTime(2015-06-25T13:45:43.457-06:00 America/Chicago)\u003e,\n  name: \"Paul\", time_test: #\u003cDuration(PT19H45M43S)\u003e}]\niex(3)\u003e\n```\n\n## Additional Documentation\n\nDocumentation for Timex and timex_ecto are available\n[here], and on [hexdocs].\n\n[here]: https://hexdocs.pm/timex\n[hexdocs]: http://hexdocs.pm/timex_ecto/\n\n## License\n\nThis project is MIT licensed. See the LICENSE file in this repo.\n","funding_links":[],"categories":["ORM and Datamapping"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitwalker%2Ftimex_ecto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbitwalker%2Ftimex_ecto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitwalker%2Ftimex_ecto/lists"}