File tree Expand file tree Collapse file tree 2 files changed +20
-11
lines changed
DigitalLearningSolutions.Web Expand file tree Collapse file tree 2 files changed +20
-11
lines changed Original file line number Diff line number Diff line change 8181 <PackageReference Include =" Microsoft.VisualStudio.Web.BrowserLink" Version =" 2.2.0" />
8282 <PackageReference Include =" Microsoft.VisualStudio.Web.CodeGeneration.Design" Version =" 6.0.16" />
8383 <PackageReference Include =" NHSUKViewComponents.Web" Version =" 1.0.24" />
84+ <PackageReference Include =" NodaTime" Version =" 3.1.11" />
8485 <PackageReference Include =" Npm" Version =" 3.5.2" />
8586 <PackageReference Include =" Serilog.AspNetCore" Version =" 7.0.0" />
8687 <PackageReference Include =" Serilog.Sinks.MSSqlServer" Version =" 6.3.0" />
Original file line number Diff line number Diff line change 22{
33 using DigitalLearningSolutions . Data . Enums ;
44 using Microsoft . AspNetCore . Http ;
5+ using NodaTime ;
56 using System ;
67
78 public static class DateHelper
@@ -36,21 +37,28 @@ public static string GetFormatStringForDateInTable(ReportInterval interval)
3637 } ;
3738 }
3839
39- public static DateTime ? GetLocalDateTime ( DateTime ? dateUtc )
40+ public static DateTime ? GetLocalDateTime ( DateTime ? utcDateTime )
4041 {
41- if ( dateUtc == null )
42+ if ( utcDateTime == null )
4243 return null ;
44+ try
45+ {
46+ var accessor = new HttpContextAccessor ( ) ;
47+ var ianaTimeZoneId = accessor . HttpContext . User . GetUserTimeZone ( CustomClaimTypes . UserTimeZone ) ;
4348
44- var accessor = new HttpContextAccessor ( ) ;
45- var timeZone = accessor . HttpContext . User . GetUserTimeZone ( CustomClaimTypes . UserTimeZone ) ;
46-
47- if ( string . IsNullOrEmpty ( timeZone ) )
48- timeZone = DefaultTimeZone ;
49-
50- TimeZoneInfo timeZoneInfo = TimeZoneInfo . FindSystemTimeZoneById ( timeZone ) ;
51- DateTime localDateTime = TimeZoneInfo . ConvertTimeFromUtc ( ( DateTime ) dateUtc , timeZoneInfo ) ;
49+ var timeZoneProvider = DateTimeZoneProviders . Tzdb ;
50+ var dateTimeZone = timeZoneProvider . GetZoneOrNull ( ianaTimeZoneId ) ;
51+ var instant = Instant . FromDateTimeUtc ( DateTime . SpecifyKind ( ( DateTime ) utcDateTime , DateTimeKind . Utc ) ) ;
52+ var userZonedDateTime = instant . InZone ( dateTimeZone ) ;
53+ var userLocalDateTime = userZonedDateTime . ToDateTimeUnspecified ( ) ;
5254
53- return localDateTime ;
55+ return userLocalDateTime ;
56+ }
57+ catch ( Exception )
58+ {
59+ return utcDateTime ;
60+ }
61+
5462 }
5563 }
5664}
You can’t perform that action at this time.
0 commit comments