Skip to content

Commit 5e471d5

Browse files
authored
Version 1.0.5 (#2)
* Add target_commitish to release action for better versioning control (cherry picked from commit d266d06) * Add support for .razor files in GitHub Actions workflow paths (cherry picked from commit 79a33db) * Refactor ILocalTimeService to use SetTimeZoneInfo method for setting time zone information (cherry picked from commit 7b591f3) * Update GetBrowserTimeZone method to check IsTimeZoneInfoAvailable before accessing TimeZoneInfo (cherry picked from commit 7ed2b09) * Add LocalTimeTotal components and tests for time zone handling (cherry picked from commit 292a7a9)
1 parent 6cfb1e4 commit 5e471d5

File tree

8 files changed

+79
-22
lines changed

8 files changed

+79
-22
lines changed

.github/workflows/release-github.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
paths:
1010
- '**.csproj'
1111
- '**.cs'
12+
- '**.razor'
1213
- '**.yaml'
1314

1415
permissions:
@@ -52,6 +53,7 @@ jobs:
5253
with:
5354
name: ${{ steps.nbgv.outputs.SemVer2 }}
5455
tag_name: ${{ steps.nbgv.outputs.SemVer2 }}
56+
target_commitish: ${{ github.ref }}
5557
generate_release_notes: true
5658
files: ./artifacts/*.nupkg
5759
# if hyphen contains in the tag name, it will be prerelease

.github/workflows/test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
paths:
77
- '**.csproj'
88
- '**.cs'
9+
- '**.razor'
910
- '**.yaml'
1011

1112
jobs:

src/BlazorLocalTime/Components/BlazorLocalTimeProvider.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
3030
);
3131
var timeZoneString = await module.InvokeAsync<string>("getBrowserTimeZone");
3232
var timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneString);
33-
LocalTimeService.TimeZoneInfo = timeZone;
33+
LocalTimeService.SetTimeZoneInfo(timeZone);
3434
}
3535
catch (JSDisconnectedException)
3636
{

src/BlazorLocalTime/LocalTimeService.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public interface ILocalTimeService
88
/// <summary>
99
/// Browser's time zone information.
1010
/// </summary>
11-
TimeZoneInfo? TimeZoneInfo { get; set; }
11+
TimeZoneInfo? TimeZoneInfo { get; }
1212

1313
/// <summary>
1414
/// Gets the current browser's local time as a <see cref="DateTimeOffset"/>.
@@ -25,6 +25,12 @@ public interface ILocalTimeService
2525
/// </summary>
2626
public bool IsTimeZoneInfoAvailable => TimeZoneInfo != null;
2727

28+
/// <summary>
29+
/// Sets the browser's time zone information.
30+
/// this method is only for internal use.
31+
/// </summary>
32+
internal void SetTimeZoneInfo(TimeZoneInfo timeZoneInfo);
33+
2834
/// <summary>
2935
/// Converts the specified UTC <see cref="DateTime"/> to local time.
3036
/// </summary>
@@ -71,7 +77,7 @@ public DateTimeOffset ToLocalTimeOffset(DateTimeOffset dateTimeOffset)
7177
/// <returns>The <see cref="TimeZoneInfo"/> representing the browser's time zone.</returns>
7278
public TimeZoneInfo GetBrowserTimeZone()
7379
{
74-
if (TimeZoneInfo == null)
80+
if (TimeZoneInfo == null || !IsTimeZoneInfoAvailable)
7581
{
7682
throw new InvalidOperationException(
7783
"""
@@ -85,7 +91,6 @@ and perform processing after the time zone information has been set.
8591
"""
8692
);
8793
}
88-
8994
return TimeZoneInfo;
9095
}
9196
}
@@ -95,27 +100,24 @@ and perform processing after the time zone information has been set.
95100
/// </summary>
96101
internal class LocalTimeService(TimeProvider timeProvider) : ILocalTimeService
97102
{
98-
private TimeZoneInfo? _timeZoneInfo;
99-
100103
/// <inheritdoc />
101-
public TimeZoneInfo? TimeZoneInfo
102-
{
103-
get => _timeZoneInfo;
104-
set
105-
{
106-
if (_timeZoneInfo != null && _timeZoneInfo.Equals(value))
107-
{
108-
return;
109-
}
110-
_timeZoneInfo = value;
111-
LocalTimeZoneChanged.Invoke(this, EventArgs.Empty);
112-
}
113-
}
104+
public TimeZoneInfo? TimeZoneInfo { get; internal set; }
114105

115106
/// <inheritdoc />
116107
public event EventHandler LocalTimeZoneChanged = delegate { };
117108

118109
/// <inheritdoc />
119110
public DateTimeOffset Now =>
120111
((ILocalTimeService)this).ToLocalTimeOffset(timeProvider.GetUtcNow());
112+
113+
/// <inheritdoc />
114+
public void SetTimeZoneInfo(TimeZoneInfo timeZoneInfo)
115+
{
116+
if (TimeZoneInfo != null && TimeZoneInfo.Equals(timeZoneInfo))
117+
{
118+
return;
119+
}
120+
TimeZoneInfo = timeZoneInfo;
121+
LocalTimeZoneChanged.Invoke(this, EventArgs.Empty);
122+
}
121123
}

tests/BlazorLocalTimeTest/BlazorLocalTimeProviderTest.razor

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@
1111
};
1212

1313
Services.AddScoped<ILocalTimeService, LocalTimeMockService>(_ => tms);
14-
var module = JSInterop.SetupModule("./_content/BlazorLocalTime/Components/BlazorLocalTimeProvider.razor.js");
15-
module.Setup<string>("getBrowserTimeZone").SetResult("Asia/Tokyo");
14+
JavaScriptInitializer(JSInterop);
1615

17-
var cut = Render(
16+
Render(
1817
@<BlazorLocalTimeProvider />
1918
);
2019

2120
// timezone should be set to Asia/Tokyo
2221
tms.TimeZoneInfo!.Id.ShouldBe("Asia/Tokyo");
2322
}
23+
24+
internal static void JavaScriptInitializer(BunitJSInterop jsInterop)
25+
{
26+
var module = jsInterop.SetupModule("./_content/BlazorLocalTime/Components/BlazorLocalTimeProvider.razor.js");
27+
module.Setup<string>("getBrowserTimeZone").SetResult("Asia/Tokyo");
28+
}
2429
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@inject ILocalTimeService LocalTimeService
2+
@inject TimeProvider TimeProvider
3+
4+
<p>
5+
<LocalTimeText Value="TimeProvider.GetUtcNow()" Format="yyyy-MM-dd HH:mm:ss" />
6+
</p>
7+
<BlazorLocalTimeProvider />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@inject ILocalTimeService LocalTimeService
2+
3+
<BlazorLocalTimeProvider />
4+
5+
@code {
6+
protected override void OnInitialized()
7+
{
8+
// that will be failed because the time zone is not available
9+
var _ = LocalTimeService.Now;
10+
}
11+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
@using BlazorLocalTimeTest.Component
2+
@using Shouldly
3+
@inherits TestContext
4+
@code {
5+
[Fact]
6+
public void TestTotal1()
7+
{
8+
var timeProvider = new MockTimeProvider(new(2023, 10, 1, 12, 34, 56, TimeSpan.Zero));
9+
Services.AddBlazorLocalTimeService(timeProvider);
10+
BlazorLocalTimeProviderTest.JavaScriptInitializer(JSInterop);
11+
12+
var cut = RenderComponent<LocalTimeTotal1>();
13+
// 12:34+09:00 -> 2023-10-01 21:34:56
14+
cut.MarkupMatches("<p>2023-10-01 21:34:56</p>");
15+
}
16+
17+
[Fact]
18+
public void TestTotalFailed1()
19+
{
20+
Services.AddBlazorLocalTimeService();
21+
BlazorLocalTimeProviderTest.JavaScriptInitializer(JSInterop);
22+
23+
Should.Throw<InvalidOperationException>(() =>
24+
{
25+
// This test is expected to fail because BlazorLocalTimeProvider is not initialized
26+
RenderComponent<LocalTimeTotalFailed1>();
27+
});
28+
}
29+
}

0 commit comments

Comments
 (0)