Thread: Time calculation problem

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    Smile Time calculation problem

    Hey there, I have been working on creating a program which calculates the number of days inbetween two dates. I have got most of the program going, but it produces some unusal results and i'm not quite sure why. Could anyone point me in the right direction?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    long days(int sm, int sd, int sy, int em, int ed, int ey);
    long factor(int mm, int dd, int yy);
    
    int main(int argc, char *argv[])
    {
    int sm, sd, sy; // starting date
    int em, ed ,ey; // ending date
    long old;
    
    printf("\n Calculate days between two dates .....");
    printf("\n\n Enter first date in mm/dd/yyyy format: ");
    scanf("%d/%d/%d",&sm,&sd,&sy);
    printf("\n Enter second date in mm/dd/yyyy format: ");
    scanf("%d/%d/%d",&em,&ed,&ey);
    old = days(sm,sd,sy,em,ed,ey);
    
    printf("\n There are %ld days between %d/%d/%d and %d/%d/%d\n",
    old,sm,sd,sy,em,ed,ey);
    
    system("PAUSE"); 
    return 0;
    }
    
    long days(int sm, int sd, int sy, int em, int ed, int ey)
    {
    long fac1, fac2, elap;
    
    fac1 = factor(sm, sd, sy);
    fac2 = factor(em, ed, ey);
    elap = fac2 - fac1;
    return (elap);
    }
    
    long factor(int mm, int dd, int yy)
    {
    long xx;
    
    xx = 365 * yy + dd + 31 * (mm - 1);
    if (mm < 3)
    xx = xx + ((yy -1)/4) - (.75 * ((yy - 1)/100)+1);
    else
    xx = xx - (.4 * mm + 2.3) + (yy / 4) - (((.75 * (yy / 100) + 1)));
    return (xx);
    }
    Many thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not familiar with the factor() math. Where did you get it?
    Can you verify that the math is correct when you do this by hand, given the same input that the program fails on?

    If you're not watching the variables in a watch window of an IDE, you may need to insert several print statements to see what's gone off the rails.

    I don't see anything obvious wrong with the code itself.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You need proper indentation! That code is impossible to read.
    Also, why don't you just convert the month, day and year into time_t and then use difftime?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Quote Originally Posted by QuantumPete View Post
    You need proper indentation! That code is impossible to read.
    Also, why don't you just convert the month, day and year into time_t and then use difftime?

    QuantumPete
    I tried to create a code snippet using difftime, but it didn't work. First I had to subtract 70 from the resulting years, since gmtime starts from 00:00:00 on January 1, 1970. But the struct tm starts from 1900. The snippet computed months and years correctly, but incorrect results for days.

  5. #5
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    It appears that your problem involves your variable types. Doing math with floating point values even though you're using integer values is going to cause problems. Using integers, 7/4=1, not 1.75 or 2. Consider using floats or doubles instead of longs (longs are 32-bit integers), especially when you're working with fractional values.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# calculation problem
    By Diablo02 in forum C# Programming
    Replies: 13
    Last Post: 10-25-2007, 08:42 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. time problem
    By sand_man in forum C Programming
    Replies: 9
    Last Post: 09-13-2005, 05:59 PM
  4. problem : input and calculation working together
    By itay390 in forum C Programming
    Replies: 13
    Last Post: 07-30-2005, 12:32 PM
  5. Problem with time on the board, or is it just me?
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 10-23-2002, 05:45 AM