reworked GetTimeZoneValue to ust the date to determine daylight saving times

This commit is contained in:
t.ruspekhofer 2022-03-12 23:00:37 +01:00
parent 35a1ff111b
commit 2cf4018e33
2 changed files with 9 additions and 17 deletions

View file

@ -6,9 +6,6 @@ namespace Lieb.Data
{ {
private readonly IJSRuntime _jsRuntime; private readonly IJSRuntime _jsRuntime;
private TimeSpan? _userOffset;
private int _offsetInMinutes;
public TimeZoneService(IJSRuntime jsRuntime) public TimeZoneService(IJSRuntime jsRuntime)
{ {
_jsRuntime = jsRuntime; _jsRuntime = jsRuntime;
@ -16,24 +13,18 @@ namespace Lieb.Data
public async ValueTask<DateTimeOffset> GetLocalDateTime(DateTimeOffset dateTime) public async ValueTask<DateTimeOffset> GetLocalDateTime(DateTimeOffset dateTime)
{ {
if (_userOffset == null) int offsetInMinutes = await _jsRuntime.InvokeAsync<int>("GetTimezoneValue", dateTime);
{ TimeSpan userOffset = TimeSpan.FromMinutes(-offsetInMinutes);
_offsetInMinutes = await _jsRuntime.InvokeAsync<int>("GetTimezoneValue");
_userOffset = TimeSpan.FromMinutes(-_offsetInMinutes);
}
return dateTime.ToOffset(_userOffset.Value); return dateTime.ToOffset(userOffset);
} }
public async ValueTask<DateTimeOffset> GetUTCDateTime(DateTimeOffset dateTime) public async ValueTask<DateTimeOffset> GetUTCDateTime(DateTimeOffset dateTime)
{ {
if (_userOffset == null) int offsetInMinutes = await _jsRuntime.InvokeAsync<int>("GetTimezoneValue", dateTime);
{ TimeSpan userOffset = TimeSpan.FromMinutes(-offsetInMinutes);
_offsetInMinutes = await _jsRuntime.InvokeAsync<int>("GetTimezoneValue");
_userOffset = TimeSpan.FromMinutes(-_offsetInMinutes);
}
return new DateTimeOffset(dateTime.DateTime.AddMinutes(_offsetInMinutes), new TimeSpan(0)); return new DateTimeOffset(dateTime.DateTime.AddMinutes(offsetInMinutes), new TimeSpan(0));
} }
public async ValueTask<string> GetUserTimeZone() public async ValueTask<string> GetUserTimeZone()

View file

@ -8,9 +8,10 @@
@(await Html.RenderComponentAsync<App>(RenderMode.Server)) @(await Html.RenderComponentAsync<App>(RenderMode.Server))
<!--<component type="typeof(App)" render-mode="ServerPrerendered" />--> <!--<component type="typeof(App)" render-mode="ServerPrerendered" />-->
<script> <script>
function GetTimezoneValue() { function GetTimezoneValue(dateTime) {
// Returns the time difference in minutes between UTC time and local time. // Returns the time difference in minutes between UTC time and local time.
return new Date().getTimezoneOffset(); date = new Date(dateTime);
return date.getTimezoneOffset();
} }
</script> </script>
<script> <script>