DD.WellWorkover.Cloud/AsbCloudApp/Data/TimeDto.cs

159 lines
3.6 KiB
C#
Raw Permalink Normal View History

using System;
2022-05-31 12:30:03 +05:00
2024-08-19 10:01:07 +05:00
namespace AsbCloudApp.Data;
/// <summary>
/// DTO времени
/// </summary>
public class TimeDto : IComparable<TimeDto>
2022-05-31 12:30:03 +05:00
{
2024-08-19 10:01:07 +05:00
private int hour = 0;
private int minute = 0;
private int second = 0;
2022-05-31 12:30:03 +05:00
/// <summary>
2024-08-19 10:01:07 +05:00
/// час
2022-05-31 12:30:03 +05:00
/// </summary>
2024-08-19 10:01:07 +05:00
public int Hour
2022-05-31 12:30:03 +05:00
{
2024-08-19 10:01:07 +05:00
get => hour;
set
2022-06-15 14:57:37 +05:00
{
2024-08-19 10:01:07 +05:00
if (value > 23 || value < 0)
throw new ArgumentOutOfRangeException(nameof(Hour), "hour should be in [0; 23]");
hour = value;
2022-05-31 12:30:03 +05:00
}
2024-08-19 10:01:07 +05:00
}
2022-05-31 12:30:03 +05:00
2024-08-19 10:01:07 +05:00
/// <summary>
/// минута
/// </summary>
public int Minute
{
get => minute;
set
2022-05-31 12:30:03 +05:00
{
2024-08-19 10:01:07 +05:00
if (value > 59 || value < 0)
throw new ArgumentOutOfRangeException(nameof(minute), "minute should be in [0; 59]");
minute = value;
2022-05-31 12:30:03 +05:00
}
2024-08-19 10:01:07 +05:00
}
2022-05-31 12:30:03 +05:00
2024-08-19 10:01:07 +05:00
/// <summary>
/// секунда
/// </summary>
public int Second
{
get => second;
set
2022-05-31 12:30:03 +05:00
{
2024-08-19 10:01:07 +05:00
if (value > 59 || value < 0)
throw new ArgumentOutOfRangeException(nameof(second), "second should be in [0; 59]");
second = value;
2022-05-31 12:30:03 +05:00
}
2024-08-19 10:01:07 +05:00
}
2022-05-31 12:30:03 +05:00
2024-08-19 10:01:07 +05:00
/// <summary>
/// Кол-во секунд с начала суток
/// </summary>
public int TotalSeconds => (Hour * 60 + minute) * 60 + second;
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public TimeDto()
{ }
2022-05-31 12:30:03 +05:00
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public TimeDto(int hour = 0, int minute = 0, int second = 0)
{
this.hour = hour;
this.minute = minute;
this.second = second;
}
2022-06-09 13:36:03 +05:00
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public TimeDto(TimeOnly time)
{
hour = time.Hour;
minute = time.Minute;
second = time.Second;
}
2022-05-31 12:30:03 +05:00
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public TimeDto(DateTime fullDate)
{
hour = fullDate.Hour;
minute = fullDate.Minute;
second = fullDate.Second;
}
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public TimeDto(DateTimeOffset fullDate)
{
hour = fullDate.Hour;
minute = fullDate.Minute;
second = fullDate.Second;
}
2024-02-08 11:38:25 +05:00
2024-08-19 10:01:07 +05:00
/// <summary>
/// Makes System.TimeOnly
/// </summary>
/// <returns>System.TimeOnly</returns>
public TimeOnly MakeTimeOnly() => new(Hour, Minute, Second);
2022-05-31 12:30:03 +05:00
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public override string ToString()
{
var str = $"{Hour:00}:{Minute:00}:{Second:00}";
return str;
}
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public static bool operator ==(TimeDto a, TimeDto b) => a?.TotalSeconds == b?.TotalSeconds;
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public static bool operator !=(TimeDto a, TimeDto b) => !(a == b);
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public static bool operator <=(TimeDto a, TimeDto b) => a.TotalSeconds <= b.TotalSeconds;
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public static bool operator >=(TimeDto a, TimeDto b) => a.TotalSeconds >= b.TotalSeconds;
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public static bool operator <(TimeDto a, TimeDto b) => a.TotalSeconds < b.TotalSeconds;
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public static bool operator >(TimeDto a, TimeDto b) => a.TotalSeconds > b.TotalSeconds;
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public int CompareTo(TimeDto? other)
=> TotalSeconds - other?.TotalSeconds??0;
2022-08-03 11:13:23 +05:00
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public override bool Equals(object? obj)
{
if (ReferenceEquals(this, obj))
2022-08-03 11:13:23 +05:00
{
2024-08-19 10:01:07 +05:00
return true;
}
2022-08-03 11:13:23 +05:00
2024-08-19 10:01:07 +05:00
if (obj is null)
{
2022-08-03 11:13:23 +05:00
return false;
}
2024-08-19 10:01:07 +05:00
if (obj is TimeDto objTime)
2022-08-03 11:13:23 +05:00
{
2024-08-19 10:01:07 +05:00
return objTime == this;
2022-08-03 11:13:23 +05:00
}
2024-08-19 10:01:07 +05:00
return false;
}
/// <inheritdoc/>
public override int GetHashCode()
{
return base.GetHashCode();
2022-05-31 12:30:03 +05:00
}
}