c\c++ 时间相关函数 include<time.h>



#include <cstdio>
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	clock_t start,end;?/clock_t 与 time_t 都是 long 型的
	long i = 10000000L;
	
	start = clock();
	while(i--);
	end = clock();
	printf("time = %lf\n",(double)(end-start)/1000.0);
	//time = 0.024000
	
	time_t t = time(NULL);
	printf("Calender time is: %ld ms,the distance of 1700-1-1 days = %d\n",t,t/60/60/24);
	//Calender time is: 1394952675 ms,the distance of 1700-1-1 days = 16145
	
	tm * lt;	
	lt = gmtime(&t);
	printf("current time is :%04d %02d %02d , %02d:%02d:%02d\n",lt->tm_year+1900,lt->tm_mon+1,lt->tm_mday,
	lt->tm_hour,lt->tm_min,lt->tm_sec);

	lt = localtime(&t);
	printf("current time is :%04d %02d %02d , %02d:%02d:%02d\n",lt->tm_year+1900,lt->tm_mon+1,lt->tm_mday,
	lt->tm_hour,lt->tm_min,lt->tm_sec);
	//current time is :2014 03 16 , 06:51:15
	//current time is :2014 03 16 , 14:51:15
	
	printf("%s",asctime(lt)); //自动有换行 
	printf("%s",ctime(&t));
	//Sun Mar 16 14:52:18 2014
	//Sun Mar 16 14:52:18 2014
	
	char str[80];
	strftime(str,100,"%Y %B %d %A %H:%M:%S",lt);
	printf("%s\n",str);
	//2014 March 16 Sunday 14:52:18
	
	 
	struct tm t2;
	t2.tm_year = 2014-1900;
	t2.tm_mon = 2;
	t2.tm_mday = 16;
	t2.tm_hour = 11;
	t2.tm_min = 14;
	t2.tm_sec = 0;
	time_t tt2 = mktime(&t2);
	printf("t2=%ld\n",ctime(&tt2));
	printf("%s\n",ctime(&tt2));
	//t2=3280808
	//Sun Mar 16 11:14:00 2014
	return 0;
}