• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

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.
0
llagreca
Asked:
llagreca
  • 5
  • 3
1 Solution
 
makerpCommented:
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;
}
0
 
llagrecaAuthor Commented:
How do you convert the sting to a tm time structure?
0
 
makerpCommented:
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 '\/:- '
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
llagrecaAuthor Commented:
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.
0
 
makerpCommented:
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....
0
 
makerpCommented:
// 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);
0
 
llagrecaAuthor Commented:
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.
0
 
makerpCommented:
no time_t represnts the time as a long interger from midnight jan 1973 or some time around then.

tm is a full structure containing data items for each bit of time and date.

you can convert a tm into time_t. see the docuemtation . theres loads of examles under time management routines.

:)

hope i helped you
0

Featured Post

Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now