For reasons of design, the DateTime class of .NET is not suitable for me. So I decided to create my own version:
public class HolidayDateTime
{
public readonly uint[] RangeValueDay = { 1, 31 };
public readonly uint[] RangeValueMonth = { 1, 12 };
public readonly uint[] RangeValueHour = { 0, 23 };
public readonly uint[] RangeValueMinute = { 0, 60 };
public uint Day { get; private set; }
public uint Month { get; private set; }
public uint Hour { get; private set; }
public uint Minute { get; private set; }
protected HolidayDateTime() { }
public HolidayDateTime(uint day, uint month, uint hour, uint minute)
{
Guard.IntengerRangeInvalid((int)day, (int)RangeValueDay[0], (int)RangeValueDay[1], "O dia informado é inválido");
Guard.IntengerRangeInvalid((int)month, (int)RangeValueMonth[0], (int)RangeValueMonth[1], "O mês informado é inválido");
Guard.IntengerRangeInvalid((int)hour, (int)RangeValueHour[0], (int)RangeValueHour[1], "A hora informado é inválida");
Guard.IntengerRangeInvalid((int)minute, (int)RangeValueMinute[0], (int)RangeValueMinute[1], "O minuto informado é inválido");
Day = day;
Month = month;
Hour = hour;
Minute = minute;
}
public string DateFullToString() => Day + @"/" + Month + " " + Hour + ":" + Minute;
public uint[] DateFullToArray() => new uint[] { Day, Month, Hour, Minute };
In the project I will need to make comparisons between the dates, so I will need to overload the operators > , < , == < < = and ! =
For the == and ! = operators, Boolean logic is quiet to do:
public static bool operator ==(HolidayDateTime hdaydt1, HolidayDateTime hdaydt2) => hdaydt1.Day == hdaydt2.Day && hdaydt1.Month == hdaydt2.Month && hdaydt1.Hour == hdaydt2.Hour && hdaydt1.Minute == hdaydt2.Minute;
public static bool operator !=(HolidayDateTime hdaydt1, HolidayDateTime hdaydt2) => hdaydt1.Day != hdaydt2.Day || hdaydt1.Month != hdaydt2.Month || hdaydt1.Hour != hdaydt2.Hour || hdaydt1.Minute != hdaydt2.Minute;
My difficulty is with the > , < , && = strong operators the Boolean combination to define whether a date is larger, smaller, greater-or-equal, smaller-or-equal, is huge which would result in a difficult to read and large code.
I would like tips for a better solution.