查找同一年中两个日期之间的天数(c ++)

时间:2015-10-24 23:36:09

标签: c++ date break days

我想找到两个月之间的天数。我使用Xcode但是不想经历安装boost或&#39; date.h&#39;的麻烦,所以我试图更原始地做,但不知何故代码在某一点上不断破坏:< / p>

    for ( it=mymap.begin() ; it != mymap.end(); it++ ) {
        auto nx = next(it);

        if (it->second.patientID == nx->second.patientID) {

            //31 28 31 30 31 30 31 31 30 31 30 31
            yue = it->second.month;
            yue2 = nx->second.month;
            sincejan1 = 0;
            sincejan = 0;

            //it keeps breaking at the line below
            if (abs(yue-yue2) > 0) {

            if (yue ==12)
                sincejan1 = 365-31;
            if (yue ==11)
                sincejan1 = 365-31-30;
            if (yue ==10)
                sincejan1 = 365-31-30-31;
            if (yue ==9)
                sincejan1 = 365-31-30-31-30;
            if (yue ==8)
                sincejan1 = 31+28+31+30+31+30+31+31;
            if (yue ==7)
                sincejan1 = 31+28+31+30+31+30+31;
            if (yue ==6)
                sincejan1 = 31+28+31+30+31+30;
            if (yue ==5)
                sincejan1 = 31+28+31+30+31;
            if (yue ==4)
                sincejan1 = 31+28+31+30;
            if (yue ==3)
                sincejan1 = 31+28+31;
            if (yue ==2)
                sincejan1 = 31+28;
            if (yue ==1)
                sincejan1 = 31;

            if (yue2 ==12)
                sincejan = 365-31;
            if (yue2 ==11)
                sincejan = 365-31-30;
            if (yue2 ==10)
                sincejan = 365-31-30-31;
            if (yue2 ==9)
                sincejan = 365-31-30-31-30;
            if (yue2 ==8)
                sincejan = 31+28+31+30+31+30+31+31;
            if (yue2 ==7)
                sincejan = 31+28+31+30+31+30+31;
            if (yue2 ==6)
                sincejan = 31+28+31+30+31+30;
            if (yue2 ==5)
                sincejan = 31+28+31+30+31;
            if (yue2 ==4)
                sincejan = 31+28+31+30;
            if (yue2 ==3)
                sincejan = 31+28+31;
            if (yue2 ==2)
                sincejan = 31+28;
            if (yue2 ==1)
                sincejan = 31;
            }

            monthDiff = sincejan1 - sincejan;
        }
    }

我不确定是什么错,或者这是否可行。我非常感谢任何帮助/建议!我是一名编程初学者。

2 个答案:

答案 0 :(得分:1)

我建议使用“difftime”:

http://www.manpagez.com/man/3/difftime/

附录:

我以为我会在Eclipse / CDT上尝试一些示例代码......但我的CDT安装无法正常工作:(我最终重新安装。

ANYWAY:

datediff.c:

#include <stdio.h>   /* printf() etc */
#include <time.h>    /* time(), difftime(), time_t, struct tm */
#include <stdlib.h>  /* atoi() */

#define SECONDS_IN_DAY (60 * 60 * 24) /* Note importance of parentheses */

int
datediff(int m1, int m2) {
        double diff_seconds;
        int diff_days;

        /* Populate timeptr structs */
        time_t now = time(NULL);
        struct tm *tm_ptr = gmtime(&now);
        struct tm t1 = *tm_ptr, t2 = *tm_ptr;
        t1.tm_mon = m1;
        t2.tm_mon = m2;

    /* Compute difference between m1 and m2 */
        diff_seconds = difftime(mktime(&t1), mktime(&t2));
        diff_days = diff_seconds / SECONDS_IN_DAY;
        if (diff_days < 0) diff_days = -diff_days;
        return diff_days;
}

int
main (int argc,char *argv[]) {
        /* Input: month1, month2 */
        if (argc != 3) {
                printf ("USAGE: datediff m1 m2\n");
                return 1;
        }

        /* Compare dates */
        printf ("#/months= %d\n", datediff(atoi(argv[1]), atoi(argv[2])));

        /* Exit */
        return 0;
}

示例输出:

./datediff 9 1
#/months= 242

./datediff 1 9
#/months= 242

./datediff 9 8

./datediff 9 8
#/months= 30

./datediff 3 2
#/months= 31

答案 1 :(得分:1)

这类似于我要求潜在雇员为我编码的面试问题。 (为了进行面试评估,他们不允许使用内置的日期/时间功能)。

OP的问题被简化为测量同一年内日期的增量 - 这样可以使事情变得更容易。

首先,我们需要一个简单的函数来告诉我们,我们处理的年份是否是闰年,因为在代码中的某些时候,我们必须处理闰年。而且,闰年不仅仅是每四年一次#34;。 But you already know that.

bool isLeapYear(int year)
{
    bool isDivisibleByFour = !(year % 4);
    bool isDivisibleBy100 = !(year % 100);
    bool isDivisibleBy400 = !(year % 400);
    return (isDivisibleBy400) || (isDivisibleByFour && !isDivisibleBy100);
}

我们需要另一个辅助函数来返回一个月内的天数,它需要考虑二月份的闰年。

int getDaysInMonth(int month, int year)
{
    int days_in_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

    int result = days_in_month[month-1];
    if ((month == 2) && isLeapYear(year))
    {
        result++;
    }
    return result;
}

在上面的代码中假设&#34; 1月&#34;将由&#34;月== 1&#34;表示12月是&#34;月== 12&#34;。因此,数组查找中的[month-1]事物。我们会在上面的代码中添加一些参数验证,但它应该用于讨论目的。

现在我们有办法计算一个月内的天数,我们需要一个能告诉我们&#34;自今年年初以来多少天的功能&#34;对于给定的月/日/年。

int getDayOfYear(int month, int day, int year)
{
    int count = 0;
    int m = 1;

    while (m != month)
    {
        count += getDaysInMonth(m, year);
        m++;
    }
    count += day - 1;
    return count;
}

上述功能将返回&#34; 0&#34; (1,1,2015)和&#34; 364&#34; (12,31,2015)。同样,生产代码需要参数验证。

现在计算同年任何两天之间的天数差异:

int getDiffOfDaysInSameYear(int month1, int day1, int month2, int day2, int year)
{
    int day_of_year1 = getDayOfYear(month1, day1, year);
    int day_of_year2 = getDayOfYear(month2, day2, year);
    return day_of_year2 - day_of_year1;
}

让我们测试一下:

int main()
{
    int x = getDiffOfDaysInSameYear(4,4, 10,24, 2015); // number of days to get to  10/24/2015 from 4/4/2015
    printf("The delta in days between April 4 and October 2015 is: %d days\n", x);
    return 0;
}

打印出来:The delta in days between April 4 and October 2015 is: 203 days

如果你想将它简化为仅计算几个月之间的天数,那么只需传入&#34; 1&#34;为了这些日子。

int main()
{
    int x = getDiffOfDaysInSameYear(5, 1, 11, 1, 2015);
    printf("The delta in days between May and November is %d\n", x);
    return 0;
}

打印出来:The delta in days between May and November is 184

希望这有帮助。