获取系统时间
#include <iostream>
#include <ctime>
using namespace std;
int main(void)
{
struct tm *times; //定义时间指针变量.....(只能用指针)
time_t t; //定义系统时间变量,供提供系统时间
t = time(0); //当前系统时间给变量t
times = localtime(&t); //把系统时间变量强制转换成tm结构体形式时间
cout << (times->tm_year+1900)<<endl;
cout << times->tm_mon<<endl;
cout << times->tm_mday<<endl;
cout << times->tm_hour<<endl;
cout << times->tm_min<<endl;
cout << times->tm_sec<<endl;
return 0;
}
输出:2006 6 19 22 21 22
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
获得随机数
#include <iostream>
#include <ctime>
using namespace std;
void main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
{
cout<<rand()%10<<endl;
}
}