can someone explain in simple terms how to actually use the following <ctime> functions? I have read thru a few C++ reference websites but the explainations given are very vague and complex.
struct tm: is just a structure that can be use like any other structure. The localtime() function takes a time_t object and returns a pointer to a struct tm.
structtm* tm = localtime(&now); // get struct filled out
cout << "Today is "
<< tm->tm_mon+1// month
<< "/" << tm->tm_mday // day
<< "/" << tm->tm_year + 1900// year with century
<< " " << tm->tm_hour // hour
<< ":" << tm->tm_min // minute
<< ":" << tm->tm_sec; // second
mktime: does just the opposite of localtime(). It takes a struct tm object and converts it to time_t. In addition it may also normalize the struct tm object. Lets say you want to get a struct tm and time_t for some future date, say one month from now. To do that, you first call localtime() as shown above, add 30 days to the tm_mon structure member, then call mktime() to normalize the structure members. mktime() will take care of insuring the structure contains the correct month, day and year (such as for month and year roll-overs).
however i tried creating a tm structure outside the int main() function and it always doesnt work. It seems to only work in int main(). I can't figure out why.
i declared the day mon yr etc as INT types. The compiler doesnt compile if the below is outside the int main().
Example:
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.