Solved

Comparing times that user input

Posted on 2000-04-20
8
212 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
  • 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

821 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