Skip to content

Commit

Permalink
Issue-42: Term program UTC date fix (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
NomisNostab authored May 1, 2023
2 parents 7d01bb9 + d44263f commit 69bdada
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions Model/ReportGeneration/ReportGenerationRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ReportGenerationRequest
public string EventName { get; set; } = string.Empty;
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
public TimeSpan CurrentUtcOffset { get; set; } = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
public bool BreakByPatrol { get; set; } = false;
public bool GroupByMember { get; set; } = false;
public bool FormatLikeTerrain { get; set; } = false;
Expand Down
27 changes: 23 additions & 4 deletions Services/ReportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Topo.Model.Wallchart;
using Topo.Model.Progress;
using Syncfusion.EJ2.Spreadsheet;
using System;

namespace Topo.Services
{
Expand All @@ -36,6 +37,8 @@ public interface IReportService
}
public class ReportService : IReportService
{
public TimeSpan CurrentUtcOffset { get; set; } = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);

private enum participateAssistLead
{
participate,
Expand Down Expand Up @@ -112,8 +115,11 @@ private enum wallchartGroups
Color.FromArgb(156, 242, 80)
};

private bool RunningInUtc { get; set; }

public ReportService()
{
RunningInUtc = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now) == TimeSpan.Zero;
}

public IWorkbook CreateWorkbookWithSheets(int sheetsToCreate)
Expand Down Expand Up @@ -2347,6 +2353,14 @@ public IWorkbook GenerateTermProgramWorkbook(List<EventListModel> eventEntries,
var currentMonth = 0;
foreach (var eventEntry in eventEntries)
{
if (RunningInUtc)
{
// Adjust time to users local time.
// To Do: Check for daylight savings correction
eventEntry.StartDateTime = ConvertToClientLocalTime(eventEntry.StartDateTime);
eventEntry.EndDateTime = ConvertToClientLocalTime(eventEntry.EndDateTime);
}

if (currentMonth != eventEntry.StartDateTime.Month)
{
rowNumber++;
Expand All @@ -2366,13 +2380,13 @@ public IWorkbook GenerateTermProgramWorkbook(List<EventListModel> eventEntries,
sheet.Range[rowNumber, columnNumber].BorderAround();
sheet.Range[rowNumber, columnNumber].NumberFormat = "dd/MM/yy HH:mm";
columnNumber++;
sheet.Range[rowNumber, columnNumber].Text = formatEventDate(eventEntry.StartDateTime, eventEntry.EndDateTime);
sheet.Range[rowNumber, columnNumber].Text = FormatEventDate(eventEntry.StartDateTime, eventEntry.EndDateTime);
sheet.Range[rowNumber, columnNumber].BorderAround();
columnNumber++;
sheet.Range[rowNumber, columnNumber].Text = "";
sheet.Range[rowNumber, columnNumber].BorderAround();
columnNumber++;
sheet.Range[rowNumber, columnNumber].Text = formatEventTime(eventEntry.StartDateTime, eventEntry.EndDateTime);
sheet.Range[rowNumber, columnNumber].Text = FormatEventTime(eventEntry.StartDateTime, eventEntry.EndDateTime);
sheet.Range[rowNumber, columnNumber].BorderAround();
columnNumber++;
sheet.Range[rowNumber, columnNumber].Text = eventEntry.Location;
Expand Down Expand Up @@ -2429,15 +2443,20 @@ public IWorkbook GenerateTermProgramWorkbook(List<EventListModel> eventEntries,

}

private string formatEventDate (DateTime startDateTime, DateTime endDateTime)
private DateTime ConvertToClientLocalTime(DateTime dateTime)
{
return dateTime.AddHours(CurrentUtcOffset.Hours).AddMinutes(CurrentUtcOffset.Minutes);
}

private string FormatEventDate (DateTime startDateTime, DateTime endDateTime)
{
if (startDateTime.Date == endDateTime.Date)
return startDateTime.ToString("ddd d");
else
return $"{startDateTime.ToString("ddd d")} - {endDateTime.ToString("ddd d")}";
}

private string formatEventTime(DateTime startDateTime, DateTime endDateTime)
private string FormatEventTime(DateTime startDateTime, DateTime endDateTime)
{
if (startDateTime.Date == endDateTime.Date)
return $"{startDateTime.ToShortTimeString()} - {endDateTime.ToShortTimeString()}";
Expand Down
10 changes: 8 additions & 2 deletions Topo/Services/ReportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public async Task<byte[]> GetAdditionalAwardsReport(string groupName, string sec

public async Task<byte[]> GetApprovalsReport(string groupName, string section, string unitName, OutputType outputType, string serialisedReportData, DateTime fromDate, DateTime toDate, bool groupByMember)
{
var reportGenerationRequest = new ReportGenerationRequest()
var reportGenerationRequest = new ReportGenerationRequest()
{
ReportType = ReportType.Approvals,
GroupName = groupName,
Expand Down Expand Up @@ -283,7 +283,13 @@ public async Task<byte[]> GetTermProgramReport(string groupName, string section,

private async Task<byte[]> CallReportGeneratorFunction(ReportGenerationRequest reportGenerationRequest)
{
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Put, "https://gkgfntjuhcuc3qtjmohaaxigwe0uevae.lambda-url.ap-southeast-2.on.aws/");
#if DEBUG
string functionUrl = "https://57aqgtwtgggsk47rtvpgqifima0tvhwy.lambda-url.ap-southeast-2.on.aws/";
#else
string functionUrl = "https://qwhcdbhrempok4kpmk6utzavxq0zjzha.lambda-url.ap-southeast-2.on.aws/";
#endif
string functionUrlLatest = "https://gkgfntjuhcuc3qtjmohaaxigwe0uevae.lambda-url.ap-southeast-2.on.aws/";
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Put, functionUrl);
var content = JsonConvert.SerializeObject(reportGenerationRequest);
httpRequest.Content = new StringContent(content, Encoding.UTF8, "application/json");
httpRequest.Headers.Add("accept", "application/json, text/plain, */*");
Expand Down
3 changes: 2 additions & 1 deletion TopoReportFunction/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public string FunctionHandler(APIGatewayProxyRequest request, ILambdaContext con
{
try
{
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("en-AU");
CultureInfo cultureInfo = new("en-AU");
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

Expand All @@ -49,6 +49,7 @@ public string FunctionHandler(APIGatewayProxyRequest request, ILambdaContext con
if (reportGenerationRequest != null)
{
var workbook = reportService.CreateWorkbookWithSheets(1);
reportService.CurrentUtcOffset = reportGenerationRequest.CurrentUtcOffset;
switch (reportGenerationRequest.ReportType)
{
case ReportType.MemberList:
Expand Down

0 comments on commit 69bdada

Please sign in to comment.