{"id":18452002,"url":"https://github.com/benbjohnson/clock","last_synced_at":"2026-01-10T06:26:02.742Z","repository":{"id":20092887,"uuid":"23362234","full_name":"benbjohnson/clock","owner":"benbjohnson","description":"Clock is a small library for mocking time in Go.","archived":true,"fork":false,"pushed_at":"2023-05-18T03:44:34.000Z","size":78,"stargazers_count":677,"open_issues_count":3,"forks_count":114,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-10-29T19:24:33.652Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","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/benbjohnson.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,"governance":null}},"created_at":"2014-08-26T18:24:30.000Z","updated_at":"2024-10-25T15:21:30.000Z","dependencies_parsed_at":"2023-10-20T18:21:35.589Z","dependency_job_id":null,"html_url":"https://github.com/benbjohnson/clock","commit_stats":{"total_commits":52,"total_committers":24,"mean_commits":"2.1666666666666665","dds":0.7884615384615384,"last_synced_commit":"2fbf0cb60cfd2bd74f35e61d0333b7c2bd8cb75e"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benbjohnson%2Fclock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benbjohnson%2Fclock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benbjohnson%2Fclock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benbjohnson%2Fclock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benbjohnson","download_url":"https://codeload.github.com/benbjohnson/clock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222291153,"owners_count":16961737,"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-11-06T07:29:52.436Z","updated_at":"2026-01-10T06:26:02.671Z","avatar_url":"https://github.com/benbjohnson.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"clock\n=====\n\n[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\u0026style=flat-square)](https://pkg.go.dev/mod/github.com/benbjohnson/clock)\n\nClock is a small library for mocking time in Go. It provides an interface\naround the standard library's [`time`][time] package so that the application\ncan use the realtime clock while tests can use the mock clock.\n\nThis module is no longer maintained.\n\n[time]: https://pkg.go.dev/github.com/benbjohnson/clock\n\n## Usage\n\n### Realtime Clock\n\nYour application can maintain a `Clock` variable that will allow realtime and\nmock clocks to be interchangeable. For example, if you had an `Application` type:\n\n```go\nimport \"github.com/benbjohnson/clock\"\n\ntype Application struct {\n\tClock clock.Clock\n}\n```\n\nYou could initialize it to use the realtime clock like this:\n\n```go\nvar app Application\napp.Clock = clock.New()\n...\n```\n\nThen all timers and time-related functionality should be performed from the\n`Clock` variable.\n\n\n### Mocking time\n\nIn your tests, you will want to use a `Mock` clock:\n\n```go\nimport (\n\t\"testing\"\n\n\t\"github.com/benbjohnson/clock\"\n)\n\nfunc TestApplication_DoSomething(t *testing.T) {\n\tmock := clock.NewMock()\n\tapp := Application{Clock: mock}\n\t...\n}\n```\n\nNow that you've initialized your application to use the mock clock, you can\nadjust the time programmatically. The mock clock always starts from the Unix\nepoch (midnight UTC on Jan 1, 1970).\n\n\n### Controlling time\n\nThe mock clock provides the same functions that the standard library's `time`\npackage provides. For example, to find the current time, you use the `Now()`\nfunction:\n\n```go\nmock := clock.NewMock()\n\n// Find the current time.\nmock.Now().UTC() // 1970-01-01 00:00:00 +0000 UTC\n\n// Move the clock forward.\nmock.Add(2 * time.Hour)\n\n// Check the time again. It's 2 hours later!\nmock.Now().UTC() // 1970-01-01 02:00:00 +0000 UTC\n```\n\nTimers and Tickers are also controlled by this same mock clock. They will only\nexecute when the clock is moved forward:\n\n```go\nmock := clock.NewMock()\ncount := 0\n\n// Kick off a timer to increment every 1 mock second.\ngo func() {\n    ticker := mock.Ticker(1 * time.Second)\n    for {\n        \u003c-ticker.C\n        count++\n    }\n}()\nruntime.Gosched()\n\n// Move the clock forward 10 seconds.\nmock.Add(10 * time.Second)\n\n// This prints 10.\nfmt.Println(count)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenbjohnson%2Fclock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenbjohnson%2Fclock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenbjohnson%2Fclock/lists"}