Skip to content

Commit 36866e7

Browse files
author
Scott Williams
committed
Add pin delegate for clock and clockoffset
- this enables clock offsetting capabilities
1 parent 3012c1f commit 36866e7

13 files changed

Lines changed: 131 additions & 53 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<SolutionConfiguration>
2+
<Settings>
3+
<AllowParallelTestExecution>True</AllowParallelTestExecution>
4+
<SolutionConfigured>True</SolutionConfigured>
5+
</Settings>
6+
</SolutionConfiguration>

src/Tocsoft.DateTimeAbstractions/Clock.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,23 @@ public static IDisposable Pin()
4444
/// <summary>
4545
/// Pins the specified date/time retuned to the current time until the disposable is disposed.
4646
/// </summary>
47-
/// <param name="date">The date and time to the clocks time to.</param>
47+
/// <param name="date">The date and time to the clock.</param>
4848
/// <returns>The disposer that manages the lifetime of the scoped pinned value.</returns>
4949
public static IDisposable Pin(DateTime date)
5050
{
5151
return Pin(new StaticDateTimeProvider(date));
5252
}
5353

54+
/// <summary>
55+
/// Pins the clock to call the function every time it needs access to the current time until the disposable is disposed.
56+
/// </summary>
57+
/// <param name="dateFunc">The function that returnes date and time to the clocks.</param>
58+
/// <returns>The disposer that manages the lifetime of the scoped pinned value.</returns>
59+
public static IDisposable Pin(Func<DateTime> dateFunc)
60+
{
61+
return Pin(new DelegateDateTimeProvider(dateFunc));
62+
}
63+
5464
internal static IDisposable Pin(DateTimeProvider provider)
5565
{
5666
ImmutableStack<DateTimeProvider> stack = clockStack.Value ?? ImmutableStack.Create<DateTimeProvider>();
@@ -82,12 +92,12 @@ public void Dispose()
8292
/// <summary>
8393
/// Gets the current local DateTime unless pinned then it will returned the pinned time as a local time.
8494
/// </summary>
85-
public static DateTime Now => CurrentProvider.Now();
95+
public static DateTime Now => CurrentProvider.UtcNow().ToLocalTime();
8696

8797
/// <summary>
8898
/// Gets the current the Date portion of the current local DateTime unless pinned then it will returned the date portion of the pinned time.
8999
/// </summary>
90-
public static DateTime Today => CurrentProvider.Now().Date;
100+
public static DateTime Today => Now.Date;
91101

92102
/// <summary>
93103
/// Gets the current UTC DateTime unless pinned then it will returned the pinned time as a UTC time.

src/Tocsoft.DateTimeAbstractions/ClockOffset.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ public static IDisposable Pin(DateTimeOffset date)
5151
return Pin(new StaticDateTimeOffsetProvider(date));
5252
}
5353

54+
/// <summary>
55+
/// Pins the clock to call the function every time it needs access to the current time until the disposable is disposed.
56+
/// </summary>
57+
/// <param name="dateFunc">The function that returnes date and time to the clocks.</param>
58+
/// <returns>The disposer that manages the lifetime of the scoped pinned value.</returns>
59+
public static IDisposable Pin(Func<DateTimeOffset> dateFunc)
60+
{
61+
return Pin(new DelegateDateTimeOffsetProvider(dateFunc));
62+
}
63+
5464
internal static IDisposable Pin(DateTimeOffsetProvider provider)
5565
{
5666
ImmutableStack<DateTimeOffsetProvider> stack = clockStack.Value ?? ImmutableStack.Create<DateTimeOffsetProvider>();
@@ -82,7 +92,7 @@ public void Dispose()
8292
/// <summary>
8393
/// Gets the current local DateTimeOffset unless pinned then it will returned the pinned time as a local time.
8494
/// </summary>
85-
public static DateTimeOffset Now => CurrentProvider.Now();
95+
public static DateTimeOffset Now => CurrentProvider.UtcNow().ToLocalTime();
8696

8797
/// <summary>
8898
/// Gets the current UTC DateTimeOffset unless pinned then it will returned the pinned time as a UTC time.

src/Tocsoft.DateTimeAbstractions/Providers/CurrentDateTimeOffsetProvider.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ namespace Tocsoft.DateTimeAbstractions.Providers
77
{
88
internal class CurrentDateTimeOffsetProvider : DateTimeOffsetProvider
99
{
10-
public override DateTimeOffset Now()
11-
{
12-
return DateTimeOffset.Now;
13-
}
14-
1510
public override DateTimeOffset UtcNow()
1611
{
1712
return DateTimeOffset.UtcNow;

src/Tocsoft.DateTimeAbstractions/Providers/CurrentDateTimeProvider.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ namespace Tocsoft.DateTimeAbstractions.Providers
77
{
88
internal class CurrentDateTimeProvider : DateTimeProvider
99
{
10-
public override DateTime Now()
11-
{
12-
return DateTime.Now;
13-
}
14-
1510
public override DateTime UtcNow()
1611
{
1712
return DateTime.UtcNow;

src/Tocsoft.DateTimeAbstractions/Providers/DateTimeOffsetProvider.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ namespace Tocsoft.DateTimeAbstractions.Providers
77
{
88
internal abstract class DateTimeOffsetProvider
99
{
10-
public abstract DateTimeOffset Now();
11-
1210
public abstract DateTimeOffset UtcNow();
1311
}
1412
}

src/Tocsoft.DateTimeAbstractions/Providers/DateTimeProvider.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ namespace Tocsoft.DateTimeAbstractions.Providers
77
{
88
internal abstract class DateTimeProvider
99
{
10-
public abstract DateTime Now();
11-
1210
public abstract DateTime UtcNow();
1311
}
1412
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Tocsoft and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System;
5+
6+
namespace Tocsoft.DateTimeAbstractions.Providers
7+
{
8+
internal class DelegateDateTimeOffsetProvider : DateTimeOffsetProvider
9+
{
10+
private readonly Func<DateTimeOffset> dateFunc;
11+
12+
public DelegateDateTimeOffsetProvider(Func<DateTimeOffset> dateFunc)
13+
{
14+
this.dateFunc = dateFunc;
15+
}
16+
17+
public override DateTimeOffset UtcNow()
18+
{
19+
return this.dateFunc().ToUniversalTime();
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Tocsoft and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System;
5+
6+
namespace Tocsoft.DateTimeAbstractions.Providers
7+
{
8+
internal class DelegateDateTimeProvider : DateTimeProvider
9+
{
10+
private readonly Func<DateTime> datefunc;
11+
12+
public DelegateDateTimeProvider(Func<DateTime> datefunc)
13+
{
14+
this.datefunc = datefunc;
15+
}
16+
17+
public override DateTime UtcNow()
18+
{
19+
return this.datefunc().ToUniversalTime();
20+
}
21+
}
22+
}

src/Tocsoft.DateTimeAbstractions/Providers/StaticDateTimeOffsetProvider.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,13 @@ namespace Tocsoft.DateTimeAbstractions.Providers
77
{
88
internal class StaticDateTimeOffsetProvider : DateTimeOffsetProvider
99
{
10-
private readonly DateTimeOffset date;
1110
private readonly DateTimeOffset utcdate;
1211

1312
public StaticDateTimeOffsetProvider(DateTimeOffset date)
1413
{
15-
this.date = date.ToLocalTime();
1614
this.utcdate = date.ToUniversalTime();
1715
}
1816

19-
public override DateTimeOffset Now()
20-
{
21-
return this.date;
22-
}
23-
2417
public override DateTimeOffset UtcNow()
2518
{
2619
return this.utcdate;

0 commit comments

Comments
 (0)