https://github.com/tocsoft/datetimeabstractions
Provides a test friendly wrapper over DateTime static properties
https://github.com/tocsoft/datetimeabstractions
Last synced: 9 months ago
JSON representation
Provides a test friendly wrapper over DateTime static properties
- Host: GitHub
- URL: https://github.com/tocsoft/datetimeabstractions
- Owner: tocsoft
- License: apache-2.0
- Created: 2018-01-30T08:07:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-01-25T18:59:42.000Z (over 5 years ago)
- Last Synced: 2025-10-06T07:53:57.959Z (9 months ago)
- Language: C#
- Homepage:
- Size: 104 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tocsoft.DateTimeAbstractions
**Tocsoft.DateTimeAbstractions** is a testable alternative to the static `DateTime.Now` and `DateTimeOffset.Now` methods.
[](https://github.com/tocsoft/DateTimeAbstractions/actions?query=workflow%3A.NET+branch%3Amaster)
[](https://codecov.io/gh/Tocsoft/DateTimeAbstractions)
[](https://raw.githubusercontent.com/Tocsoft/DateTimeAbstractions/master/LICENSE)
[](https://github.com/Tocsoft/DateTimeAbstractions/issues)
[](https://github.com/Tocsoft/DateTimeAbstractions/stargazers)
[](https://github.com/Tocsoft/DateTimeAbstractions/network)
### Installation
We also have a [MyGet package repository](https://www.myget.org/gallery/tocsoft) - for bleeding-edge / development NuGet releases.
### Usage
Using `Tocsoft.DateTimeAbstractions` is as simple as switching out all usages of `DateTime.Now` with `Clock.Now` and your done your businesslogic thats dependent of on getting the current time is now more testable.
### Using `Tocsoft.DateTimeAbstractions` from Tests
Now your code is using `Clock.Now` you can now wrap your code under test like so
```c#
using (Clock.Pin(new DateTime(2018, 02, 02)))
{
// Clock.Now will always return the pinned value until the using block has ended.
Assert.Equal(new DateTime(2018, 02, 02), Clock.Now);
}
````
### Additional APIs
You probably aren't using DateTime.Now in you code so how can this library help? well we support all the static properties from `DateTime` and `DateTimeOffset` so any of this properties can be switched out and tested.
| System API | DateTimeAbstractions Replacement |
|------------------------|----------------------------------|
| `DateTime.Now` | `Clock.Now` |
| `DateTime.UtcNow` | `Clock.UtcNow` |
| `DateTime.Today` | `Clock.Today` |
| `DateTimeOffset.Now` | `ClockOffset.Now` |
| `DateTimeOffset.UtcNow`| `ClockOffset.UtcNow` |
> We include warnings and code fixes for all these mappings.
In addition to the `DateTime` set of APIs we also include a testable wrapper around using the `Stopwatch` class in the form of the `ClockTimer` as with the `Clock` APIs we support pinning for testing purposes.
### The Team
Lead
- [Scott Williams](https://github.com/tocsoft)