Thread: Cannot calculate the difference between two dates

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    19

    Cannot calculate the difference between two dates

    I'm trying to calculate the difference between two dates (the current date and a date stored in a structure) expressed in hours, minutes and seconds, but when I print the results, it's not being calculated correctly.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void) {
        struct tm date;
        date.tm_year = 2019 - 1900;
        date.tm_mon = 1 - 1;
        date.tm_mday = 8;
        date.tm_hour = 2;
        date.tm_min = 30;
        date.tm_sec = 0;
        time_t PreviousDate = mktime(&date);
        time_t CurrentDate = time(NULL);
        time_t difference = difftime(CurrentDate, PreviousDate);
        time_t hours, minutes;
        hours = difference / (60*60);
        difference -= difference % (60*60);
        minutes = difference / 60;
        difference -= difference % 60;
        printf("%ld hours, %ld minutes and %ld seconds.\n", hours, minutes, difference);
        return 0;
    }
    This is what's being shown:
    Code:
    19 hours, 1140 minutes and 68400 seconds.
    Which is not right, because the maximum of minutes and seconds should be 60. It seems the remainder of the conversion to minutes is not working, because 1140 minutes are 19 hours and 68400 seconds are 19 hours.

    Could it be that the type time_t can't be operated with integers? If it can't, how should I calculate the difference between those dates?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Your main problem is an incorrect calculation.
    This is wrong:
    Code:
    difference -= difference % (60*60);
    You want something a little simpler:
    Code:
    difference %= (60*60);
    Same thing with calculating minutes.

    And it's best to make difference a long so that you know exactly what it is (time_t could be anything).
    difftime returns a double, but if you aren't interested in the fractional seconds, storing it's return value in a long is good enough.
    Code:
    #include <stdio.h>
    #include <time.h>
     
    int main() {
        struct tm date;
        date.tm_year = 2019 - 1900;
        date.tm_mon = 1 - 1;
        date.tm_mday = 8;
        date.tm_hour = 2;
        date.tm_min = 30;
        date.tm_sec = 0;
        date.tm_isdst = -1; // ask system to try to determine if dst is true or not
     
        time_t PreviousDate = mktime(&date);
        time_t CurrentDate = time(NULL);
        long difference = difftime(CurrentDate, PreviousDate);
     
        int hours = difference / (60 * 60);
        difference %= (60 * 60);
        int minutes = difference / 60;
        difference %= 60;
        printf("%d hours, %d minutes and %ld seconds.\n", hours, minutes, difference);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Aug 2018
    Posts
    19
    Quote Originally Posted by john.c View Post
    Your main problem is an incorrect calculation.
    This is wrong:
    Code:
    difference -= difference % (60*60);
    You want something a little simpler:
    Code:
    difference %= (60*60);
    Same thing with calculating minutes.
    [/code]
    Whoops! I didn't notice it. I understand why the result was always 19 hours now. It's because I was subtracting the remainder, so I was getting the quotient. But with difference %= (60*60); I subtract the quotient to get the remainder, which is the expected result.

    Quote Originally Posted by john.c View Post
    And it's best to make difference a long so that you know exactly what it is (time_t could be anything).
    difftime returns a double, but if you aren't interested in the fractional seconds, storing it's return value in a long is good enough.
    Okay, I'll keep this in mind.

    Thank you very much, john.c.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference between two dates C++
    By hughng92 in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2016, 02:30 AM
  2. GUI for difference between two dates
    By amroto in forum C++ Programming
    Replies: 3
    Last Post: 07-10-2012, 04:52 AM
  3. calculate the days between two dates stored in strings
    By koupoua in forum C Programming
    Replies: 2
    Last Post: 05-29-2012, 05:29 AM
  4. Calculate the number of Days between two dates
    By wablackwell in forum C Programming
    Replies: 1
    Last Post: 04-02-2012, 10:02 PM
  5. difference between dates
    By rahulsk1947 in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2007, 09:57 AM

Tags for this Thread