{"id":19893603,"url":"https://github.com/rzajac/clock","last_synced_at":"2025-07-03T17:36:00.794Z","repository":{"id":57561386,"uuid":"327936976","full_name":"rzajac/clock","owner":"rzajac","description":"Package clock is a thin wrapper around time.Now() to help testing time dependent code.","archived":false,"fork":false,"pushed_at":"2021-01-10T21:40:57.000Z","size":14,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-01T05:28:16.207Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rzajac.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":"2021-01-08T15:15:27.000Z","updated_at":"2021-01-10T21:40:41.000Z","dependencies_parsed_at":"2022-09-10T06:13:33.076Z","dependency_job_id":null,"html_url":"https://github.com/rzajac/clock","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/rzajac/clock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzajac%2Fclock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzajac%2Fclock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzajac%2Fclock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzajac%2Fclock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rzajac","download_url":"https://codeload.github.com/rzajac/clock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzajac%2Fclock/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263370171,"owners_count":23456425,"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-12T18:30:05.040Z","updated_at":"2025-07-03T17:36:00.772Z","avatar_url":"https://github.com/rzajac.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Go Report Card](https://goreportcard.com/badge/github.com/rzajac/clock)](https://goreportcard.com/report/github.com/rzajac/clock)\n[![GoDoc](https://img.shields.io/badge/api-Godoc-blue.svg)](https://pkg.go.dev/github.com/rzajac/clock)\n\n# clock\n\nTesting code which uses `time.Now()` is hard and in many instances impossible.\nPackage `clock` is an attempt to simplify this kind of tests by providing\npackage level clock and few test clock implementations.\n\n# Installation\n\n```\ngo get github.com/rzajac/clock\n```\n\n# Usage\n\n## Inject clock implementation.\n\nProduction code:\n\n```\nfunc NewStruct(clk clock.Clock) {\n\t// ...\n\tnow := clk()\n\t// ...\n}\n\ns := NewStruct(time.Now)\n\n// or\n\ns := NewStruct(clock.Now) // Works exaclty the same way as time.Now().\n```\n\nTests:\n\n```\ntim := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)\ns := NewStruct(clock.Fixed(start))\n```\n\n## Set clock globally\n\nIn case your code already uses `time.Now()` and you are not able to use\ninjection method, use package level clock and override its implementation in\ntests.\n\nProduction code:\n\n```\nfunc doStuff() {\n    // ...\n    doOtherStuff(clock.Now())\n    // ...\n}\n```\n\nTests:\n\n```\ntim := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)\nclock.SetClock(clock.Fixed(tim))\ndefer clock.SetDefault() // Make sure you set package level clock to default!\n\n// Perform your tests.\n```\n\n## Useful clock implementations.\n\nPackage `clock` provides few useful \"clock\" you can use in your tests.\n\n- `StartingAt(tim time.Time)` - sets current time.\n- `Fixed(tim time.Time)` - clock always returning the same time.\n    ```\n    tim := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)\n    clk := clock.Fixed(tim)\n    \n    fmt.Println(clk().Format(time.RFC3339)) // 2020-01-01T00:00:00Z\n    fmt.Println(clk().Format(time.RFC3339)) // 2020-01-01T00:00:00Z\n    fmt.Println(clk().Format(time.RFC3339)) // 2020-01-01T00:00:00Z\n    ```  \n- `Deterministic(start time.Time, tick time.Duration)` - clock which advances\n  given start time by tick every time you call it (no matter now fast or slow\n  you do it).\n    ```\n    start := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)\n    clk := clock.Deterministic(start, time.Second)\n\n    fmt.Println(clk().Format(time.RFC3339)) // 2020-01-01T00:00:00Z\n    fmt.Println(clk().Format(time.RFC3339)) // 2020-01-01T00:00:01Z\n    fmt.Println(clk().Format(time.RFC3339)) // 2020-01-01T00:00:02Z\n    ```\n\n## Benchmarks\n\n```\nBenchmarkClockNow-12    27322105    42.9 ns/op    0 B/op    0 allocs/op\nBenchmarkTimeNow-12     28653739    42.0 ns/op    0 B/op    0 allocs/op\n```\n\n## License\n\nApache License, Version 2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frzajac%2Fclock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frzajac%2Fclock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frzajac%2Fclock/lists"}