創建Time 對象

創建2個Time對象,執行兩for循環,顯示時間延緩!

Time類:

//:C09:Cpptime.h
//A simple time class
#ifndef CPPTIME_H
#define CPPTIME_H
#include<ctime>
#include<cstring>

class Time
{
	std::time_t t;
	std::tm local;
	char asciiRep[27];
	unsigned char lflag ,aflag;
	void updateLocal() 
	{
		if(!lflag) 
		{
			local =*std::localtime (&t);
			lflag++;
		}
	}
	void updateAscii() 
	{
		if(!aflag) 
		{
			updateLocal();
			std::strcpy (asciiRep,std::asctime(&local));
		}
	}
public:
	Time() {mark();}
	void mark() 
	{
		lflag=aflag=0;
		std::time(&t);
	}
	const char * ascii()
	{
		updateAscii();
		return asciiRep;
	}
	int delta(Time *dt) const 
	{
		return int (std::difftime(t,dt->t));
	}
	int daylightSavings()
	{
		updateLocal();
		return local.tm_isdst;
	}
	int dayOfyear()
	{
		updateLocal();
		return local.tm_yday ;
	}
	int dayOfweek()
	{
		updateLocal();
		return local.tm_wday;
	}
	int since1900()
	{
		updateLocal();
		return local.tm_year;
	}
	int month()
	{
		updateLocal();
		return local.tm_mon;
	}
	int dayOfMonth()
	{
		updateLocal();
		return  local.tm_mday;
	}
	int hour()
	{
		updateLocal();
		return local.tm_hour;
	}
	int minute()
	{
		updateLocal();
		return local.tm_min ;
	}
	int second()
	{
		updateLocal();
		return local.tm_sec ;
	}
};
#endif 

測試:

#include "Cpptime.h"
#include<iostream>
using namespace std;
int main()
{
	 Time start;
	 for(int i=1;i<900000;i++)
	 {
		 for(int j=1;j<999;j++){}
		/* cout<<i <<" ";*/
		/* if(i%10==0) cout<<endl;*/
	 }
	 Time end;
	 cout<<endl;
	 cout<<"start= " <<start.ascii()<<endl;
	 cout<<"end= " <<end.ascii ()<<endl;
	 cout<<"delta= "<<end.delta (&start);

}


發佈了306 篇原創文章 · 獲贊 7 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章