![]() |
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! |
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.
|
Re: Date Comparison
Quote:
|
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 */ |
Re: Date Comparison
Oh, and I don't need to know how far apart they are either.. Just if the distance is >= to 5
|
Re: Date Comparison
Quote:
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. |
Re: Date Comparison
Quote:
|
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. |
Re: Date Comparison
Quote:
Quote:
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. } |
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 22:16. |