Solved

Comparing times that user input

Posted on 2000-04-20
8
227 Views
Last Modified: 2010-08-05
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
Comment
Question by:llagreca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 10

Expert Comment

by:makerp
ID: 2734329
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
 

Author Comment

by:llagreca
ID: 2734369
How do you convert the sting to a tm time structure?
0
 
LVL 10

Expert Comment

by:makerp
ID: 2734397
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:llagreca
ID: 2734501
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
 
LVL 10

Expert Comment

by:makerp
ID: 2734515
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
 
LVL 10

Expert Comment

by:makerp
ID: 2734539
// 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
 

Author Comment

by:llagreca
ID: 2734745
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
 
LVL 10

Accepted Solution

by:
makerp earned 50 total points
ID: 2734772
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question