Override and 2 new functions

This commit is contained in:
Leigh Pointer 2023-08-25 12:09:16 +02:00
parent 073b10929a
commit 96480b4382
2 changed files with 54 additions and 11 deletions

View File

@ -132,10 +132,13 @@
_isEnabled = job.IsEnabled.ToString(); _isEnabled = job.IsEnabled.ToString();
_interval = job.Interval.ToString(); _interval = job.Interval.ToString();
_frequency = job.Frequency; _frequency = job.Frequency;
(_startDate, _startTime) = Utilities.UtcAsLocalDateAndTime(job.StartDate); _startDate = Utilities.UtcAsLocalDateAndTimeToDate(job.StartDate);
(_endDate, _endTime) = Utilities.UtcAsLocalDateAndTime(job.EndDate); _startTime = Utilities.UtcAsLocalDateAndTimeToTime(job.StartDate);
_endDate = Utilities.UtcAsLocalDateAndTimeToDate(job.EndDate);
_endTime = Utilities.UtcAsLocalDateAndTimeToTime(job.EndDate);
_retentionHistory = job.RetentionHistory.ToString(); _retentionHistory = job.RetentionHistory.ToString();
(_nextDate, _nextTime) = Utilities.UtcAsLocalDateAndTime(job.NextExecution); _nextDate = Utilities.UtcAsLocalDateAndTimeToDate(job.NextExecution);
_nextTime = Utilities.UtcAsLocalDateAndTimeToTime(job.NextExecution);
createdby = job.CreatedBy; createdby = job.CreatedBy;
createdon = job.CreatedOn; createdon = job.CreatedOn;
modifiedby = job.ModifiedBy; modifiedby = job.ModifiedBy;

View File

@ -508,21 +508,21 @@ namespace Oqtane.Shared
return null; return null;
} }
public static (DateTime? date, DateTime? time) UtcAsLocalDateAndTime(DateTime? dateTime, TimeZoneInfo timeZone = null) public static (DateTime? date, string time) UtcAsLocalDateAndTime(DateTime? dateTime, TimeZoneInfo timeZone = null)
{ {
timeZone ??= TimeZoneInfo.Local; timeZone ??= TimeZoneInfo.Local;
DateTime? localDateTime = null; DateTime? localDateTime = null;
DateTime? localTime = null; string localTime = string.Empty;
if (dateTime.HasValue && dateTime?.Kind != DateTimeKind.Local) if (dateTime.HasValue && dateTime?.Kind != DateTimeKind.Local)
{ {
if (dateTime?.Kind == DateTimeKind.Unspecified) 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. // 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 the database should be UTC. // All values in database should be UTC.
// Normal .net conversion treats Unspecified as local. // Normal .net conversion treats Unspecified as local.
// https://docs.microsoft.com/en-us/dotnet/api/system.timezoneinfo.converttime?view=net-6.0 // https://docs.microsoft.com/en-us/dotnet/api/system.timezoneinfo.converttime?view=net-6.0
localDateTime = TimeZoneInfo.ConvertTime(new DateTimeOffset(dateTime.Value.Ticks, TimeSpan.Zero), timeZone).DateTime; localDateTime = TimeZoneInfo.ConvertTime(new DateTime(dateTime.Value.Ticks, DateTimeKind.Utc), timeZone);
} }
else else
{ {
@ -530,16 +530,56 @@ namespace Oqtane.Shared
} }
} }
if (localDateTime != null) if (localDateTime != null && localDateTime.Value.TimeOfDay.TotalSeconds != 0)
{ {
localTime = localDateTime.Value.TimeOfDay.TotalSeconds != 0 ? localDateTime.Value.Date.Add(localDateTime.Value.TimeOfDay) : (DateTime?)null; localTime = localDateTime.Value.ToString("HH:mm");
} }
return (localDateTime?.Date, localTime); return (localDateTime?.Date, localTime);
} }
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? UtcAsLocalDateAndTimeToDate(DateTime? dateTime, TimeZoneInfo timeZone = null)
{
var result = UtcAsLocalDateAndTime(dateTime, timeZone);
return result.date;
}
public static DateTime? UtcAsLocalDateAndTimeToTime(DateTime? dateTime, TimeZoneInfo timeZone = null)
{
var result = UtcAsLocalDateAndTime(dateTime, timeZone);
if (string.IsNullOrEmpty(result.time))
{
return result.date;
}
else
{
var timeParts = result.time.Split(':');
if (timeParts.Length == 2 && int.TryParse(timeParts[0], out int hours) && int.TryParse(timeParts[1], out int minutes))
{
TimeSpan timeOfDay = new TimeSpan(hours, minutes, 0);
return result.date?.Date + timeOfDay;
}
else
{
// Handle parsing error
return null;
}
}
}
[Obsolete("ContentUrl(Alias alias, int fileId) is deprecated. Use FileUrl(Alias alias, int fileId) instead.", false)] [Obsolete("ContentUrl(Alias alias, int fileId) is deprecated. Use FileUrl(Alias alias, int fileId) instead.", false)]
public static string ContentUrl(Alias alias, int fileId) public static string ContentUrl(Alias alias, int fileId)