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

finding number of days between two dates in the same year (c++)

我想计算两个月之间的天数。我使用 Xcode 但不想经历安装 boost 或 'date.h' 的麻烦,所以我尝试更原始地做,但不知何故代码在某个点不断中断:

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

我不确定哪里出了问题,也不确定这样做是否合适。我将不胜感激 help/advice!我是编程初学者。

我建议使用 "difftime":

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

附录:

我想我会在 Eclipse/CDT 上尝试一些示例代码...但是我的 CDT 安装不工作 :( 我最终重新安装。

无论如何:

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

这类似于我要求未来雇员为我编写代码的面试问题。 (并且出于面试评估的目的,他们不允许使用内置的 date/time 功能)。

并且 OP 的问题被简化为测量同一年内日期的天数增量 - 这样就更容易了。

首先,我们需要一个简单的函数来告诉我们正在处理的年份是否是闰年,因为在代码中的某些地方,我们必须处理闰年。而且,闰年不仅仅是 "every four year"。 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);
}

而且我们需要另一个辅助函数来 return 一个月中的天数,它需要考虑二月份的闰年。

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

假设在上面的代码中 "January" 将由 "month == 1" 表示,十二月是 "month == 12"。因此,数组查找中的 [month-1] 东西。我们会在上面的代码中添加一些参数验证,但它应该可以用于讨论。

现在我们有一种方法可以计算一个月中的天数,我们需要一个函数来告诉我们 "how many days since the beginning of the year" 对于给定的 month/day/year。

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

上面的函数将 return (1,1,2015) 为“0”,(12,31,2015) 为“364”。同样,生产代码需要参数验证。

现在计算同一年任意两天之间的天数差:

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

如果您想将其简化为仅计算月份之间的天数,则只需为天数传递“1”即可。

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

希望这对您有所帮助。