#include <iostream>
#include <ctime>
using namespace std;
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC 1000
#endif
int main()
{
char tempbuf[128];
struct tm * mytime;
time_t myclock;
clock_t start ,finish;
time_t tstart ,tfinish ,tcurr;
double duration;
long count = 600000000L;
time(&myclock);
mytime = localtime(&myclock);
cout << "Usage of asctime(): " << endl;
//ssctime() 用于将指定的时间以字符串的形式输出。
cout << "asctime() : Current Data and Time :" << asctime(mytime) << endl;
cout << "Usage of ctime(): " << endl;
cout << "ctime(): Current Data and Time :" << ctime(&myclock) << endl;
cout << "Usage of clock(): " << endl;
start = clock();
while(count--);
finish = clock();
duration = (double)(finish - start)/CLOCKS_PER_SEC;
cout << "clock(): Consume " << duration << " second." << endl << endl;
cout << "Usage of difftime() :" << endl;
count = 600000000L;
time(&tstart);
while(count--);
time(&tfinish);
duration = tfinish - tstart;
cout << "time(): Consume " << duration << "seconds." << endl << endl;
cout << "Usage of gmtime() (GMT):" << endl;
time(&tcurr);
mytime = gmtime(&tcurr);
cout << "The current time : " << mytime->tm_year + 1900 << " ," << mytime->tm_mon + 1 << " ," << mytime->tm_mday << " ,";
cout << mytime->tm_hour << " :" << mytime->tm_min << " :" << mytime->tm_sec << " , ";
cout << (mytime->tm_isdst? "Night.":"Daylight") << ", " << mytime->tm_wday << ", " << mytime->tm_yday << "th." << endl;
cout << endl;
cout << "Usage of localtime() :" << endl;
time(&tcurr);
mytime = localtime(&tcurr) ;
cout << "The current time : " << mytime->tm_year + 1900 << " ," << mytime->tm_mon + 1 << " ," << mytime->tm_mday << " ,";
cout << mytime->tm_hour << " :" << mytime->tm_min << " :" << mytime->tm_sec << " , ";
cout << (mytime->tm_isdst? "Night.":"Daylight") << ", " << mytime->tm_wday << ", " << mytime->tm_yday << "th." << endl;
cout << endl;
cout << "Usage of mktime() :" << endl;
tcurr = mktime(mytime);
cout << "The seconds from 1970/1/1 0:0:0" << tcurr << "seconds." << endl << endl;
cout << "Usage of time() :" << endl;
time(&tcurr);
cout << "time transformed by localtime() :" << asctime(localtime(&tcurr)) ;
cout << "time transformed by gmtime() :" << asctime(gmtime(&tcurr)) << endl;
cout << endl;
cout << "Usage of strftime() :" << endl;
mytime = localtime(&tcurr);
strftime(tempbuf ,128 ,"Today is %A ,dat %d of %B in the year %Y.\n" ,mytime);
cout << tempbuf << endl;
return 0;
}
STL中的时间
最新推荐文章于 2024-05-15 13:43:37 发布