Skip to content

Commit c373338

Browse files
authored
Merge pull request #2792 from TechnologyEnhancedLearning/Develop/Fix/TD-4436-Enrolment-dateTime-issue
TD-4436- Converted utc date to local date time using NodaTime package
2 parents b31623d + 3ad42ef commit c373338

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

DigitalLearningSolutions.Web/DigitalLearningSolutions.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
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" />

DigitalLearningSolutions.Web/Helpers/DateHelper.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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
}

0 commit comments

Comments
 (0)