{"id":15288089,"url":"https://github.com/y-taka-23/time-machine","last_synced_at":"2026-03-05T11:03:21.360Z","repository":{"id":56880515,"uuid":"106702470","full_name":"y-taka-23/time-machine","owner":"y-taka-23","description":"A Haskell library to mock the current time. ⏰","archived":false,"fork":false,"pushed_at":"2018-05-01T06:28:06.000Z","size":16,"stargazers_count":18,"open_issues_count":2,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-08T16:19:41.851Z","etag":null,"topics":["haskell","monad","testing"],"latest_commit_sha":null,"homepage":"http://hackage.haskell.org/package/time-machine","language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/y-taka-23.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-12T14:18:35.000Z","updated_at":"2024-10-07T02:17:47.000Z","dependencies_parsed_at":"2022-08-21T00:20:24.001Z","dependency_job_id":null,"html_url":"https://github.com/y-taka-23/time-machine","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/y-taka-23/time-machine","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/y-taka-23%2Ftime-machine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/y-taka-23%2Ftime-machine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/y-taka-23%2Ftime-machine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/y-taka-23%2Ftime-machine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/y-taka-23","download_url":"https://codeload.github.com/y-taka-23/time-machine/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/y-taka-23%2Ftime-machine/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30121079,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-05T10:44:24.758Z","status":"ssl_error","status_checked_at":"2026-03-05T10:44:15.079Z","response_time":93,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["haskell","monad","testing"],"created_at":"2024-09-30T15:44:06.371Z","updated_at":"2026-03-05T11:03:21.319Z","avatar_url":"https://github.com/y-taka-23.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"time-machine\n============\n\n[![Build Status](https://travis-ci.org/y-taka-23/time-machine.svg?branch=master)](https://travis-ci.org/y-taka-23/time-machine)\n[![Hackage](https://img.shields.io/hackage/v/time-machine.svg)](https://hackage.haskell.org/package/time-machine)\n\nA library to mock the current time and relevant `IO` functions\nby using a type class.\nYou can get the great command of the current time in UTC,\ntime zones, and the speed of time.\n\n```haskell\nmodule Main where\n\nimport Control.Monad.TimeMachine\nimport Control.Monad.Trans ( liftIO )\n\nmain :: IO ()\nmain = backTo (the future) $ do\n    t \u003c- getCurrentTime\n    liftIO . putStrLn $ \"We are at \" ++ show t\n\n-- We are at 1985-10-26 08:24:00.035889 UTC\n```\n\nAs you know, time-dependent `IO` actions are extremely hard to test.\nAssume, for example, the following simple action.\nIt should return `\"Good morning\"` only in the morning,\nthus the results of its unit tests are fatally fragile.\n\n```haskell\ngetGreeting :: IO String\ngetGreeting = do\n    t \u003c- getCurrentTime\n    if utctDayTime t \u003c= 12 * 60 * 60\n        then return \"Good morning\"\n        else return \"Hello\"\n```\n\nThis library aims to make such actions testable with minimal changes.\nActually what you have to do is just:\n\n1. Use `MonadTime` type class instead of `IO`\n1. Wrap `IO` actions in `liftIO` if necessary\n\nHere is the testable version of `getGreeting`.\nYou can see that nothing is changed excepting the signature.\n\n```haskell\ngetGreeting :: (MonadTime m) =\u003e m String\ngetGreeting = do\n    t \u003c- getCurrentTime\n    if utctDayTime t \u003c= 12 * 60 * 60\n        then return \"Good morning\"\n        else return \"Hello\"\n```\n\nExamples\n--------\n\n### Mocking the Current Time\n\n`travelTo` changes the result of `getCurrentTime` and relevant actions.\n\nOther than pointing the target `UTCTime` explicitly,\nyou have two ways to determine how to mock the current time.\nThis library provides a small DSL to construct the destinations of your time travels.\n\n```haskell\n-- By a date-time according to your local time zone\nmain = travelTo (oct 26 1985 am 1 24) $ do\n    getCurrentTime \u003e\u003e= (liftIO . print)\n```\n\n```haskell\n-- By a relative date\nmain = travelTo (3 `days` ago) $ do\n    getCurrentTime \u003e\u003e= (liftIO . print)\n```\n\nFor more detail,\nsee the [document](https://hackage.haskell.org/package/time-machine).\n\n### Mocking Time Zones\n\n`jumpTo` switch the time zone which is used for calculating the local time.\nBy this function, you can test time-zone-sensitive actions.\n\n```haskell\nimport qualified Data.Time.Zones as TZ\n\nmain = jumpTo \"Asia/Shanghai\" $ do\n    t  \u003c- getCurrentTime\n    tz \u003c- loadLocalTZ\n    liftIO . print $ TZ.timeZoneForUTCTime tz t  -- CST\n```\n\n### Mocking the Speed of Time\n\n`accelerate` changes the speed of time.\nIn the following example, time flies 60 times faster than the real.\n\n```haskell\nmain = accelerate (x 60) $ do\n    getCurrentTime \u003e\u003e= (liftIO . print)  -- (*)\n    liftIO . threadDelay $ 1000 * 1000   -- wait a second\n    getCurrentTime \u003e\u003e= (liftIO . print)  -- around a minute after (*)\n```\n\nMoreover, as a special case of `accelerate`, `halt` stops the time.\nThat is remarkably useful to fix the point of time during your tests.\n\n```haskell\nmain = halt $ do\n    getCurrentTime \u003e\u003e= (liftIO . print)  -- (*)\n    liftIO . threadDelay $ 1000 * 1000   -- wait a second\n    getCurrentTime \u003e\u003e= (liftIO . print)  -- exactly same as (*)\n```\n\nInstallation\n------------\n\nThe project is managed by Stack, so you can install it simply:\n\n```console\n$ git clone https://github.com/y-taka-23/time-machine.git\n$ cd time-machine\n$ stack install\n```\n\nLicense\n-------\n\nThis project is released under the BSD 3-clause license.\nFor more details, see [LICENSE](./LICENSE) file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fy-taka-23%2Ftime-machine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fy-taka-23%2Ftime-machine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fy-taka-23%2Ftime-machine/lists"}