{"id":15022445,"url":"https://github.com/stphnwlsh/simpledatetimeprovider","last_synced_at":"2025-10-24T05:30:39.279Z","repository":{"id":44512596,"uuid":"430481387","full_name":"stphnwlsh/SimpleDateTimeProvider","owner":"stphnwlsh","description":"A simple abstraction over C#'s DateTime.Now, DateTime.Today and DateTime.UtcNow so you can control these values in your tests.","archived":false,"fork":false,"pushed_at":"2025-01-01T00:54:03.000Z","size":118,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-30T23:41:31.183Z","etag":null,"topics":["datetime","nuget","nuget-package","testing"],"latest_commit_sha":null,"homepage":"","language":"C#","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/stphnwlsh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-21T21:11:49.000Z","updated_at":"2025-01-01T00:54:07.000Z","dependencies_parsed_at":"2023-12-01T01:35:46.630Z","dependency_job_id":"8940ef10-c9ae-421d-bc98-4b5c373c01d0","html_url":"https://github.com/stphnwlsh/SimpleDateTimeProvider","commit_stats":{"total_commits":75,"total_committers":2,"mean_commits":37.5,"dds":"0.31999999999999995","last_synced_commit":"ffec58163a2a666c3a619aa9f135324a1e0377e8"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stphnwlsh%2FSimpleDateTimeProvider","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stphnwlsh%2FSimpleDateTimeProvider/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stphnwlsh%2FSimpleDateTimeProvider/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stphnwlsh%2FSimpleDateTimeProvider/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stphnwlsh","download_url":"https://codeload.github.com/stphnwlsh/SimpleDateTimeProvider/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237915421,"owners_count":19386724,"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":["datetime","nuget","nuget-package","testing"],"created_at":"2024-09-24T19:57:57.693Z","updated_at":"2025-10-24T05:30:38.924Z","avatar_url":"https://github.com/stphnwlsh.png","language":"C#","readme":"# SimpleDateTimeProvider\n\nA simple abstraction over C#'s DateTime.Now, DateTime.Today and DateTime.UtcNow so you can control these values in your tests. No longer do you have to attempt shennanigans in your tests to handle when you need to use those values in your code.\n\n## Inspiration\n\nI have been lucky enough to work with a few individuals over time who have enlightened me to the utility of being able to mock `DateTime` in my tests.  So thanks to [Andrew Harcourt](https://github.com/uglybugger) for pushing this at unnamed previous employers, and thanks to my team at Andrew Wickens and Jared Wilton at [Tigerspike](https://tigerspike.com) you are responsible for keeping me honest and pushing me to always be learning.\n\n## Features\n\n * `SystemDateTimeProvider` - Abstraction over the top of System.DateTime. \n * `MockDateTimeProvider` - Mockable provider to control DateTime values in test projects.\n\n\n## Setup\n\nIt's easy to get under way using the providers, simply inject the system provider under the `IDateTimeProvider` interface in your functional code.  If you are using another library you'll know the syntax but follow the same formula.\n\n```csharp\n_ = services.AddSingleton\u003cIDateTimeProvider, SystemDateTimeProvider\u003e();\n```\n\nCreate your class and use that registered `SystemDateTimeProvider` that we just created via the `IDateTimeProvider` interface.  Then use the provider to set the `DateTime` values in your class.\n\n```csharp\npublic class Service\n{\n    private readonly IDateTimeProvider dateTimeProvider;\n\n    public Service(IDateTimeProvider dateTimeProvider)\n    {\n        this.dateTimeProvider = dateTimeProvider;\n    }\n\n    public string DateTimeNow()\n    {\n        return $\"DateTime.Now is {this.dateTimeProvider.Now}\";\n    }\n}\n```\n\n## Testing\n\nThe whole purpose of this was to allow for testable code.  So now that you have your class above, you can inject the `MockDateTimeProvider` in its place to control the `DateTime` values in your tests.  It's as easy as the sample below.  \n\n```csharp\n[Fact]\npublic void Now_ShouldReturn_MockedToday()\n{\n    // Arrange\n    var provider = new MockDateTimeProvider();\n    var service = new Service(provider);\n    var today = DateTime.Today;\n\n    provider.Today = today;\n\n    // Act\n    var result = service.DateTimeToday();\n\n    // Assert\n    _ = result.ShouldBeOfType\u003cstring\u003e();\n    result.ShouldBe($\"DateTime.Today is {today}\");\n}\n```\n\n## Support\n\nI'm sharing some of my work here and if it helps you, I'd love it if you'd consider supporting me.\n\n[![\"Buy Me A Coffee\"](https://www.buymeacoffee.com/assets/img/guidelines/download-assets-sm-1.svg)](https://www.buymeacoffee.com/stphnwlsh)\n\n","funding_links":["https://www.buymeacoffee.com/stphnwlsh"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstphnwlsh%2Fsimpledatetimeprovider","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstphnwlsh%2Fsimpledatetimeprovider","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstphnwlsh%2Fsimpledatetimeprovider/lists"}