Home > Mobile >  How to compare two datetime structures in C efficiently?
How to compare two datetime structures in C efficiently?

Time:07-08

I have the following DateTime structure:

struct DateTime
{
    std::uint16_t year;
    std::uint8_t month;
    std::uint8_t day;
    std::uint8_t hour;
    std::uint8_t minute;
    std::uint8_t second;
    std::uint16_t milisecond;
};

My doubt is about the LessThan and GreaterThan methods. I have implemented as follows in order to avoid a bunch of ifs and elses, but I might have not covered all possible situations:

bool GreaterThan(const DateTime& datetime)
{
    bool greater{true};

    // When found a different value for the most significant value, the evaluation is interrupted
    if ((year <= datetime.year) && (month <= datetime.month || year < datetime.year) &&
        (day <= datetime.day || month < datetime.month) && (hour <= datetime.hour || day < datetime.day) &&
        (minute <= datetime.minute || hour < datetime.hour) &&
        (second <= datetime.second || minute < datetime.minute) &&
        (milisecond <= datetime.milisecond || second < datetime.second))
    {
        greater = false;
    }

    return greater;
}

bool LessThan(const DateTime& datetime)
{
    bool less{true};

    // When found a different value for the most significant value, the evaluation is interrupted
    if ((year >= datetime.year) && (month >= datetime.month || year > datetime.year) &&
        (day >= datetime.day || month > datetime.month) && (hour >= datetime.hour || day > datetime.day) &&
        (minute >= datetime.minute || hour > datetime.hour) &&
        (second >= datetime.second || minute > datetime.minute) &&
        (milisecond >= datetime.milisecond || second > datetime.second))
    {
        less = false;
    }

    return less;
}

Please, let me know which possible situation is not covered.

CodePudding user response:

Your implementation looks overly painful. There's a simpler, logical way to acheive this. If you know of the greedy algorithm, its a similar thought process to that.

bool GreaterThan(const datetime& datetime)
{
    if(year != datetime.year) return year > datetime.year;
    if(month != datetime.month) return month > datetime.month;
    //... and so on (omitted)
    return false; // they are the same
}

You can implement LessThan similarly.

CodePudding user response:

From all the comments, I think the best solution was proposed by – Richard Critten:

bool GreaterThan(const DateTime& date_time)
{
    return (std::tie(year, month, day, hour, minute, second, milisecond) > 
            std::tie(date_time.year, date_time.month, date_time.day, date_time.hour, date_time.minute, date_time.second, date_time.milisecond));
}

bool LessThan(const DateTime& date_time)
{
    return (std::tie(year, month, day, hour, minute, second, milisecond) <
            std::tie(date_time.year, date_time.month, date_time.day, date_time.hour, date_time.minute, date_time.second, date_time.milisecond));
}
  • Related