forked from Sarah/Lieb-Website
Added TimeZone Functionality
This commit is contained in:
parent
c298f4d20e
commit
c215ed058f
9 changed files with 129 additions and 48 deletions
44
Lieb/Data/TimeZoneService.cs
Normal file
44
Lieb/Data/TimeZoneService.cs
Normal file
|
@ -0,0 +1,44 @@
|
|||
using Microsoft.JSInterop;
|
||||
|
||||
namespace Lieb.Data
|
||||
{
|
||||
public class TimeZoneService
|
||||
{
|
||||
private readonly IJSRuntime _jsRuntime;
|
||||
|
||||
private TimeSpan? _userOffset;
|
||||
private int _offsetInMinutes;
|
||||
|
||||
public TimeZoneService(IJSRuntime jsRuntime)
|
||||
{
|
||||
_jsRuntime = jsRuntime;
|
||||
}
|
||||
|
||||
public async ValueTask<DateTimeOffset> GetLocalDateTime(DateTimeOffset dateTime)
|
||||
{
|
||||
if (_userOffset == null)
|
||||
{
|
||||
_offsetInMinutes = await _jsRuntime.InvokeAsync<int>("GetTimezoneValue");
|
||||
_userOffset = TimeSpan.FromMinutes(-_offsetInMinutes);
|
||||
}
|
||||
|
||||
return dateTime.ToOffset(_userOffset.Value);
|
||||
}
|
||||
|
||||
public async ValueTask<DateTimeOffset> GetUTCDateTime(DateTimeOffset dateTime)
|
||||
{
|
||||
if (_userOffset == null)
|
||||
{
|
||||
_offsetInMinutes = await _jsRuntime.InvokeAsync<int>("GetTimezoneValue");
|
||||
_userOffset = TimeSpan.FromMinutes(-_offsetInMinutes);
|
||||
}
|
||||
|
||||
return new DateTimeOffset(dateTime.DateTime.AddMinutes(_offsetInMinutes), new TimeSpan(0));
|
||||
}
|
||||
|
||||
public async ValueTask<string> GetUserTimeZone()
|
||||
{
|
||||
return await _jsRuntime.InvokeAsync<string>("GetTimezone");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue