Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Date Comparison (http://www.go4expert.com/forums/date-comparison-t4491/)

jkappers 30May2007 20:10

Date Comparison
 
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!

DaWei 31May2007 01:05

Re: Date Comparison
 
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.

tailhook123 31May2007 02:45

Re: Date Comparison
 
Quote:

Originally Posted by jkappers
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.

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?

jkappers 31May2007 03:23

Re: Date Comparison
 
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.

jkappers 31May2007 03:24

Re: Date Comparison
 
Oh, and I don't need to know how far apart they are either.. Just if the distance is >= to 5

tailhook123 31May2007 06:40

Re: Date Comparison
 
Quote:

Originally Posted by jkappers
Oh, and I don't need to know how far apart they are either.. Just if the distance is >= to 5

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.

tailhook123 31May2007 06:45

Re: Date Comparison
 
Quote:

Originally Posted by tailhook123
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.

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.

jkappers 31May2007 20:10

Re: Date Comparison
 
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.

tailhook123 31May2007 22:54

Re: Date Comparison
 
Quote:

Originally Posted by jkappers
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.

Yup.. C is pretty nitty gritty.

Quote:

Originally Posted by jkappers
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.

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.
}

DaWei 1Jun2007 05:01

Re: Date Comparison
 
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:


All times are GMT +5.5. The time now is 09:49.