50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
using System.Globalization;
|
|
|
|
namespace MeetTheTeacher.Model;
|
|
|
|
/// <summary>
|
|
/// Represents a time frame between a start and end time.
|
|
/// Start time is always before end time.
|
|
/// </summary>
|
|
/// <param name="Start">Start time</param>
|
|
/// <param name="End">End time</param>
|
|
public readonly record struct TimeFrame(TimeOnly Start, TimeOnly End)
|
|
{
|
|
private static readonly CultureInfo culture = new ("de-AT");
|
|
|
|
/// <summary>
|
|
/// Tries to parse a time frame from a string in format HH:MM-HH:MM.
|
|
/// Additional whitespace is trimmed and ignored.
|
|
/// </summary>
|
|
/// <param name="timeFrame">Timeframe string to parse</param>
|
|
/// <param name="result">Set to the parsed time frame if possible</param>
|
|
/// <returns>True if time frame could be parsed successfully; false otherwise</returns>
|
|
public static bool TryParse(string timeFrame, out TimeFrame? result)
|
|
{
|
|
result = null;
|
|
string[] timeFrames = timeFrame.Split("-");
|
|
if (timeFrames.Length != 2)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!TimeOnly.TryParse(timeFrames[0], out TimeOnly start) || !TimeOnly.TryParse(timeFrames[1], out TimeOnly end))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (start > end)
|
|
{
|
|
(start, end) = (end, start);
|
|
}
|
|
|
|
result = new TimeFrame(start, end);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a string representation of the time frame in format HH:MM-HH:MM
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override string ToString() => $"{Start}-{End}";
|
|
}
|