Skip to content

Commit edbe644

Browse files
committed
add utcnow
1 parent 8c4c2c7 commit edbe644

8 files changed

Lines changed: 101 additions & 21 deletions

File tree

Tocsoft.DateTimeAbstractions.Analyzer.Test/TocsoftDateTimeAbstractionsAnalyzerUnitTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,46 @@ public TypeName(){
9797
VerifyCSharpFix(test, fixtest);
9898
}
9999

100+
[TestMethod]
101+
public void DateTimeUtcNowMappsToClockUtcNow()
102+
{
103+
var test = @"
104+
using System;
105+
namespace TestApplication
106+
{
107+
class TypeName
108+
{
109+
public TypeName(){
110+
DateTime time = DateTime.UtcNow;
111+
}
112+
}
113+
}";
114+
AdditionalCodeFiles = new[] {
115+
@"
116+
using System;
117+
118+
namespace Tocsoft.DateTimeAbstractions
119+
{
120+
public static class Clock { public static DateTime UtcNow { get; set; } }
121+
}"
122+
};
123+
124+
var fixtest = @"
125+
using System;
126+
using Tocsoft.DateTimeAbstractions;
127+
128+
namespace TestApplication
129+
{
130+
class TypeName
131+
{
132+
public TypeName(){
133+
DateTime time = Clock.UtcNow;
134+
}
135+
}
136+
}";
137+
VerifyCSharpFix(test, fixtest);
138+
}
139+
100140
protected override CodeFixProvider GetCSharpCodeFixProvider()
101141
{
102142
return new DateTimeUsageCodeFixProvider();

Tocsoft.DateTimeAbstractions.Analyzer/DateTimeUsageCodeFixProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ public sealed override FixAllProvider GetFixAllProvider()
3333
return WellKnownFixAllProviders.BatchFixer;
3434
}
3535

36-
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
36+
public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
3737
{
38-
39-
// TODO: Replace the following code with your own analysis, generating a CodeAction for each fix to suggest
4038
var diagnostic = context.Diagnostics.First();
4139

4240
// Register a code action that will invoke the fix.
@@ -46,6 +44,8 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
4644
createChangedDocument: c => ReplaceWithCallToClock(context, c),
4745
equivalenceKey: title),
4846
diagnostic);
47+
48+
return Task.CompletedTask;
4949
}
5050

5151
private async Task<Document> ReplaceWithCallToClock(CodeFixContext context, CancellationToken cancellationToken)
@@ -54,7 +54,7 @@ private async Task<Document> ReplaceWithCallToClock(CodeFixContext context, Canc
5454

5555
var root = await context.Document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
5656

57-
// this is us accessing the proeprty on datetime i.e. the call to 'DateTime.Now'
57+
// this is us accessing the property on datetime i.e. the call to 'DateTime.Now'
5858

5959
root = await ReplaceMemberCall(context, root).ConfigureAwait(false);
6060
root = ApplyUsings(root);

Tocsoft.DateTimeAbstractions.Tests/AsyncScoppedClock.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,38 @@ namespace Tocsoft.DateTimeAbstractions.Tests
88
{
99
public class AsyncScoppedClock
1010
{
11+
[Fact]
12+
public void LocalTimeConfiguredStaticProviderNowAlwausReturnsLocalTime()
13+
{
14+
var p = new StaticDateTimeProvider(new DateTime(2000, 01, 01, 1, 2, 3, DateTimeKind.Local));
15+
var localTime = p.Now();
16+
Assert.Equal(DateTimeKind.Local, localTime.Kind);
17+
}
18+
19+
[Fact]
20+
public void UtcTimeConfiguredStaticProviderNowAlwausReturnsLocalTime()
21+
{
22+
var p = new StaticDateTimeProvider(new DateTime(2000, 01, 01, 1, 2, 3, DateTimeKind.Utc));
23+
var localTime = p.Now();
24+
Assert.Equal(DateTimeKind.Local, localTime.Kind);
25+
}
26+
27+
[Fact]
28+
public void LocalTimeConfiguredStaticProviderUtcNowAlwausReturnsUtcTime()
29+
{
30+
var p = new StaticDateTimeProvider(new DateTime(2000, 01, 01, 1, 2, 3, DateTimeKind.Local));
31+
var localTime = p.UtcNow();
32+
Assert.Equal(DateTimeKind.Utc, localTime.Kind);
33+
}
34+
35+
[Fact]
36+
public void UtcTimeConfiguredStaticProviderUtcNowAlwausReturnsUtcTime()
37+
{
38+
var p = new StaticDateTimeProvider(new DateTime(2000, 01, 01, 1, 2, 3, DateTimeKind.Utc));
39+
var localTime = p.UtcNow();
40+
Assert.Equal(DateTimeKind.Utc, localTime.Kind);
41+
}
42+
1143
[Theory]
1244
[InlineData(1)]
1345
[InlineData(100)]
@@ -64,8 +96,6 @@ private async Task RunTest(int count)
6496
// we move away from the pinned after the using statement
6597
Assert.NotEqual(date, Clock.Now);
6698

67-
Clock.DefaultProvider = new UtcCurrentDateTimeProvider();
68-
6999
using (Clock.Pin(new StaticDateTimeProvider(date)))
70100
{
71101
var task1 = DelayedNow(true);
@@ -102,15 +132,15 @@ public async Task PinSubContext()
102132

103133
public async Task<DateTime> DelayedNow(bool continueOnCapturedContext)
104134
{
105-
await Task.Delay(1).ConfigureAwait(continueOnCapturedContext); // to force a propert delay
135+
await Task.Delay(1).ConfigureAwait(continueOnCapturedContext); // to force a proper delay
106136
return Clock.Now;
107137
}
108138

109139
public async Task<DateTime> DelayedDate(DateTime pinnedDate)
110140
{
111141
using (Clock.Pin(new StaticDateTimeProvider(pinnedDate)))
112142
{
113-
await Task.Delay(1); // to force a propert delay
143+
await Task.Delay(1); // to force a proper delay
114144
return Clock.Now;
115145
}
116146
}

Tocsoft.DateTimeAbstractions/Clock.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ public void Dispose()
6565
}
6666

6767
public static DateTime Now => CurrentProvider.Now();
68+
public static DateTime UtcNow => CurrentProvider.UtcNow();
6869
}
6970
}

Tocsoft.DateTimeAbstractions/Providers/CurrentDateTimeProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ namespace Tocsoft.DateTimeAbstractions.Providers
55
public class CurrentDateTimeProvider : DateTimeProvider
66
{
77
public override DateTime Now()
8+
{
9+
return DateTime.Now;
10+
}
11+
12+
public override DateTime UtcNow()
813
{
914
return DateTime.UtcNow;
1015
}

Tocsoft.DateTimeAbstractions/Providers/DateTimeProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ namespace Tocsoft.DateTimeAbstractions.Providers
55
public abstract class DateTimeProvider
66
{
77
public abstract DateTime Now();
8+
public abstract DateTime UtcNow();
89
}
910
}

Tocsoft.DateTimeAbstractions/Providers/StaticDateTimeProvider.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,31 @@ namespace Tocsoft.DateTimeAbstractions.Providers
44
{
55
public class StaticDateTimeProvider : DateTimeProvider
66
{
7+
private readonly DateTime utcdate;
8+
79
private readonly DateTime date;
810

911
public StaticDateTimeProvider(DateTime date)
1012
{
11-
this.date = date;
13+
if (date.Kind == DateTimeKind.Utc)
14+
{
15+
this.utcdate = date;
16+
this.date = date.ToLocalTime();
17+
}
18+
else
19+
{
20+
this.date = date;
21+
this.utcdate = date.ToUniversalTime();
22+
}
1223
}
1324

1425
public override DateTime Now()
1526
{
1627
return date;
1728
}
29+
public override DateTime UtcNow()
30+
{
31+
return utcdate;
32+
}
1833
}
1934
}

Tocsoft.DateTimeAbstractions/Providers/UtcCurrentDateTimeProvider.cs

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

0 commit comments

Comments
 (0)