简体   繁体   中英

calculate the difference in days from two dates in C

I have a problem with my code: I need to calculate a difference time between two date. I have the dates inside a struct.

Why the result is 0? I'm blocked here for 2 days.

typedef struct {
    int day;
    int month;
    int year;
} DIAGNOSISDATE;

DIAGNOSISDATE dateDiagnosis[3];

typedef struct {
    int day;
    int month;
    int year;
} HEALINGDATE;

HEALINGDATE dateHealing[3];

typedef struct {
    char firstName[20];
    char lastName[20];
    char fiscalCode[17];
    STATESICK stateSick;
    DIAGNOSISDATE dateDiagnosis;
    HEALINGDATE dateHealing;
} SICKREGION;

SICKREGION sickregion[SIZESICK];

and I have my function here:

void timeDiff() {
    struct tm ts;
    char buf[80];
    //int getTimeDiff = 0;
    long total = 0;
    int i = 0;
    int k = 0;
    int j = 0;
    for (i = 0; i < 1; i++) {
        for (j = 0; j < 1; j++) {
            for (k = 0; k < 3; k++) {
                struct tm time1;
                time1.tm_mday = region[i].hospital[j].sickregion[k].dateHealing.day;
                time1.tm_mon = region[i].hospital[j].sickregion[k].dateHealing.month;
                time1.tm_year = region[i].hospital[j].sickregion[k].dateHealing.year;
                struct tm time0;
                time0.tm_mday = region[i].hospital[j].sickregion[k].dateDiagnosis.day;
                time0.tm_mon = region[i].hospital[j].sickregion[k].dateDiagnosis.month;
                time0.tm_year = region[i].hospital[j].sickregion[k].dateDiagnosis.year;
                time_t rawtime = difftime(mktime(&time1), mktime(&time0));
                // Total = (Total + rawtime);

                ts = *localtime(&rawtime);
                //Total = (rawtime + Total) + 1;
                total += (rawtime / 86400);
            }
        }
    }
    strftime(buf, sizeof(buf), "%j", &ts);
    // printf("%s\n", buf);
    printf("%ld\n", total);
}

I don't understand where is the problem. Anyway in my macbook it will run, in windows not work. :/ the code is the same. I tried to dichiarate the struct tm 0 and struct tm 1 inside my typedef but nothing differences. anyone know where is the problem?

Your code has undefined behavior because you do not initialize the other members of tm_time1 and tm_time2 . A quick fix for this is:

    struct tm time1 = { 0 };
    struct tm time2 = { 0 };

Another problem is you store the return value of difftime of type double into a time_t variable. The time difference may be negative and time_t might be an unsigned type, causing rawtime / 86400 to produce a large positive number. You should probably use a long to store the time difference and reject the data if this difference comes out negative.

With this problem fixed, long rawtime = difftime(mktime(&time1), mktime(&time0)); will compute the time difference between the 2 dates at 0:00:00 am in seconds.

Dividing this by 86400 is not completely correct as the difference may not be an exact multiple of 86400 if there were one or more local time changes due to DST in the interval. I suggest you use:

    total += (rawtime + 3600 * 3) / 86400; // difference in days, ignoring DST.

Alternately, you could define rawtime as a double and use:

    total += round(rawtime / 86400.0);

Finally, computing ts = *localtime(&rawtime); makes no sense at all: converting a number of seconds to a date is only meaningful if this number of seconds represents a valid date. rawtime is a duration, not a valid date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM