Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasnordqvist committed Jun 17, 2024
1 parent 9816e7d commit 13d6593
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 7 deletions.
73 changes: 66 additions & 7 deletions TimeKeeping/DotNetThoughts.TimeKeeping.App/Pages/Timer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,60 @@

<PageTitle>Timing</PageTitle>

<h1>System Time</h1>
<p role="status">@systemTime.ToString("O")</p>
<h1>UTC Real Time</h1>
<p role="status">@realTime.ToString("O")</p>
<div>
<button class="btn btn-primary" @onclick="SetBaseLine">Baseline 2021-01-01</button>
<button class="btn btn-primary" @onclick="FreezeAbsolute">Freeze 2021-01-01</button>
<button class="btn btn-primary" @onclick="AdvanceOneHour">Advance 1 Hour</button>
<button class="btn btn-primary" @onclick="SubtractOneHour">Subtract 1 Hour</button>
<button class="btn btn-primary" @onclick="Freeze">Freeze</button>
<button class="btn btn-primary" disabled="@IsNotFrozen()" @onclick="Thaw">Thaw</button>
<button class="btn btn-primary" @onclick="Reset">Reset</button>
<h1>Real Time</h1>
<p role="status">@realTime.ToString("O")</p>
</div>
<div>
<button class="btn btn-primary" @onclick="AdvanceOneHour">Advance 1 Hour</button>
<button class="btn btn-primary" @onclick="AdvanceOneDay">Advance 1 Day</button>
<button class="btn btn-primary" @onclick="AdvanceOneWeek">Advance 1 Week</button>
</div>
<div>
<button class="btn btn-primary" @onclick="SubtractOneHour">Subtract 1 Hour</button>
<button class="btn btn-primary" @onclick="SubtractOneDay">Subtract 1 Day</button>
<button class="btn btn-primary" @onclick="SubtractOneWeek">Subtract 1 Week</button>
</div>

<h1>UTC System Time</h1>
<p role="status">@systemTime.ToString("O")</p>

<h1>Local System Time</h1>
<p role="status">@localSystemTime.ToString("O")</p>

<h1>
<select value="@selectedTimeZone" @onchange=OnTimeZoneChange>
@foreach (var tz in TimeZoneInfo.GetSystemTimeZones())
{
<option value="@tz.Id">@tz.Id Local System Time</option>
}
</select>
</h1>
<p role="status">@nairobiLocalSystemTime.ToString("O")</p>


@code {
private int currentCount = 0;
private TimeTravelersClock clock = new TimeTravelersClock();
private DateTimeOffset systemTime = DateTimeOffset.UtcNow;
private DateTimeOffset realTime = DateTimeOffset.UtcNow;
private DateTimeOffset systemTime = DateTimeOffset.UtcNow;
private DateTimeOffset localSystemTime = DateTimeOffset.UtcNow;
private DateTimeOffset nairobiLocalSystemTime = DateTimeOffset.UtcNow;
private string selectedTimeZone = TimeZoneInfo.GetSystemTimeZones().First().Id;
System.Threading.Timer? timer;

private void UpdateUI()
{
systemTime = clock.UtcNow();
realTime = DateTimeOffset.UtcNow;
localSystemTime = new LocalDateTime(systemTime.DateTime, TimeZoneInfo.Utc).ToTimeZone(TimeZoneInfo.Local).ToDateTimeOffsetLocal();
nairobiLocalSystemTime = new LocalDateTime(systemTime.DateTime, TimeZoneInfo.Utc).ToTimeZone(TimeZoneInfo.FindSystemTimeZoneById(selectedTimeZone)).ToDateTimeOffsetLocal();

}
protected override void OnInitialized()
{
Expand All @@ -38,18 +67,48 @@
await InvokeAsync(StateHasChanged);
}, null, 0, 10);
}

private void OnTimeZoneChange(ChangeEventArgs args)
{
selectedTimeZone = ((string)args.Value);
}
private void AdvanceOneHour()
{
clock.Advance(TimeSpan.FromHours(1));
UpdateUI();

}

private void AdvanceOneDay()
{
clock.Advance(TimeSpan.FromDays(1));
UpdateUI();

}

private void AdvanceOneWeek()
{
clock.Advance(TimeSpan.FromDays(7));
UpdateUI();

}

private void SubtractOneHour()
{
clock.Advance(TimeSpan.FromHours(-1));
UpdateUI();
}

private void SubtractOneDay()
{
clock.Advance(TimeSpan.FromDays(-1));
UpdateUI();
}

private void SubtractOneWeek()
{
clock.Advance(TimeSpan.FromDays(-7));
UpdateUI();
}

private void Freeze()
Expand Down
45 changes: 45 additions & 0 deletions TimeKeeping/DotNetThoughts.TimeKeeping.Tests/LocalDateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,51 @@ public void TestLocalDateTimeOffset()
ldt.ToDateTimeOffsetLocal().Should().Be(expectedDateTimeOffset);
}

[Fact]
public void ConvertBetweenTimeZones_Utc()
{
var t = new DateTime(2024, 06, 06, 0, 0, 0, DateTimeKind.Unspecified);
var tz = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var ldt = new LocalDateTime(t, tz);
var utc = ldt.ToTimeZone(TimeZoneInfo.Utc);
utc.DateTime.Kind.Should().Be(DateTimeKind.Utc);
utc.ToDateTimeOffsetUtc().Should().Be(ldt.ToDateTimeOffsetUtc());
}

[Fact]
public void UtcIsLocalUtc()
{
var t = new DateTime(2024, 06, 06, 0, 0, 0, DateTimeKind.Utc);
var tz = TimeZoneInfo.FindSystemTimeZoneById("UTC");
var ldt = new LocalDateTime(t, tz);
ldt.ToDateTimeOffsetLocal().Should().Be(ldt.ToDateTimeOffsetUtc());
}

[Fact]
public void ConvertBetweenTimeZones_Hawaii1()
{
var t = new DateTime(2024, 06, 17, 22, 24, 0, DateTimeKind.Unspecified);
var tz = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var ldt = new LocalDateTime(t, tz);
var hawaii = ldt.ToTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time"));
hawaii.DateTime.Kind.Should().Be(DateTimeKind.Unspecified);
hawaii.ToDateTimeOffsetUtc().Should().Be(ldt.ToDateTimeOffsetUtc());
hawaii.ToDateTimeOffsetLocal().ToString("yyyy-MM-ddTHH:mm:ssK").Should().Be("2024-06-17T10:24:00-10:00");
}

[Fact]
public void ConvertBetweenTimeZones_Hawaii2()
{
var t = new DateTime(2024, 02, 17, 22, 24, 0, DateTimeKind.Unspecified);
var tz = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var ldt = new LocalDateTime(t, tz);
var hawaii = ldt.ToTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time"));
hawaii.DateTime.Kind.Should().Be(DateTimeKind.Unspecified);
hawaii.ToDateTimeOffsetUtc().Should().Be(ldt.ToDateTimeOffsetUtc());
hawaii.ToDateTimeOffsetLocal().ToString("yyyy-MM-ddTHH:mm:ssK").Should().Be("2024-02-17T11:24:00-10:00");
}

//
[Fact]
public void TestDateConstructor()
{
Expand Down
5 changes: 5 additions & 0 deletions TimeKeeping/DotNetThoughts.TimeKeeping/LocalDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public LocalDateTime(DateTime dateTime, TimeZoneInfo timeZoneInfo)
public DateTime DateTime { get; }

Check warning on line 34 in TimeKeeping/DotNetThoughts.TimeKeeping/LocalDateTime.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Missing XML comment for publicly visible type or member 'LocalDateTime.DateTime'
public TimeZoneInfo TimeZoneInfo { get; }

Check warning on line 35 in TimeKeeping/DotNetThoughts.TimeKeeping/LocalDateTime.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Missing XML comment for publicly visible type or member 'LocalDateTime.TimeZoneInfo'

public LocalDateTime ToTimeZone(TimeZoneInfo newTz)

Check warning on line 37 in TimeKeeping/DotNetThoughts.TimeKeeping/LocalDateTime.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Missing XML comment for publicly visible type or member 'LocalDateTime.ToTimeZone(TimeZoneInfo)'
{
return new LocalDateTime(TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo, newTz), newTz);
}

public LocalDateTime ToMidnight()

Check warning on line 42 in TimeKeeping/DotNetThoughts.TimeKeeping/LocalDateTime.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Missing XML comment for publicly visible type or member 'LocalDateTime.ToMidnight()'
{
return new LocalDateTime(DateTime.Date, TimeZoneInfo);
Expand Down

0 comments on commit 13d6593

Please sign in to comment.