Is there a way for my program to take the time from the user's computer clock and store it into some sort of variable? I'd like to put a bonus in my game program for somebody using it in the wee hours of morning for example.
Is there a way for my program to take the time from the user's computer clock and store it into some sort of variable? I'd like to put a bonus in my game program for somebody using it in the wee hours of morning for example.
Some simple function calls and an include will get you on your way:
then to get the current time:Code:#include <time.h>
and ptm contains all of the information about the current time.
Here's the information stored in the ptm structure:
Quoted from msdn
tm_sec
Seconds after minute (0 59)
tm_min
Minutes after hour (0 59)
tm_hour
Hours after midnight (0 23)
tm_mday
Day of month (1 31)
tm_mon
Month (0 11; January = 0)
tm_year
Year (current year minus 1900)
tm_wday
Day of week (0 6; Sunday = 0)
tm_yday
Day of year (0 365; January 1 = 0)
tm_isdst
Positive value if daylight saving time is in effect; 0 if daylight saving time is not in effect; negative value if status of daylight saving time is unknown. The C run-time library assumes the United Statess rules for implementing the calculation of Daylight Saving Time (DST).
and to access each one just use:
or whatever you want to access.Code:ptm->tm_mon
-Hope that helps
Wow! Thank you very much, that's exactly what I need!
>>#include <time.h>
#include <ctime> in C++. The functions are also placed within the std namespace, so qualification is required if the compiler conforms to the standard.
>>
Both of these functions can fail. Always test return values.Code:time_t plaintime; tm *ptm; time(&plaintime); ptm = localtime(&plaintime);
Code:time_t plaintime; tm *ptm; if (time(&plaintime) == (time_t)-1) { // Handle the error } ptm = localtime(&plaintime); if (ptm == 0) { // Handle the error }