Merge pull request #2406 from orionlaw/dev

Make sure Job date times are stored in the database as UTC.
This commit is contained in:
Shaun Walker 2022-09-08 09:14:31 -04:00 committed by GitHub
commit 7158595801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 130 additions and 62 deletions

View File

@ -132,22 +132,10 @@
_isEnabled = job.IsEnabled.ToString(); _isEnabled = job.IsEnabled.ToString();
_interval = job.Interval.ToString(); _interval = job.Interval.ToString();
_frequency = job.Frequency; _frequency = job.Frequency;
_startDate = job.StartDate; (_startDate, _startTime) = Utilities.UtcAsLocalDateAndTime(job.StartDate);
if (job.StartDate != null && job.StartDate.Value.TimeOfDay.TotalSeconds != 0) (_endDate, _endTime) = Utilities.UtcAsLocalDateAndTime(job.EndDate);
{
_startTime = job.StartDate.Value.ToString("HH:mm");
}
_endDate = job.EndDate;
if (job.EndDate != null && job.EndDate.Value.TimeOfDay.TotalSeconds != 0)
{
_endTime = job.EndDate.Value.ToString("HH:mm");
}
_retentionHistory = job.RetentionHistory.ToString(); _retentionHistory = job.RetentionHistory.ToString();
_nextDate = job.NextExecution; (_nextDate, _nextTime) = Utilities.UtcAsLocalDateAndTime(job.NextExecution);
if (job.NextExecution != null && job.NextExecution.Value.TimeOfDay.TotalSeconds != 0)
{
_nextTime = job.NextExecution.Value.ToString("HH:mm");
}
createdby = job.CreatedBy; createdby = job.CreatedBy;
createdon = job.CreatedOn; createdon = job.CreatedOn;
modifiedby = job.ModifiedBy; modifiedby = job.ModifiedBy;
@ -180,50 +168,27 @@
{ {
job.Interval = int.Parse(_interval); job.Interval = int.Parse(_interval);
} }
job.StartDate = _startDate; job.StartDate = Utilities.LocalDateAndTimeAsUtc(_startDate, _startTime);
if (job.StartDate != null) job.EndDate = Utilities.LocalDateAndTimeAsUtc(_endDate, _endTime);
{ job.RetentionHistory = int.Parse(_retentionHistory);
job.StartDate = job.StartDate.Value.Date; job.NextExecution = Utilities.LocalDateAndTimeAsUtc(_nextDate, _nextTime);
if (!string.IsNullOrEmpty(_startTime))
{ try
job.StartDate = DateTime.Parse(job.StartDate.Value.ToShortDateString() + " " + _startTime); {
} job = await JobService.UpdateJobAsync(job);
} await logger.LogInformation("Job Updated {Job}", job);
job.EndDate = _endDate; NavigationManager.NavigateTo(NavigateUrl());
if (job.EndDate != null) }
{ catch (Exception ex)
job.EndDate = job.EndDate.Value.Date; {
if (!string.IsNullOrEmpty(_endTime)) await logger.LogError(ex, "Error Udate Job {Job} {Error}", job, ex.Message);
{ AddModuleMessage(Localizer["Error.Job.Update"], MessageType.Error);
job.EndDate = DateTime.Parse(job.EndDate.Value.ToShortDateString() + " " + _endTime); }
} }
} else
job.RetentionHistory = int.Parse(_retentionHistory); {
job.NextExecution = _nextDate; AddModuleMessage(Localizer["Message.Required.JobInfo"], MessageType.Warning);
if (job.NextExecution != null) }
{ }
job.NextExecution = job.NextExecution.Value.Date;
if (!string.IsNullOrEmpty(_nextTime))
{
job.NextExecution = DateTime.Parse(job.NextExecution.Value.ToShortDateString() + " " + _nextTime);
}
}
try
{
job = await JobService.UpdateJobAsync(job);
await logger.LogInformation("Job Updated {Job}", job);
NavigationManager.NavigateTo(NavigateUrl());
}
catch (Exception ex)
{
await logger.LogError(ex, "Error Udate Job {Job} {Error}", job, ex.Message);
AddModuleMessage(Localizer["Error.Job.Update"], MessageType.Error);
}
}
else
{
AddModuleMessage(Localizer["Message.Required.JobInfo"], MessageType.Warning);
}
}
} }

View File

@ -33,7 +33,7 @@ else
<td>@context.Name</td> <td>@context.Name</td>
<td>@DisplayStatus(context.IsEnabled, context.IsExecuting)</td> <td>@DisplayStatus(context.IsEnabled, context.IsExecuting)</td>
<td>@DisplayFrequency(context.Interval, context.Frequency)</td> <td>@DisplayFrequency(context.Interval, context.Frequency)</td>
<td>@context.NextExecution</td> <td>@context.NextExecution?.ToLocalTime()</td>
<td> <td>
@if (context.IsStarted) @if (context.IsStarted)
{ {

View File

@ -17,7 +17,7 @@ namespace Oqtane.Infrastructure
Name = "Purge Job"; Name = "Purge Job";
Frequency = "d"; // daily Frequency = "d"; // daily
Interval = 1; Interval = 1;
StartDate = DateTime.ParseExact("03:00", "H:mm", null, System.Globalization.DateTimeStyles.None); // 3 AM StartDate = DateTime.ParseExact("03:00", "H:mm", null, System.Globalization.DateTimeStyles.AssumeLocal).ToUniversalTime(); // 3 AM
IsEnabled = true; IsEnabled = true;
} }

View File

@ -446,5 +446,50 @@ namespace Oqtane.Shared
{ {
return $"[{@class.GetType()}] {message}"; return $"[{@class.GetType()}] {message}";
} }
public static DateTime? LocalDateAndTimeAsUtc(DateTime? date, string time, TimeZoneInfo localTimeZone = null)
{
localTimeZone ??= TimeZoneInfo.Local;
if (date != null)
{
if (!string.IsNullOrEmpty(time))
{
return TimeZoneInfo.ConvertTime(DateTime.Parse(date.Value.Date.ToShortDateString() + " " + time), localTimeZone, TimeZoneInfo.Utc);
}
return TimeZoneInfo.ConvertTime(date.Value.Date, localTimeZone, TimeZoneInfo.Utc);
}
return null;
}
public static (DateTime? date, string time) UtcAsLocalDateAndTime(DateTime? dateTime, TimeZoneInfo timeZone = null)
{
timeZone ??= TimeZoneInfo.Local;
DateTime? localDateTime = null;
string localTime = string.Empty;
if (dateTime.HasValue && dateTime?.Kind != DateTimeKind.Local)
{
if (dateTime?.Kind == DateTimeKind.Unspecified)
{
// Treat Unspecified as Utc not Local. This is due to EF Core, on some databases, after retrieval will have DateTimeKind as Unspecified.
// All values in database should be UTC.
// Normal .net conversion treats Unspecified as local.
// https://docs.microsoft.com/en-us/dotnet/api/system.timezoneinfo.converttime?view=net-6.0
localDateTime = TimeZoneInfo.ConvertTime(new DateTime(dateTime.Value.Ticks, DateTimeKind.Utc), timeZone);
}
else
{
localDateTime = TimeZoneInfo.ConvertTime(dateTime.Value, timeZone);
}
}
if (localDateTime != null && localDateTime.Value.TimeOfDay.TotalSeconds != 0)
{
localTime = localDateTime.Value.ToString("HH:mm");
}
return (localDateTime?.Date, localTime);
}
} }
} }

View File

@ -1,3 +1,5 @@
using System;
using System.Globalization;
using Oqtane.Shared; using Oqtane.Shared;
using Xunit; using Xunit;
@ -27,5 +29,61 @@ namespace Oqtane.Test.Oqtane.Shared.Tests
// Assert // Assert
Assert.Equal(expectedUrl, navigatedUrl); Assert.Equal(expectedUrl, navigatedUrl);
} }
[Theory]
[InlineData(2022, 02, 01, "21:00", "Eastern Standard Time", 2022, 2, 2, 2)]
[InlineData(2022, 02, 02, "15:00", "Eastern Standard Time", 2022, 2, 2, 20)]
[InlineData(2022, 02, 02, "", "Eastern Standard Time", 2022, 2, 2, 5)]
[InlineData(0, 0, 0, "", "Eastern Standard Time", 0, 0, 0, 0)]
public void LocalDateAndTimeAsUtcTest(int yr, int mo, int day, string timeString, string zone, int yrUtc, int moUtc, int dayUtc, int hrUtc)
{
// Arrange
DateTime? srcDate = null;
if (yr > 0)
{
srcDate = new DateTime(yr, mo, day);
}
// Act
var dateTime = Utilities.LocalDateAndTimeAsUtc(srcDate, timeString, TimeZoneInfo.FindSystemTimeZoneById(zone));
// Assert
DateTime? expected = null;
if (yrUtc > 0)
{
expected = new DateTime(yrUtc, moUtc, dayUtc, hrUtc, 0, 0, DateTimeKind.Utc);
}
Assert.Equal(expected, dateTime);
}
[Theory]
// Standard Time
[InlineData(2022, 2, 2, 2, DateTimeKind.Unspecified, "Eastern Standard Time", "2022/02/01", "21:00")]
[InlineData(2022, 2, 2, 2, DateTimeKind.Utc, "Eastern Standard Time", "2022/02/01", "21:00")]
[InlineData(2022, 2, 2, 20, DateTimeKind.Unspecified, "Eastern Standard Time", "2022/02/02", "15:00")]
[InlineData(2022, 2, 2, 20, DateTimeKind.Utc, "Eastern Standard Time", "2022/02/02", "15:00")]
[InlineData(2022, 2, 2, 5, DateTimeKind.Unspecified, "Eastern Standard Time", "2022/02/02", "")]
[InlineData(2022, 2, 2, 5, DateTimeKind.Utc, "Eastern Standard Time", "2022/02/02", "")]
// Daylight Savings Time
[InlineData(2022, 7, 2, 20, DateTimeKind.Unspecified, "Eastern Standard Time", "2022/07/02", "16:00")]
[InlineData(2022, 7, 2, 20, DateTimeKind.Utc, "Eastern Standard Time", "2022/07/02", "16:00")]
[InlineData(2022, 7, 2, 4, DateTimeKind.Unspecified, "Eastern Standard Time", "2022/07/02", "")]
[InlineData(2022, 7, 2, 4, DateTimeKind.Utc, "Eastern Standard Time", "2022/07/02", "")]
public void UtcAsLocalDateAndTimeTest(int yr, int mo, int day, int hr, DateTimeKind dateTimeKind, string zone, string expectedDate, string expectedTime)
{
// Arrange
DateTime? srcDate = null;
if (yr > 0)
{
srcDate = new DateTime(yr, mo, day, hr, 0, 0, dateTimeKind);
}
// Act
var dateAndTime = Utilities.UtcAsLocalDateAndTime(srcDate, TimeZoneInfo.FindSystemTimeZoneById(zone));
// Assert
Assert.Equal(expectedDate, dateAndTime.date.Value.ToString("yyyy/MM/dd"));
Assert.Equal(expectedTime, dateAndTime.time);
}
} }
} }