//---------------------------------------------------------------------------- // // FILE : system_time_and_date.C // AUTHOR : Eugene Wallingford // CREATION DATE : August 29, 1997 // DESCRIPTION : This file demonstrates how to retrieve and use the // current date and time on a Unix system. // //---------------------------------------------------------------------------- // // MODIFIED BY : // DATE : // DESCRIPTION : // //---------------------------------------------------------------------------- #include #include // Include the following file in any file // where you access date or time. It defines // functions to interact with Unix. void main() { // Use the following lines in any *function* where you access date or time. // They create objects to hold the time as formatted in Unix and to provide // access to the time field-by-filed. tm * local_time; // pointer to a tm object time_t current_time; // time_t object to hold current time time( ¤t_time ); // get the current date local_time = localtime( ¤t_time ); // break it up into a tm object // Here's how you access month, day, and year: int day = local_time->tm_mday; int month = local_time->tm_mon + 1; // month in tm is in range [0..11] int year = local_time->tm_year + 1900; // year in tm is number since 1900 // Here's how you access hiurs, minutes, and seconds: int hour = local_time->tm_hour; int minute = local_time->tm_min; int second = local_time->tm_sec; //-------------------------------------------------------------------------- cout << "Current time is: " << hour << ':' << minute << ':' << second << endl << "Current date is: " << month << '/' << day << '/' << year << endl; }