Comparing dates with custom date class

1

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.

    
asked by anonymous 16.04.2018 / 15:06

2 answers

3

You can simplify comparisons by bringing 'date / time' as strings as long as you obey the progress hierarchy and a fixed format.

For simplicity, I've created a method that places HolidayDateTime in the fixed string format defined ( MMddHHmm ):

private static string GetString(HolidayDateTime hdt)
{
    return hdt.Month.ToString("00") + hdt.Day.ToString("00") + hdt.Hour.ToString("00") + hdt.Minute.ToString("00");
}

I use this same method in overloading ToString (for presentation purposes only):

public override string ToString()
{
    return GetString(this);
}

In creating the operators, I use the string.Compare to determine if it is greater, less than or equal to:

public static bool operator >=(HolidayDateTime hdaydt1, HolidayDateTime hdaydt2) => string.Compare(GetString(hdaydt1), GetString(hdaydt2)) >= 0;
public static bool operator <=(HolidayDateTime hdaydt1, HolidayDateTime hdaydt2) => string.Compare(GetString(hdaydt1), GetString(hdaydt2)) <= 0;
public static bool operator >(HolidayDateTime hdaydt1, HolidayDateTime hdaydt2) => string.Compare(GetString(hdaydt1), GetString(hdaydt2)) > 0;
public static bool operator <(HolidayDateTime hdaydt1, HolidayDateTime hdaydt2) => string.Compare(GetString(hdaydt1), GetString(hdaydt2)) < 0;

See the example running on dotnetfiddle

I hope this helps.

    
16.04.2018 / 16:52
2

Many people find that the .NET date API is not appropriate. For this, NodaTime was created. If it is not appropriate it is 99.999999% sure you are doing something wrong.

This is the best solution .

Do not reinvent the wheel. What Jon Skeet did was reinvent the wheel the right way. Reinventing the wheel is worthwhile when there is a deep understanding of the problem and if you know that it will do something better, which is not the case with your code, it has several problems in it, especially for someone who is concerned about the readability of the code. Almost all the language mechanisms used in it are inadequate.

    
16.04.2018 / 15:18