Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 01-28-2008, 02:32 PM   #1 (permalink)
 
Junior Techie

Join Date: Nov 2005

Posts: 76

sum1nowhere is on a distinguished road

Default ctime

I am trying to compare the time in my C++ program to various other constants. I am using system("time /t"); How can I assign the time to an integer variable so it can a be compared to other things such as: if (variable > 12:00) or something like that.
sum1nowhere is offline  
Old 01-28-2008, 05:03 PM   #2 (permalink)
void's Avatar
 
True Techie

Join Date: Oct 2005

Posts: 198

void

Default Re: ctime

Look at some of the functions in <ctime>. Because "time /t" returns a string you'll have to parse it and generate a struct tm and use mktime() to convert it to time_t. If your using "time /t" to get the current time, don't because time() will give you the current time as time_t. difftime() will allow you to compare two times and return the difference in seconds.
void is offline  
Old 01-29-2008, 09:29 AM   #3 (permalink)
 
Junior Techie

Join Date: Nov 2005

Posts: 76

sum1nowhere is on a distinguished road

Default Re: ctime

Can you give me an example of what you mean? I don't understand the whole 1970 thing.
sum1nowhere is offline  
Old 01-29-2008, 11:16 AM   #4 (permalink)
void's Avatar
 
True Techie

Join Date: Oct 2005

Posts: 198

void

Default Re: ctime

As I understand it you are comparing a string time to the system time.

You need to two time values of type time_t to used by the function difftime(). The current time of the system can be got from the function "time_t time(time_t *);". The second time value can be gotten from somewhere else, it is best to put the values into a struct tm. A struct tm holds the different elements of time such as seconds, minutes, hours etc. By using the function "time_t mktime(struct tm *time)" you can convert the second time into time_t which can be compared. Its best to get the second time first, i.e. the one which isn't the current time.

Code:
//Parse the external time (e.g. one from a file or somewhere else) and populate the structure.
struct tm first_tm;
second_tm.secs = 0;
second_tm.minutes = 1;
second_tm.days = 5;
//etc.

//Convert it to time_t.
time_t first_time = mktime(&first_tm);
//Get the current time.
time_t current_time = time(NULL);
//Returns the different in seconds 
//E.g. 
//	current_time - first_time OR
//	later time - earlier time
double time_different = difftime(current_time, first_time);
A good place to look is here ctime (time.h) - C++ Reference
void is offline  
Old 01-29-2008, 01:16 PM   #5 (permalink)
 
Junior Techie

Join Date: Nov 2005

Posts: 76

sum1nowhere is on a distinguished road

Default Re: ctime

All I want is for a program to give the correct greeting such as good afternoon or something. So I am comparing the current time to a constant such as 12:00. I don't know how to go about getting the check variable into the right format to be compared to the current time. So is it possible to have like if(currtime > noon) so it compares the current time to the constant noon.
sum1nowhere is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On