Fix for Schedular Allows incorrect Time format #3184

Modified code to accept correct time types.
This commit is contained in:
Leigh Pointer 2023-08-24 18:37:33 +02:00
parent 6a2cfcab34
commit 073b10929a
2 changed files with 38 additions and 31 deletions

View File

@ -56,7 +56,7 @@
<input id="starting" type="date" class="form-control" @bind="@_startDate" />
</div>
<div class="col">
<input id="starting" type="text" class="form-control" placeholder="hh:mm" @bind="@_startTime" />
<input id="starting" type="time" class="form-control" placeholder="hh:mm" @bind="@_startTime" />
</div>
</div>
</div>
@ -69,7 +69,7 @@
<input id="ending" type="date" class="form-control" @bind="@_endDate" />
</div>
<div class="col">
<input id="ending" type="text" class="form-control" placeholder="hh:mm" @bind="@_endTime" />
<input id="ending" type="time" class="form-control" placeholder="hh:mm" @bind="@_endTime" />
</div>
</div>
</div>
@ -82,7 +82,7 @@
<input id="next" type="date" class="form-control" @bind="@_nextDate" />
</div>
<div class="col">
<input id="next" type="text" class="form-control" placeholder="hh:mm" @bind="@_nextTime" />
<input id="next" type="time" class="form-control" placeholder="hh:mm" @bind="@_nextTime" />
</div>
</div>
</div>
@ -106,12 +106,12 @@
private string _interval = string.Empty;
private string _frequency = string.Empty;
private DateTime? _startDate = null;
private string _startTime = string.Empty;
private DateTime? _startTime = null;
private DateTime? _endDate = null;
private string _endTime = string.Empty;
private DateTime? _endTime = null;
private string _retentionHistory = string.Empty;
private DateTime? _nextDate = null;
private string _nextTime = string.Empty;
private DateTime? _nextTime = null;
private string createdby;
private DateTime createdon;
private string modifiedby;

View File

@ -490,35 +490,39 @@ namespace Oqtane.Shared
return $"[{@class.GetType()}] {message}";
}
public static DateTime? LocalDateAndTimeAsUtc(DateTime? date, string time, TimeZoneInfo localTimeZone = null)
public static DateTime? LocalDateAndTimeAsUtc(DateTime? date, DateTime? time, TimeZoneInfo localTimeZone = null)
{
localTimeZone ??= TimeZoneInfo.Local;
if (date != null)
{
if (!string.IsNullOrEmpty(time))
if (time != null)
{
return TimeZoneInfo.ConvertTime(DateTime.Parse(date.Value.Date.ToShortDateString() + " " + time), localTimeZone, TimeZoneInfo.Utc);
DateTime localDateTime = date.Value.Date.Add(time.Value.TimeOfDay);
return TimeZoneInfo.ConvertTimeToUtc(localDateTime, localTimeZone);
}
return TimeZoneInfo.ConvertTime(date.Value.Date, localTimeZone, TimeZoneInfo.Utc);
return TimeZoneInfo.ConvertTimeToUtc(date.Value.Date, localTimeZone);
}
return null;
}
public static (DateTime? date, string time) UtcAsLocalDateAndTime(DateTime? dateTime, TimeZoneInfo timeZone = null)
public static (DateTime? date, DateTime? time) UtcAsLocalDateAndTime(DateTime? dateTime, TimeZoneInfo timeZone = null)
{
timeZone ??= TimeZoneInfo.Local;
DateTime? localDateTime = null;
string localTime = string.Empty;
DateTime? localTime = null;
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.
// All values in the 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);
localDateTime = TimeZoneInfo.ConvertTime(new DateTimeOffset(dateTime.Value.Ticks, TimeSpan.Zero), timeZone).DateTime;
}
else
{
@ -526,14 +530,17 @@ namespace Oqtane.Shared
}
}
if (localDateTime != null && localDateTime.Value.TimeOfDay.TotalSeconds != 0)
if (localDateTime != null)
{
localTime = localDateTime.Value.ToString("HH:mm");
localTime = localDateTime.Value.TimeOfDay.TotalSeconds != 0 ? localDateTime.Value.Date.Add(localDateTime.Value.TimeOfDay) : (DateTime?)null;
}
return (localDateTime?.Date, localTime);
}
[Obsolete("ContentUrl(Alias alias, int fileId) is deprecated. Use FileUrl(Alias alias, int fileId) instead.", false)]
public static string ContentUrl(Alias alias, int fileId)
{