Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.

Commit 308280d

Browse files
committed
Add mention of System.TimeProvider to README and mark ITimeProvider with obsolete attribute.
1 parent 0d9026e commit 308280d

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

IClock.Documentation/IClock.Documentation.shfbproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
<NamespaceSummaryItem name="IClock" isDocumented="True">&lt;para&gt;Provides interfaces and classes to work with (testable) date/times as an alternative to using &lt;see cref="P:System.DateTime.Now"&gt;DateTime.Now&lt;/see&gt; / &lt;see cref="P:System.DateTime.UtcNow"&gt;DateTime.UtcNow&lt;/see&gt;.&lt;/para&gt;</NamespaceSummaryItem>
5555
</NamespaceSummaries>
5656
<CopyrightHref>http://riii.me</CopyrightHref>
57+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
58+
<TargetFrameworkProfile />
5759
</PropertyGroup>
5860
<!-- There are no properties for these groups. AnyCPU needs to appear in order for Visual Studio to perform
5961
the build. The others are optional common platform types that may appear. -->

IClock/IClock.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
<RepositoryType>Git</RepositoryType>
1414
<Description>Provides a testable abstraction and alternative to DateTime.Now / DateTime.UtcNow</Description>
1515
<PackageTags>clock datetime datetimeoffset test unittesting mock dotnet-standard datetimeprovider</PackageTags>
16-
<Version>1.0.1</Version>
16+
<Version>1.1.0</Version>
1717
<Configurations>Debug;Release;ReleaseWithDocumentation</Configurations>
1818
<LangVersion>latest</LangVersion>
1919
<Nullable>enable</Nullable>
2020
<EnableNETAnalyzers>true</EnableNETAnalyzers>
2121
<AnalysisLevel>latest</AnalysisLevel>
22+
<PackageReadmeFile>README.md</PackageReadmeFile>
2223
</PropertyGroup>
2324

2425
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
@@ -30,6 +31,10 @@
3031
</PropertyGroup>
3132

3233
<ItemGroup>
34+
<None Include="..\README.md">
35+
<Pack>True</Pack>
36+
<PackagePath>\</PackagePath>
37+
</None>
3338
<None Include="..\logo.png">
3439
<Pack>True</Pack>
3540
<PackagePath></PackagePath>

IClock/ITimeProvider.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using System;
22

3-
namespace IClock
3+
namespace IClock;
4+
5+
/// <summary>
6+
/// Defines a method to get (date)time.
7+
/// </summary>
8+
[Obsolete("Use System.TimeProvider instead (https://learn.microsoft.com/en-us/dotnet/api/system.timeprovider)")]
9+
public interface ITimeProvider
410
{
511
/// <summary>
6-
/// Defines a method to get (date)time.
12+
/// Returns the providers' (date)time.
713
/// </summary>
8-
public interface ITimeProvider
9-
{
10-
/// <summary>
11-
/// Returns the providers' (date)time.
12-
/// </summary>
13-
/// <returns>The providers' (date)time.</returns>
14-
DateTimeOffset GetTime();
15-
}
14+
/// <returns>The providers' (date)time.</returns>
15+
DateTimeOffset GetTime();
1616
}

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
# <img src="https://raw.githubusercontent.com/RobThree/IClock/master/logo.png" alt="Logo" width="32" height="32"> IClock
1+
# <img src="https://raw.githubusercontent.com/RobThree/IClock/master/logo.png" alt="Logo" width="32" height="32"> IClock
22
Provides a testable abstraction and alternative to `DateTime.Now` / `DateTime.UtcNow` and `DateTimeOffset.Now` / `DateTimeOffset.UtcNow`. Targets netstandard1.0 and higher.
33

44
[![Build status](https://ci.appveyor.com/api/projects/status/cfocayl8qvi3d8cl)](https://ci.appveyor.com/project/RobIII/iclock) <a href="https://www.nuget.org/packages/IClock/"><img src="http://img.shields.io/nuget/v/IClock.svg?style=flat-square" alt="NuGet version" height="18"></a>
55

6+
## First: Good news! 🎉
7+
8+
As of the release of .Net 8 (nov. 14th 2023) Microsoft provides the [TimeProvider class](https://learn.microsoft.com/en-us/dotnet/api/system.timeprovider) and [ITimer interface](https://learn.microsoft.com/en-us/dotnet/api/system.threading.itimer). A good primer on this topic is over at [Andrew Lock's site](https://andrewlock.net/exploring-the-dotnet-8-preview-avoiding-flaky-tests-with-timeprovider-and-itimer/) ([archived version](https://archive.is/5bZU7)). You may want to check that out before continuing. What that means for IClock? It'll most likely be the end of this library, but that's a good thing. I'll keep supporting it for a while but switching to the Microsoft provided solution shouldn't be hard.
9+
610
## Why and how
711

812
When writing and testing (date)time-related code it is tempting to use any of the `DateTime.Now`, `DateTime.UtcNow`, `DateTimeOffset.Now` or `DateTimeOffset.UtcNow` properties. This, however, causes problems during (unit)testing. It makes your tests dependent on when your tests are run. This means your tests could pass on tuesdays and, without any changes, fail on wednesdays. Or only fail during nighttime or at any other time.

0 commit comments

Comments
 (0)