Skip to content

Commit e9667e3

Browse files
authored
Add nullability annotations and simplify time zone logic (#17)
* Add nullability annotation for IsTimeZoneInfoAvailable and simplify GetBrowserTimeZone logic * Fix component name in error message for browser time zone information retrieval
1 parent d03cc09 commit e9667e3

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/BlazorLocalTime/LocalTimeService.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace BlazorLocalTime;
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace BlazorLocalTime;
24

35
/// <summary>
46
/// Provides an interface for a local time service.
@@ -33,6 +35,7 @@ public interface ILocalTimeService
3335
/// <summary>
3436
/// Is the local time zone set?
3537
/// </summary>
38+
[MemberNotNullWhen(true, nameof(TimeZoneInfo))]
3639
public bool IsTimeZoneInfoAvailable => TimeZoneInfo != null;
3740

3841
/// <summary>
@@ -94,14 +97,14 @@ public DateTimeOffset ToLocalTimeOffset(DateTimeOffset dateTimeOffset)
9497
/// <returns>The <see cref="TimeZoneInfo"/> representing the browser's time zone.</returns>
9598
public TimeZoneInfo GetBrowserTimeZone()
9699
{
97-
if (TimeZoneInfo == null || !IsTimeZoneInfoAvailable)
100+
if (!IsTimeZoneInfoAvailable)
98101
{
99102
throw new InvalidOperationException(
100103
"""
101104
Failed to obtain the browser's time zone information.
102105
Possible causes:
103-
1) The `<BrowserLocalTimeProvider />` component has not been added.
104-
In this case, please add `<BrowserLocalTimeProvider />` to a root component such as `Routes.razor`.
106+
1) The `<BlazorLocalTimeProvider />` component has not been added.
107+
In this case, please add `<BlazorLocalTimeProvider />` to a root component such as `Routes.razor`.
105108
2) You are trying to use `ILocalTimeService` in `OnInitialized(Async)`.
106109
In this case, you need to subscribe to the `ILocalTimeService.OnLocalTimeZoneChanged` event
107110
and perform processing after the time zone information has been set.

tests/BlazorLocalTimeTest/Approvals/PublicApiCheckTest.Run.net8.approved.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace BlazorLocalTime
1212
public interface ILocalTimeService
1313
{
1414
System.TimeZoneInfo? BrowserTimeZoneInfo { get; }
15+
[System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, "TimeZoneInfo")]
16+
[get: System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, "TimeZoneInfo")]
1517
bool IsTimeZoneInfoAvailable { get; }
1618
System.DateTimeOffset Now { get; }
1719
System.TimeZoneInfo? OverrideTimeZoneInfo { get; set; }

tests/BlazorLocalTimeTest/Approvals/PublicApiCheckTest.Run.net9.approved.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[assembly: System.Reflection.AssemblyMetadata("RepositoryUrl", "https://github.com/arika0093/BlazorLocalTime")]
1+
[assembly: System.Reflection.AssemblyMetadata("RepositoryUrl", "https://github.com/arika0093/BlazorLocalTime")]
22
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("BlazorLocalTimeTest")]
33
[assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v9.0", FrameworkDisplayName=".NET 9.0")]
44
namespace BlazorLocalTime
@@ -12,6 +12,8 @@ namespace BlazorLocalTime
1212
public interface ILocalTimeService
1313
{
1414
System.TimeZoneInfo? BrowserTimeZoneInfo { get; }
15+
[System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, "TimeZoneInfo")]
16+
[get: System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, "TimeZoneInfo")]
1517
bool IsTimeZoneInfoAvailable { get; }
1618
System.DateTimeOffset Now { get; }
1719
System.TimeZoneInfo? OverrideTimeZoneInfo { get; set; }

0 commit comments

Comments
 (0)