Solved

Comparing times that user input

Posted on 2000-04-20
8
183 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
Comment Utility
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
Comment Utility
How do you convert the sting to a tm time structure?
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
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
 

Author Comment

by:llagreca
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:makerp
Comment Utility
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
Comment Utility
// 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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now