{"id":19023806,"url":"https://github.com/shinyscorpion/time_machinex","last_synced_at":"2025-04-23T10:45:40.444Z","repository":{"id":62430438,"uuid":"119562856","full_name":"shinyscorpion/time_machinex","owner":"shinyscorpion","description":"Time machine clock to simplify time testing","archived":false,"fork":false,"pushed_at":"2019-12-19T11:56:34.000Z","size":27,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-14T14:09:32.701Z","etag":null,"topics":["elixir","test"],"latest_commit_sha":null,"homepage":"","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/shinyscorpion.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":"2018-01-30T16:28:48.000Z","updated_at":"2021-12-13T19:11:59.000Z","dependencies_parsed_at":"2022-11-01T20:17:42.690Z","dependency_job_id":null,"html_url":"https://github.com/shinyscorpion/time_machinex","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinyscorpion%2Ftime_machinex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinyscorpion%2Ftime_machinex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinyscorpion%2Ftime_machinex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinyscorpion%2Ftime_machinex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shinyscorpion","download_url":"https://codeload.github.com/shinyscorpion/time_machinex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250419501,"owners_count":21427612,"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":["elixir","test"],"created_at":"2024-11-08T20:33:00.637Z","updated_at":"2025-04-23T10:45:40.423Z","avatar_url":"https://github.com/shinyscorpion.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TimeMachinex\n[![Hex.pm](https://img.shields.io/hexpm/v/time_machinex.svg \"Hex\")](https://hex.pm/packages/time_machinex)\n[![Build Status](https://travis-ci.org/shinyscorpion/time_machinex.svg?branch=master)](https://travis-ci.org/shinyscorpion/time_machinex)\n[![Hex.pm](https://img.shields.io/hexpm/l/time_machinex.svg \"License\")](LICENSE.md)\n## Installation\n\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed\nby adding `time_machinex` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:time_machinex, \"~\u003e 0.3.0\"}\n  ]\nend\n```\n\n## Introduction\nTimeMachinex is just a managed clock around the system clock which works with `DateTime.t()` types.\n\n### Configuration\nConfiguring TimeMachine is very simple. What you generally want to do is to configure the *SystemClock* for `:prod` and `:dev` env:\n\n```elixir\nconfig :time_machinex, TimeMachinex, adapter: TimeMachinex.SystemClock\n```\n\nand the *ManagedClock* in the `:test` env:\n```elixir\nconfig :time_machinex, TimeMachinex, adapter: TimeMachinex.ManagedClock\n```\n\n### Usage\nWhenever you need the current system time just replace the standard `DateTime.utc_now/0` call with a `TimeMachinex.utc_now/0` or `TimeMachinex.utc_now/1` call.\nPlease note that `utc_now` returns an `UTCDateTime` type. (Please use `now/0` and `now/1` if you need a standard `DateTime`)\n\nIn `:prod` and `:dev` this will have no real side effects, since the `now/0` function is just an alias for the `DateTime.utc_now/0` thanks to the inline compilation attribute.\n\nThe magic happens in the `:test` environment since the `TimeMachinex.ManagedClock` adapter will kick in (if configured properly).\nThe only thing you need to do in your tests is to start the *ManagedClock*:\n\n    ex(1)\u003e TimeMachinex.ManagedClock.start()\n    {:ok, #PID\u003c0.190.0\u003e}\n\nwhen it starts it is configured with the current time:\n\n    iex(2)\u003e TimeMachinex.ManagedClock.utc_now()\n    ~Z[2019-12-16 14:34:32.623987]\n\nbut now all the calls to `TimeMachinex.utc_now/0` will read the time from the *ManagedClock*\n\n    iex(3)\u003e TimeMachinex.utc_now()\n    ~Z[2019-12-16 14:34:32.623987]\n\nwhich means that you can manipulate, simulate the time passing and test the time used in your production code.\n\nAnd yes, you stopped the time!\n\n    iex(4)\u003e TimeMachinex.utc_now()\n    ~Z[2019-12-16 14:34:32.623987]\n    iex(5)\u003e TimeMachinex.utc_now()\n    ~Z[2019-12-16 14:34:32.623987]\n\nIf you want to update the TimeMachinex with the current time again (to simulate time passing) you can just:\n\n    iex(7)\u003e TimeMachinex.ManagedClock.set()\n    :ok\n    iex(8)\u003e TimeMachinex.utc_now()\n    ~Z[2019-12-16 14:38:04.255975]\n\nor you may just want to set a specific time and wait for Marty McFly:\n\n    iex(9)\u003e ~N[1985-10-26 09:00:00] |\u003e DateTime.from_naive!(\"Etc/UTC\") |\u003e TimeMachinex.ManagedClock.set()\n    :ok\n\n    iex(10)\u003e TimeMachinex.utc_now()\n    ~Z[1985-10-26 09:00:00]\n\nHappy time travel!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshinyscorpion%2Ftime_machinex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshinyscorpion%2Ftime_machinex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshinyscorpion%2Ftime_machinex/lists"}