Date Comparison

Discussion in 'C' started by jkappers, May 30, 2007.

  1. jkappers

    jkappers New Member

    Joined:
    May 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I need assistance please. I'm not a new programmer, but I just started learning C for a project I've been given at work. I am trying to write some code to work along with QuickTest Professional. The issue I'm running into is writing a date comparison function using only C.

    I have two dates in format mm/dd/yyyy. I need to compare the dates to see if they are 5 days apart or more. If true then execute some code. I've made several different attempts at this and all of them are loaded with compile errors. I fix one and it just causes another problem because really, C has me confused so far. :confused:

    I've been looking for tutorials and things but haven't found anything that really shows me how to begin this process. And resources, examples, or assistance would be greatly appreciated!
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    The best thing you can do is convert your dates into a standard form. One common form is the number of seconds elapsed since the epoch. The 'epoch', quite blusteringly, is defined as Jan 1, 1970. Dates in this form can be compared quite easily. In this instance, as in may others, Google is indeed your friend.
     
  3. tailhook123

    tailhook123 New Member

    Joined:
    May 23, 2007
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Do you *just* need to know if they are more than 5 days apart or do you need to know how far they are apart if over 5 days?
     
  4. jkappers

    jkappers New Member

    Joined:
    May 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I need to know:

    if ( DifferenceBetweenDates >= 5 days )

    I'm still searching for some ways to do this and have found the same 'solution' or lead I suppose to a correct way of doing it.

    I found this code and have been attempting to apply it with my alterations to serve my purpose however I'm failing miserably.

    Code:
    /* mktime example: weekday calculator */
    #include <stdio.h>
    #include <time.h>
    
    int main ()
    {
      time_t rawtime;
      struct tm * timeinfo;
      int year, month ,day;
      char * weekday[] = { "Sunday", "Monday",
                           "Tuesday", "Wednesday",
                           "Thursday", "Friday", "Saturday"};
    
      /* prompt user for date */
      printf ("Enter year: "); scanf ("%d",&year);
      printf ("Enter month: "); scanf ("%d",&month);
      printf ("Enter day: "); scanf ("%d",&day);
    
      /* get current timeinfo and modify it to the user's choice */
      time ( &rawtime );
      timeinfo = localtime ( &rawtime );
      timeinfo->tm_year = year - 1900;
      timeinfo->tm_mon = month - 1;
      timeinfo->tm_mday = day;
    
      /* call mktime: timeinfo->tm_wday will be set */
      mktime ( timeinfo );
    
      printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);
      
      return 0;
    }
    
    I'll still keep looking though. And thank you for your replies.
     
  5. jkappers

    jkappers New Member

    Joined:
    May 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Oh, and I don't need to know how far apart they are either.. Just if the distance is >= to 5
     
  6. tailhook123

    tailhook123 New Member

    Joined:
    May 23, 2007
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    You just need to branch it down.

    Absolute value on the subtraction of the two years.

    If its greater than 1 the answer is no.

    if its 1... run a function that checks how close the lower date is to end of the year and how far the higher date is to the start of the year. Once again.. dropping obvious branching scenarios... like the lower date not being in the month of december or the higher date not being in the month of January. If the lower is in December and the higher is in January... add the number of days left in December plus the number of days since the start of January. If that number is less than 5 then they fall within 5 days of each other.

    if its 0... run a function that determines how close they are in the same calendar year. Following the same dropping of obvious branching issues. Absolute value of the subtraction of the two months... once again.. if 2 the answer is no. If its 1 then the same thing you did with years applies with months.. just checking days to end of the month for the lower and days from the start of the month on the lower. If the total is <= 5 the answer is yes.. if not its no. If its 0 the two dates are in the same month subtract the lower from the higher and if that total is <= 5 the answer is yes.. and if not the answer is no.

    Cascade back the appropriate return value. Just keep functioning it out with logic trees until you get it down.
     
  7. tailhook123

    tailhook123 New Member

    Joined:
    May 23, 2007
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Got a little ahead of myself in seeing you wanted >= 5 days but same thing applies. If you need >= 5 days just ! the return value /shrug. As long as you can show it fails being closer than 5 days.. you then prove that its greater than 5 days.
     
  8. jkappers

    jkappers New Member

    Joined:
    May 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Well, what's really got me thrown off at this point is that I'm used to new languages (for this example I'll use Javascript) where using dates is as easy as creating a Date() object and using methods like getMonths() to get the months integer value.

    Here, the only way I can think to do this would be to create a string and somehow grab the integert values out of the date string. I assume there are some functions in <string.h> that could help me achieve this.

    I understand that there is no definate string data type in C which confuses me a bit more, but I'm reading some things now to better understand a few of these concepts.

    I'm now sure i understand exactly what you were getting at in the above post. I'm sure what you're saying makes sense, I'm just C illiterate at the moment. I'm sure I'll get it once I read a little more. heh.
     
  9. tailhook123

    tailhook123 New Member

    Joined:
    May 23, 2007
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Yup.. C is pretty nitty gritty.

    No need.. just use your tm struct like you do above and pass two of that variable to a function that does the work. You allready set year, month, day to specific values.. getting them back is just as easy. If you need the year out of your tm struct just do:

    int year = timeinfo->tm_year; <--- bear in mind the year is minus 1900.

    Stop thinking about it as an object and call a function to do the work.

    int Determine_Greater_Than_5_Days(struct tm *Date1, struct tm *Date2)
    {
    return !Determine_Less_Than_5_Days(Date1, Date2);
    }

    int Determine_Greater_Than_5_Days(struct tm *Date1, struct tm *Date2)
    {
    int Temp_Year = timeinfo -> tm_year; // etc

    // Branch based on Year/Month/Day for the two Dates in the style I wrote up above. Make sure to keep functioning out each branch so its readable.
    }
     
  10. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You can farble around with that all you want to, but mktime returns a time value that is as I mentioned in my original response. You can call that with two different dates and get their time values. Then, if you can multiply 5 days by 24 hours by 60 minutes by 60 seconds to get a difference figure, you're home free, right? :rolleyes:
     
  11. tailhook123

    tailhook123 New Member

    Joined:
    May 23, 2007
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Sure.. if the specified problem was to find if any two dates between January 1st, 1970 and January 19, 2038 were within 5 days of each other. But he specifically stated he wanted dates of the form mm/dd/yyyy which include anything from Jan 1st, 0001 to year Dec 31st, 9999. And if you have to do the general form anyways.. there is no reason to slow your code down even touching _mktime.
     
  12. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Slow your code down? Get real. He's doing this calculation every few milliseconds? He's measuring intervals back before he even knew what calendar was in use? The year 3000 is far too close to take a chance on? Bub, you need to achieve some practicality.
     
  13. tailhook123

    tailhook123 New Member

    Joined:
    May 23, 2007
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Yes.. slow your code down.

    You have no idea what he's using this for and assuming anything means you're wrong. Wrong in the same way that people felt they could store years using only 2 digits. If the year 2000 debacle taught us anything its that you simply can't make assumptions on how long this code will be used for, or where. The customer says he wants dates in the form of mm/dd/yyyy and you'd better be able to handle mm/dd/yyyy and not come back to them and say 'Sorry pal.. I can do it.. but we need to keep the dates between 1970 and 2038'.

    The fact is.. its a straightforward problem to address and you do not need to go about it in a round-a-bout manner that not only takes a quite a bit of processing time to accomplish.. but fails if the dates do not stick to your specified constraints.

    I am infinitely practical.. I solve the solution per the requirements given.. preferably using only the processing time needed to actually solve the problem.

    If two dates aren't in concurrent years or the same year.. what are they? More than 5 days apart. In that situation it takes really no processing whatsoever other than a subtraction, a branch, with a return. Even breaking down to the wildest of the wild situations.. like 2 dates in the same year happen to cross what might be a leap day ... the years are divisible by 4 and 100 but not by 400... the code in that situation would be over and done with before your first _mktime call even returned an answer.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice