-1

I'm trying to write a function that finds the difference in days between the current date and the date given in a formated string (like dd.mm.yy H:M:S), but mktime returns -1 every single time. Tried initializing both time structs with zeros, but that didn't help.

And also, changing time_t to size_t makes it work with mktime(now), but not with mktime(parsedDate)

I'm really confused by this, what am I doing wrong?

I'm using MSVC2019 (v142) on Windows 10.

int findDifferenceInDays(char* dateStr) 
{   
    // Date/time format example: 
    // 12.09.2018 13:44:30
    char date[] = "12.09.2018 13:44:30";
    struct tm *now, *parsedTime;
    time_t t_now = time(0);
    now = localtime(&t_now);
    time_t temp_time = time(&temp_time);
    parsedTime = localtime(&temp_time);
    int day, month, year, hour, minutes, seconds;
    sscanf(dateStr, "%d.%d.%d %d:%d:%d",
        &day, &month, &year, &hour, &minutes, &seconds);
    parsedTime->tm_year = year - 1990;
    parsedTime->tm_mon = month - 1;
    parsedTime->tm_mday = day;
    parsedTime->tm_hour = hour;
    parsedTime->tm_min = minutes;
    parsedTime->tm_sec = seconds;
    time_t endTime, begTime; 
    endTime = mktime(now);
    begTime = mktime(parsedTime);
    double diffInSeconds = difftime(endTime, begTime);
    int daysDifference = difftime(endTime, begTime) / (60*60*24);
    printf("Days between %s and now: %lf\n", dateStr, daysDifference / (60 * 60 * 24));
    return daysDifference;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `now` and `parsedTime` both point to the same structure. – Barmar Dec 20 '19 at 18:00
  • However, I'm not sure why this would cause `mktime()` to return `-1`. It should just cause you to calculate a difference of `0`. – Barmar Dec 20 '19 at 18:03
  • `daysDifference / (60 * 60 * 24)` is an `int`, but you're printing it with `%lf` format, which is for `long double`. – Barmar Dec 20 '19 at 18:09
  • What is the `date` variable for? You never use it. What is the value of `dateStr` that you're passing to the function? – Barmar Dec 20 '19 at 18:11
  • Probably not your immediate error, but `parsedTime->tm_year = year - 1900; /* not 1990 */` – pmg Dec 20 '19 at 18:15
  • I can't reproduce the problem. I get `-1303362930`, not `-1`. The negative number is because of the `1990` typo -- you're setting it to a date in 1928. – Barmar Dec 20 '19 at 18:17
  • Maybe the problem is with how you're printing the `time_t` values. See https://stackoverflow.com/questions/2792551/what-primitive-data-type-is-time-t for recommendations on how to use `printf` with it. – Barmar Dec 20 '19 at 18:21
  • Thanks for your answers, the problem funnily enough was in the `year - 1990` typo. – iryabukhin Dec 20 '19 at 18:24
  • [Your code slightly modified (using 1900!) and cleaned works at ideone](https://ideone.com/macw6U) – pmg Dec 20 '19 at 18:26
  • As you found the reason of your problem, please write an answer and mark it. This makes others happy seeking for help. – the busybee Dec 20 '19 at 18:47
  • @thebusybee Typos don't warrant answers, the question should just be closed. – Barmar Dec 24 '19 at 01:17
  • @Barmar Right, that's even better. – the busybee Dec 24 '19 at 12:57

1 Answers1

1

Probably not your immediate error, but

parsedTime->tm_year = year - 1900; /* not 1990 */
pmg
  • 106,608
  • 13
  • 126
  • 198