简体   繁体   中英

Difference between two dates C language

!! Edited

The following is a different try, I have rawtime as result, so just seconds and I need to convert in number of days, probably doing rawtime / 86400 . But even with this code windows gives always 0 as result, and xcode works properly.

typedef struct {
    char firstName[20];
    char lastName[20];
    char fiscalCode[17];
    STATESICK stateSick;
    struct tm start;
    struct tm end;
} SICKREGION;

    { "PlutoPluto", "Leonardi", "GRSBDT06B48F839T", stayHome, { 0, 0, 0, 28, 2, 2020 }, { 0, 0, 0, 29, 2, 2020 }},

void timeDiff() {
    double rawtime = 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 < 1; k++) {
                rawtime = difftime(mktime(&region[i].hospital[j].sickregion[k].end),
                                   mktime(&region[i].hospital[j].sickregion[k].start));
            }
        }
    }
    printf("%g\n", rawtime);
}

I'm working on a project for my C class with my colleagues. We have a problem, we need to get the difference between dates taken from structs. The function I made works but just on my mac, on Windows doesn't work. , we have 0 as result, anytime. does anyone knows why? Thanks, Sergio.

Here there's the code:

void timeDiff() {
    struct tm ts;
    char buf[80];
    long rawtime = 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 end;
                end.tm_mday = region[i].hospital[j].sickregion[k].dateHealing.day;
                end.tm_mon = region[i].hospital[j].sickregion[k].dateHealing.month;
                end.tm_year = region[i].hospital[j].sickregion[k].dateHealing.year;

                struct tm start;
                start.tm_mday = region[i].hospital[j].sickregion[k].dateDiagnosis.day;
                start.tm_mon = region[i].hospital[j].sickregion[k].dateDiagnosis.month;
                start.tm_year = region[i].hospital[j].sickregion[k].dateDiagnosis.year;

                rawtime = difftime(mktime(&end), mktime(&start)) ;

                ts = *localtime(&rawtime);
            }
        }
    }
    strftime(buf, sizeof(buf), "%j", &ts);
    printf("%s\n", buf);
}


Initialize your variables

struct tm start = {0};

tm_mday , tm_mon , and tm_year are not the only fields in a struct tm.

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