Help: using time difftime function

This is a discussion on Help: using time difftime function within the C Programming forums, part of the General Programming Boards category; Hi, I have written a function to check if the age of a person is more than 65. The function ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Red face Help: using time difftime function

    Hi,

    I have written a function to check if the age of a person is more than 65. The function works fine for BirthDate 1937 and after. But for dates before 1937, the difftime output overflows, and so, the result is wrong.

    I tried to declare long double varables to store the result of difftime function and solve the problem, but it didn't work.

    I have attached the code. Thanks for your help.
    FE
    Attached Files Attached Files

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Not very long so I've posted the code for you, people are understandable reluctent to download code...never know what you'll get hehe.

    Code:
    int Age65(const char * BirthDate) {
    
        /*   BirthDate format is dd-mmm-yyyy   */
    
        struct tm  TheDate = {0};
        time_t     Birth;
        char       TempString[5];
        int        rc = 0;
        double     YearInSec = 60*60*24*365.4*65;  /*  65 years old in seconds  */
    
        memset(TempString, 0x00, sizeof(TempString));
    
        TempString[0] = BirthDate[0];
        TempString[1] = BirthDate[1];
        TempString[2] = '\0';
        TheDate.tm_mday = atoi(TempString);
    
        memset(TempString, 0x00, sizeof(TempString));
        TempString[0] = BirthDate[3];
        TempString[1] = BirthDate[4];
        TempString[2] = BirthDate[5];
        TempString[3] = '\0';
    
        if (strcmp(TempString, "JAN") == 0) TheDate.tm_mon =  0;
        if (strcmp(TempString, "FEB") == 0) TheDate.tm_mon =  1;
        if (strcmp(TempString, "MAR") == 0) TheDate.tm_mon =  2;
        if (strcmp(TempString, "APR") == 0) TheDate.tm_mon =  3;
        if (strcmp(TempString, "MAY") == 0) TheDate.tm_mon =  4;
        if (strcmp(TempString, "JUN") == 0) TheDate.tm_mon =  5;
        if (strcmp(TempString, "JUL") == 0) TheDate.tm_mon =  6;
        if (strcmp(TempString, "AUG") == 0) TheDate.tm_mon =  7;
        if (strcmp(TempString, "SEP") == 0) TheDate.tm_mon =  8;
        if (strcmp(TempString, "OCT") == 0) TheDate.tm_mon =  9;
        if (strcmp(TempString, "NOV") == 0) TheDate.tm_mon = 10;
        if (strcmp(TempString, "DEC") == 0) TheDate.tm_mon = 11;
    
        memset(TempString, 0x00, sizeof(TempString));
        TempString[0] = BirthDate[ 7];
        TempString[1] = BirthDate[ 8];
        TempString[2] = BirthDate[ 9];
        TempString[3] = BirthDate[10];
        TempString[4] = '\0';
    
        TheDate.tm_year = atoi(TempString) > 1900 ? atoi(TempString) - 1900 : 0;
    
        Birth = mktime(&TheDate);
    
        if (difftime(time(NULL), Birth) >= YearInSec) {
           rc = 1;
        }
    
        return rc;
    };
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    Seems that nobody can find a solution to this problem!!!

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    OK!

    But the problem is not in mktime(). The problem is with difftime(). The program give correct result for date 1937 and after that (for example, 13-NOV-1937). But when the difference between two dates is more than 2147483647 (long int!), the result makes overflow and becomes negative.

    When I run the attached program for 13-NOV-1920 , I get the following results:

    T2=-1550516400 (You called Birth as T2)
    Diff = difftime(time(NULL), Birth);
    diff: -1707238770.000000
    Div = Diff / (60*60*24*365.4)
    Div: -54.076924

    I expect Div ~= 82
    And, when I use long double for varaibles Diff and Div, I have:

    T2=-1550516400
    diff: -63746071383186921000000000000000000000000000000000 000000.000000,
    Div: 360208603066998260000.000000

    And it is worse!!!

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    The result is

    Epoch=Wed Dec 31 19:00:00 1969

    Now, what shall I do?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    > What does this tell you?
    That it's a good idea to check return values even in simple examples? This code crashed on me because localtime() returned NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21