Link to home
Start Free TrialLog in
Avatar of llagreca
llagreca

asked on

Comparing times that user input

In a file, one field is a date. Long as that date is between the two dates the user inputs the program runs. Right now I just have the user inputting a string. How would I compare the date field in the file to the dates(strings) that the user inputs?
Thank you.
Avatar of Paul Maker
Paul Maker
Flag of United Kingdom of Great Britain and Northern Ireland image

convert them both to a tm time structure. then compare each feild with a function such as the one below

/* this fuction takes two dates as tm structures and compares them
   it returns
   -1 if Date1 > Date2
   0  if Date1 = Date2
   1  if Date1 < Date2 */
int date_compare(struct tm *file_date, struct tm *todaysdate)
{
     if(file_date->tm_year == todaysdate->tm_year && file_date->tm_mon == todaysdate->tm_mon && file_date->tm_mday == todaysdate->tm_mday)
     {
          return 0;
     }
     if(file_date->tm_year < todaysdate->tm_year)
     {
          return 1;          
     }
     if(file_date->tm_year <= todaysdate->tm_year && file_date->tm_mon < todaysdate->tm_mon)
     {
          return 1;
     }    
     if(file_date->tm_year <= todaysdate->tm_year && file_date->tm_mon <= todaysdate->tm_mon && file_date->tm_mday < todaysdate->tm_mday)
     {
          return 1;
     }
     return - 1;
}
Avatar of llagreca
llagreca

ASKER

How do you convert the sting to a tm time structure?
im assuming here you know of tm structure. if not you have probably noticed the data items from my function i posted.

just pass the string .. pulling it appart and putting each bit in the tm..

i.e.

get the day and pout it in the day field using an appropriate data type converstion function.

i have written a great data parser that returns a tm structure.

it takes the forllowing formats and returns NULL if it cannt convert it

dd/mm/yyyy
dd/mm/yy
dd/abreviated month/yyyy
dd/full monthe name/yyyy

the / can be either '\/:- '
I am still not sure how to do it. say the user inputs
12041999
for dec 4, 1999  .  could you give me an example of how to convert that string to the tm sturct. thanks.
the thing is some where you will have to tell the user or at least provide a guidline as to the format to enter ther date..

in the above instance take the first 2 chars and copy them to a char *, then use atoi to convert the char* to an integer which you can store in the relevenat field in the tm struct i.e.

thedate->tm_mon = atoi(day);

after this take the next two for the month then the last four for the year....
// easy way of doing it,,,, you must check that then length
// of the users string eneted is at least 8 otherwise
// memeory errrrrorrrrrrrrs

char temp[5];
char date[12];
strcpy(date,"12122000");
struct tm date_s;


temp[0] = date[0];
temp[1] = date[1];
temp[2] = '\0';
date_s.tm_mday = atoi(temp);

temp[0] = date[2];
temp[1] = date[3];
temp[2] = '\0';
date_s.tm_month = atoi(temp);

temp[0] = date[4];
temp[1] = date[5];
temp[2] = date[6];
temp[3] = date[7];
temp[4] = '\0';
date_s.tm_year = atoi(temp);
Thank you very much for your help. One more question, are struct tm  and time_t the same, or are they different? I was thinking of comparing the times using difftime, but that needs two time_t values. Thanks again.
ASKER CERTIFIED SOLUTION
Avatar of Paul Maker
Paul Maker
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial