//------------------------------------------------------------------- // telltime.cpp // // This example demonstrates use of standard library functions // to display this machine's current time, date, and hostname. // // compile using: $ telltime.cpp -o telltime // execute using: $ ./telltime // // programmer: ALLAN CRUSE // written on: 17 FEB 2009 //------------------------------------------------------------------- #include // for gethostname() #include // for gettimeofday() #include // for localtime(), strftime() #include // for printf(), perror() int main( int argc, char **argv ) { char hostname[ 64 ] = { 0 }; gethostname( hostname, 63 ); struct timeval now; gettimeofday( &now, NULL ); char timebuf[ 40 ] = { 0 }, datebuf[ 40 ] = { 0 }; strftime( timebuf, 40, "%T (%Z)", localtime( &now.tv_sec ) ); strftime( datebuf, 40, "%m-%d-%Y", localtime( &now.tv_sec ) ); printf( "\n\tTime now is %s on %s ", timebuf, datebuf ); printf( "for \'%s\' \n\n", hostname ); }