Skip to content

Commit 14649ba

Browse files
authored
LocalTimeForm bugfix (#4)
1 parent 0a1ec58 commit 14649ba

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

src/BlazorLocalTime/Components/LocalTimeForm.razor.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,31 @@ public void Dispose()
4343

4444
private async void OnLocalTimeZoneChangedDetailed(object? sender, TimeZoneChangedEventArgs e)
4545
{
46+
var prevTimeZone = e.PreviousTimeZone ?? LocalTimeService.BrowserTimeZoneInfo;
47+
var currTimeZone = e.CurrentTimeZone ?? LocalTimeService.BrowserTimeZoneInfo;
48+
if (currTimeZone == null)
49+
{
50+
// If current timezone is null, we cannot proceed with the conversion.
51+
// This can happen if the browser failed time zone detection.
52+
return;
53+
}
54+
if (prevTimeZone == null)
55+
{
56+
// timezone is available now
57+
await InvokeAsync(StateHasChanged);
58+
return;
59+
}
60+
if (prevTimeZone.Equals(currTimeZone))
61+
{
62+
// If the time zones are the same, no need to change the value.
63+
return;
64+
}
65+
4666
// When timezone changes, preserve the UI input value by recalculating the UTC Value
4767
// based on the current local time displayed in the UI
4868
var currentValue = ValueAsDateTimeOffset;
4969
if (currentValue.HasValue && ValueChanged.HasDelegate)
5070
{
51-
var prevTimeZone = e.PreviousTimeZone ?? LocalTimeService.BrowserTimeZoneInfo;
52-
var currTimeZone = e.CurrentTimeZone ?? LocalTimeService.BrowserTimeZoneInfo;
53-
if (prevTimeZone == null || currTimeZone == null)
54-
{
55-
// If either previous or current timezone is null, we cannot proceed with the conversion.
56-
// This can happen if the browser failed time zone detection.
57-
return;
58-
}
59-
if (prevTimeZone.Equals(currTimeZone))
60-
{
61-
// If the time zones are the same, no need to change the value.
62-
return;
63-
}
6471
// Convert the current UTC value to the new local time based on the new time zone.
6572
var currentUtcVal = currentValue.Value.ToUniversalTime();
6673
var prevOffset = prevTimeZone.GetUtcOffset(currentValue.Value);

0 commit comments

Comments
 (0)