78 lines
2.3 KiB
C#

using System.Text.Json.Serialization;
namespace gehGassi.LocalNotifications
{
/// <summary>
/// Repräsentiert eine Zeit-Einstellung
/// </summary>
public class Time
{
/// <summary>
/// Stunde
/// </summary>
public int Hour { get; }
/// <summary>
/// Minute
/// </summary>
public int Minute { get; }
/// <summary>
/// Sekunde
/// </summary>
public int Second { get; }
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="hour">Stunde</param>
/// <param name="minute">Minute</param>
/// <param name="second">Sekunde</param>
[JsonConstructor]
public Time(int hour = 0, int minute = 0, int second = 0)
{
Validation(hour, minute, second);
Hour = hour;
Minute = minute;
Second = second;
}
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="time">Zeit als string hh:mm:ss</param>
/// <exception cref="NullReferenceException"></exception>
public Time(string time)
{
if (string.IsNullOrEmpty(time))
throw new NullReferenceException("time");
TimeSpan span = TimeSpan.Parse(time);
Validation(span.Hours, span.Minutes, span.Seconds);
Hour = span.Hours;
Minute = span.Minutes;
Second = span.Seconds;
}
/// <summary>
/// Validieren der Zeit-Einstellung
/// </summary>
/// <param name="hour">Stunde</param>
/// <param name="minute">Minute</param>
/// <param name="second">Sekunde</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
private void Validation(int hour, int minute, int second)
{
if (hour >= 24)
throw new ArgumentOutOfRangeException("Accepted range is 0 to 23 inclusive.");
if (minute >= 60)
throw new ArgumentOutOfRangeException("Accepted range is 0 to 59 inclusive.");
if (second >= 60)
throw new ArgumentOutOfRangeException("Accepted range is 0 to 59 inclusive.");
}
}
}