mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 04:53:11 +00:00
60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
namespace SeaHaven.Services.Helpers
|
||
{
|
||
public static class WorkOrderBoardApptTimeParser
|
||
{
|
||
public static bool TryParse(string? value, out TimeSpan? start, out TimeSpan? end, out string? error)
|
||
{
|
||
start = null;
|
||
end = null;
|
||
error = null;
|
||
|
||
if (string.IsNullOrWhiteSpace(value))
|
||
return true;
|
||
|
||
var trimmed = value.Trim();
|
||
var parts = trimmed.Split(new[] { '–', '—', '-' })
|
||
.Select(p => p.Trim())
|
||
.Where(p => !string.IsNullOrEmpty(p))
|
||
.ToArray();
|
||
|
||
if (parts.Length == 1)
|
||
{
|
||
if (!TimeSpan.TryParse(parts[0], out var single))
|
||
{
|
||
error = "Invalid appointment time format.";
|
||
return false;
|
||
}
|
||
|
||
start = single;
|
||
return true;
|
||
}
|
||
|
||
if (parts.Length == 2)
|
||
{
|
||
if (!TimeSpan.TryParse(parts[0], out var startTime) || !TimeSpan.TryParse(parts[1], out var endTime))
|
||
{
|
||
error = "Invalid appointment time range format.";
|
||
return false;
|
||
}
|
||
|
||
start = startTime;
|
||
end = endTime;
|
||
return true;
|
||
}
|
||
|
||
error = "Invalid appointment time format.";
|
||
return false;
|
||
}
|
||
|
||
public static DateTime? CombineDateAndTime(DateTime? date, TimeSpan? time)
|
||
{
|
||
if (!date.HasValue || !time.HasValue)
|
||
return date;
|
||
|
||
return date.Value.Date.Add(time.Value);
|
||
}
|
||
|
||
public static DateTime? ToScheduledInstant(DateTime? date, TimeSpan? time)
|
||
=> time.HasValue ? CombineDateAndTime(date, time) : null;
|
||
}
|
||
}
|