Comparison of dates with custom date class

Asked

Viewed 86 times

1

For design reasons, the class Datetime do. 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, for this I will need to overload the operators >, <, ==, >=, <= and !=

For operators == and != 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 lies in the operators >, <,>= and <= because the boolean combination to define whether a date is larger, smaller, larger-or-equal, smaller-or-equal is huge which would result in a hard-to-read and large code.

I would like tips for a better solution.

  • Actually I didn’t even try, because mentally I already predicted that it would result in a monstrous and bizarre code. Com string would even work for operators == and != but I see no possibility for other operators.

  • Ummmm, convert to numeric, yes, can be the solution...

2 answers

4

Many people consider that the . NET date API is not suitable. For this purpose the Nodatime. If it’s not suitable, it’s 99.999999% sure you’re doing something wrong.

This is the best solution.

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

Ferrari futurística com rodas de pedra

  • 1

    Hello, yes, my solution of Datetime is simple and not very beautiful. But at no time did I want to create a "substitute" for the Datetime and make it available on the Internet to anyone who wants to use it. It is a simple poor solution to be used only in the solution I am working on and only in a specific place, so much so that elsewhere I am using the Datetime standard .NET. My intention is not to create something better than the Datetime, far from it, just something that suits my specific case. Anyway I will check the Nodatime.

1


You can simplify comparisons by turning 'date/time' into strings, as long as you follow the progression hierarchy and a fixed format.

To simplify, I have created a method that puts the 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");
}

Use this same method in overloading the ToString (for the purpose of presentation only):

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

When creating operators, I use the method string.Compare to determine whether it is larger, smaller or equal:

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.

  • I was researching the return of the method Compare, the documentation says that it returns an integer that indicates its relative position in the sort order. .

  • 2

    This method uses a sort algorithm. If the result is less than zero, that means that the first string is earlier than the second, if it is equal it would be 'in the same position' and if it is higher the first string is later than the second in an ascending alphabetic ordering.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.