Skip to content

Commit 7d78408

Browse files
chore: consume Timecop.Core package, change the readme. (#3)
1 parent d23d4d4 commit 7d78408

7 files changed

Lines changed: 27 additions & 174 deletions

File tree

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Timecop is a small library that helps you test DateTime in a static, thread-safe
55

66
Timecop targets .NET Standard 2.0, has no external dependencies, and can be used with .NET Framework 4.5+ and any version of .NET and .NET Core.
77

8-
Timecop is the C# port of the [timecop](https://github.com/travisjeffery/timecop) Ruby gem.
8+
Timecop has been inspired by the [timecop](https://github.com/travisjeffery/timecop) Ruby gem.
99

1010
## Installation
1111

@@ -32,10 +32,12 @@ string Greet()
3232

3333
return $"Good {timeOfDay}!";
3434
}
35+
3536
// freeze at 2pm local time:
3637
using var tc = Timecop.Frozen(o => o.At(14,0,0).LocalTime());
3738

3839
Greet(); // Good afternoon!
40+
3941
// travel to 8pm local time:
4042
tc.TravelBy(TimeSpan.FromHours(6));
4143

@@ -48,32 +50,39 @@ Greet(); // Good evening!
4850

4951
Time is frozen with either an instance `Freeze` or a static `Frozen` method, both having the same set of signatures. The instance `Freeze` freezes the instance of `Timecop`, the static `Frozen` creates an already frozen instance.
5052

51-
Frozen time doesn't run for your tests unless you call `Resume` or dispose the `Timecop` instance:
53+
Frozen time doesn't run for your tests until you call `Resume` or dispose the `Timecop` instance:
5254

5355
```csharp
5456
using var tc = Timecop.Frozen(1990, 12, 2, 14, 38, 51, DateTimeKind.Local);
57+
5558
Clock.Now; // 1990-12-02 14:38:51
5659
5760
Thread.Sleep(TimeSpan.FromSeconds(3));
58-
Clock.Now; // 1990-12-02 14:38:51 - Still the same value
61+
62+
Clock.Now; // 1990-12-02 14:38:51 - still the same value
5963
6064
tc.Resume();
6165

6266
Thread.Sleep(TimeSpan.FromSeconds(3));
63-
Clock.Now; // 1990-12-02 14:38:54 - Time has changed
67+
68+
Clock.Now; // 1990-12-02 14:38:54 - time has changed
6469
```
6570

66-
Both `Freeze` and `Frozen` have the multiple overloads:
71+
Both `Freeze` and `Frozen` have multiple overloads:
6772

6873
```csharp
6974
// freeze at the current instant:
7075
tc.Freeze();
76+
7177
// freeze at the specified DateTime:
7278
tc.Freeze(new DateTime(1990, 12, 2, 14, 38, 51, DateTimeKind.Utc));
79+
7380
// freeze at the specified date and time:
7481
tc.Freeze(1990, 12, 2, 14, 38, 51, DateTimeKind.Utc);
82+
7583
// freeze at the specified date:
7684
tc.Freeze(1990, 12, 2, DateTimeKind.Utc);
85+
7786
// freeze at the specified date or time or both:
7887
tc.Freeze(o => o.On(1990, 12, 2)
7988
.At(14, 13, 51)
@@ -86,9 +95,12 @@ Use the `TravelBy` method to travel forward and backward in time:
8695

8796
```csharp
8897
using var tc = Timecop.Frozen(1990, 12, 2, 14, 38, 51, DateTimeKind.Local);
98+
8999
tc.TravelBy(TimeSpan.FromDays(1));
90-
Clock.Now; // 1990-12-03 14:38:51 - One day in the future
100+
101+
Clock.Now; // 1990-12-03 14:38:51 - one day in the future
91102
```
103+
92104
## License
93105

94106
Timecop was created by [Dmytro Khmara](https://dmytrokhmara.com) and is licensed under the [MIT license](LICENSE.txt).

src/Timecop/DateTimeUtils/UtcDateTime.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using TCop.Core;
23

34
namespace TCop.DateTimeUtils;
45

@@ -15,4 +16,6 @@ public UtcDateTime(DateTime value)
1516

1617
UtcValue = value.ToUniversalTime();
1718
}
19+
20+
public PointInTime PointInTime => new (UtcValue);
1821
}

src/Timecop/Timecop.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using TCop.Core;
23
using TCop.DateTimeUtils;
34

45
namespace TCop;
@@ -7,7 +8,7 @@ public class Timecop : IDisposable
78
{
89
private readonly TimecopContextStore _contextStore = new();
910

10-
public static DateTime UtcNow => TimecopContextStore.AsyncContextUtcNow;
11+
public static DateTime UtcNow => TimecopContextStore.AsyncContextUtcNow.DateTime;
1112

1213
/// <summary>Moves in time backward or forward by the specified amount of time.</summary>
1314
/// <param name="duration">The amount of time to travel by. Can be positive or negative.</param>
@@ -18,7 +19,7 @@ public void TravelBy(TimeSpan duration)
1819

1920
private void ConvertAndFreeze(UtcDateTime? utcDateTime)
2021
{
21-
_contextStore.Mutate((ref TimecopContext context, DateTime utcNow) => context.Freeze(utcDateTime?.UtcValue ?? utcNow));
22+
_contextStore.Mutate((ref TimecopContext context, PointInTime utcNow) => context.Freeze(utcDateTime?.PointInTime ?? utcNow));
2223
}
2324

2425
/// <summary>Freezes an instance of <see cref="T:TCop.Timecop" /> at the current time.</summary>
@@ -80,7 +81,7 @@ public Timecop Freeze(Action<DateTimeBuilder> config)
8081
/// <summary>Resumes the flow of time of a frozen instance of <see cref="T:TCop.Timecop" />.</summary>
8182
public void Resume()
8283
{
83-
_contextStore.Mutate((ref TimecopContext context, DateTime utcNow) => context.Unfreeze(utcNow));
84+
_contextStore.Mutate((ref TimecopContext context, PointInTime utcNow) => context.Unfreeze(utcNow));
8485
}
8586

8687
/// <summary>Creates an instance of <see cref="T:TCop.Timecop" /> and freezes it at the current time.</summary>

src/Timecop/Timecop.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
<PrivateAssets>all</PrivateAssets>
3434
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3535
</PackageReference>
36+
37+
<PackageReference Include="Timecop.Core" Version="1.1.0" />
3638
</ItemGroup>
3739

3840
</Project>

src/Timecop/TimecopContext.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/Timecop/TimecopContextStore.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/Timecop.Tests/TimecopContextTests.cs

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)