如何比较C+&#x2B中的两个DateTime结构有效?

发布于 2025-02-13 20:51:39 字数 1857 浏览 0 评论 0原文

我有以下DateTime结构:

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;
};

我的疑问是关于 Lessthan 更大的方法方法。我的实施如下,以避免一堆 ifs elses ,但我可能没有涵盖所有可能的情况:

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;
}

请让我知道哪种情况不是覆盖。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东北女汉子 2025-02-20 20:51:40

从所有评论中,我认为最好的解决方案是由 -
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));
}

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));
}
°如果伤别离去 2025-02-20 20:51:39

您的实施看起来过于痛苦。有一种更简单,合乎逻辑的方法来实现这一目标。如果您知道贪婪的算法,那是与此类似的思考过程。

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
}

您可以类似地实现Lessthan

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文