【问题标题】:How to find the difference between two dates c++如何找到两个日期之间的差异c ++
【发布时间】:2016-10-09 19:18:06
【问题描述】:

我是一名学生,我想知道是否有人可以帮助我调试我的一个函数。对于这个程序,假设用户在 1972-2071 年之间以 mm/dd/yy 格式输入两个日期,并且程序假设输出这两个日期之间的差异。除了计算 72 年 1 月 1 日的天数的函数外,我所有的函数都可以工作。这是我们的教授希望我们在没有额外功能的情况下这样做的方式。只是一个包含大量 if else 语句和 for 循环的初学者版本。

int ChangeToNumber(int m, int d, int y)
{
    int total=0;

    if(y<=71) //if the year is between 2000-2071
    {
        y+=28;

        for(int i=0; i<y; i++)
        {
            if(!LeapYear(y))
            {
                total+=365;
            }
            else
            {
                total+=366;
            }
        }
    }
    else //if the year is between 1972-1999
    {
        for(int i=72; i<y; i++)
        {
            if(!LeapYear(y))
            {
                total+=365;
            }
            else
            {
                total+=366;
            }
        }
    }


    for(int i=1; i<m; i++)
    {
        total+=DaysInMonth(m, y);
    }

    total += d;
    return total;
}

【问题讨论】:

  • 您需要添加更完整的代码示例。例如LeapYearDaysInMonth这两个方法读者不知道
  • Cheap hack:(year + 28) %100 将为您省去一些麻烦和代码重复
  • #include &lt;chrono&gt;
  • 由于 d 在整个函数中根本没有改变,但是返回值需要它,所以你首先要做的就是设置 total 等于 d。您还应该将年份表示为 4 位数,而不是 2。将 28 添加到低于或等于 71 的值是一种非常老套的做事方式,并且会降低代码的可读性。另一种计算年份的方法是简单地计算 y - 1972。

标签: c++


【解决方案1】:

您可以使用std::difftime 为您提供一些帮助。

没有额外的功能:

http://en.cppreference.com/w/cpp/chrono/c/difftime

#include <iostream>
#include <ctime>

int main()
{
    struct std::tm a = {0,0,0,24,5,104}; /* June 24, 2004 */
    struct std::tm b = {0,0,0,5,6,104}; /* July 5, 2004 */
    std::time_t x = std::mktime(&a);
    std::time_t y = std::mktime(&b);
    if ( x != (std::time_t)(-1) && y != (std::time_t)(-1) )
    {
        double difference = std::difftime(y, x) / (60 * 60 * 24);
        std::cout << std::ctime(&x);
        std::cout << std::ctime(&y);
        std::cout << "difference = " << difference << " days" << std::endl;
    }
    return 0;
}

额外功能:

#include "boost/date_time/gregorian/gregorian_types.hpp"

using namespace boost::gregorian;
date date1(2012, Apr, 2);
date date2(2003, Feb, 2);
long difference = (date1 - date2).days();

【讨论】:

  • "没有额外的功能。只是一个带有很多 if else 语句和 for 循环的初学者版本"
  • 基本语言功能不被视为额外功能 ;)
  • 也许不适合你。可悲的是,我们生活在一个教师认为vectorstring 太神秘而无法立即教授的世界中。
  • 那是老师的问题。这是我们进行 C++ 问答的 Stack Overflow,而不是“C++ 的一小部分”作业调试服务。
【解决方案2】:

我认为您正在寻找基本的公历算法:

http://howardhinnant.github.io/date_algorithms.html

ChangeToNumber 看起来很像http://howardhinnant.github.io/date_algorithms.html#days_from_civil 并且详细解释的算法在比微不足道的 1972-2071 年更大的范围内有效。

这个日历算法链接的语法不是很好(尽管算法非常好)。在 C++ 中,您可以使用非常高效的算法实现非常好的语法,并使用如下库: https://howardhinnant.github.io/date/date.html

这个程序:

#include "chrono_io.h"
#include "date.h"
#include <iostream>

int
main()
{
    using namespace date;
    std::cout << sys_days{2016_y/oct/9} - sys_days{2016_y/jan/1} << '\n';
}

输出:

282[86400]s

这意味着如果 2016-01-01 是第 0 天,那么 2016-10-09 是第 282 天

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    相关资源
    最近更新 更多