STL中的时间

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值