diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-10-30 16:40:03 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-10-30 16:40:03 +0100 |
| commit | 0725e4f7cf4c6f723264b6d461b91c660d144cb7 (patch) | |
| tree | aae5876b5760c80679161d918c34d753ec0e2582 /code/api/src/Endpoints/EndpointBase.cs | |
| parent | d76c180c9631df015d37138045c79a46cca350e8 (diff) | |
| download | greatoffice-0725e4f7cf4c6f723264b6d461b91c660d144cb7.tar.xz greatoffice-0725e4f7cf4c6f723264b6d461b91c660d144cb7.zip | |
feat: Apiwork
Diffstat (limited to 'code/api/src/Endpoints/EndpointBase.cs')
| -rw-r--r-- | code/api/src/Endpoints/EndpointBase.cs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/code/api/src/Endpoints/EndpointBase.cs b/code/api/src/Endpoints/EndpointBase.cs index de5d967..a5b0931 100644 --- a/code/api/src/Endpoints/EndpointBase.cs +++ b/code/api/src/Endpoints/EndpointBase.cs @@ -1,3 +1,5 @@ +using ILogger = Microsoft.Extensions.Logging.ILogger; + namespace IOL.GreatOffice.Api.Endpoints; [ApiController] @@ -20,4 +22,32 @@ public class EndpointBase : ControllerBase TraceId = HttpContext.TraceIdentifier }); } + + [NonAction] + protected RequestTimeZoneInfo GetRequestTimeZone(ILogger logger = default) { + Request.Headers.TryGetValue(AppHeaders.BROWSER_TIME_ZONE, out var timeZoneHeader); + var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZoneHeader.ToString().HasValue() ? timeZoneHeader.ToString() : "UTC"); + var offset = tz.BaseUtcOffset.Hours; + + // This is fine as long as the client is not connecting from Australia: Lord Howe Island, + // according to https://en.wikipedia.org/wiki/Daylight_saving_time_by_country + if (tz.IsDaylightSavingTime(AppDateTime.UtcNow)) { + offset++; + } + + logger?.LogInformation("Request time zone (" + tz.Id + ") offset is: " + offset + " hours"); + + return new RequestTimeZoneInfo() { + TimeZoneInfo = tz, + Offset = offset, + LocalDateTime = TimeZoneInfo.ConvertTimeFromUtc(AppDateTime.UtcNow, tz) + }; + } + + public class RequestTimeZoneInfo + { + public TimeZoneInfo TimeZoneInfo { get; set; } + public int Offset { get; set; } + public DateTime LocalDateTime { get; set; } + } }
\ No newline at end of file |
