⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timefunction.cpp

📁 c++的时间函数,里面绍介了几种调用系统当前时间的函数
💻 CPP
字号:
/*
//方案— 优点:仅使用C标准库;缺点:只能精确到秒级 
#include <time.h> 
#include <stdio.h> 
int main( void ) 

{ 	
	for(int i=0;;i++)
	{	
		time_t t = time( 0 ); 			
			char tmp[64]; 		
			strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z", localtime(&t) ); 				
			puts( tmp );  			
	}	
    return 0; 		
} 
*/
/*
//方案二 优点:能精确到毫秒级;缺点:使用了windows API 
#include <windows.h> 
#include <stdio.h>
#include<iostream.h>
#include<time.h>
int main( void ) 
{ 

	SYSTEMTIME sys; 
    long aa=GetTickCount();//开机到现在的时间,单位是毫秒,从后数的第四位是秒
    cout<<aa<<endl;
	for(int i=0;i<1000000000;i++)
		;
	cout<<GetCurrentTime()-aa<<endl;//这个也是表示以毫秒为单位的计算机启动后经历的时间间隔
	GetLocalTime( &sys ); 
	printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n" 
			,sys.wYear,sys.wMonth,sys.wDay 		
	    	,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds 		
		    ,sys.wDayOfWeek);
	
	return 0;	
}
*/
/*
//方案三,优点:利用系统函数
#include<stdlib.h>
#include<iostream>
using namespace std;
void main()
{	//system("date");
    system("time");	
}
*/
/*
#include<iostream>
#include<time.h>
using namespace std;
int main()
{ 	
	time_t now_time; 	
	now_time = time(NULL); 	
	cout<<now_time<<endl;
	
	long start,end;
	start=clock();    //从0开始计算一段程序运行消耗的时间
	for(int i=0;i<100000000;i++)
		;
	end=clock();
	cout<<end-start<<endl; 
	clock_t   span=clock()-start;
	cout<<span<<endl;
	//span的单位为时钟滴答数,转化为秒钟   
    //相当于double(span/CLK_TCK)秒,   
    //这样能得到很精确的时间跨度 
	return 0;

 }
*/
#include<windows.h>
#include<time.h>
#include<iostream.h>
int main()
{   //cout<<GetCurrentTime()<<endl;
	//Sleep(1000);//延时1000毫秒也就是1秒
	//cout<<GetCurrentTime()<<endl;
/*	
	SYSTEMTIME sys;
	for(int i=0;;i++)
	{
	   system("cls");
       GetLocalTime( &sys );
	   cout<<sys.wHour<<":"<<sys.wMinute<<":"<<sys.wSecond<<"."<<sys.wMilliseconds<<endl;
	   Sleep(1);
	}
*/
	
	for(int i=0;;i++)
	{
		system("cls");
		cout<<(GetCurrentTime()/3600000)%24<<":"<<(GetCurrentTime()/60000)%60<<":"<<(GetCurrentTime()/1000)%60<<endl;//<<"."<<GetCurrentTime()%1000<<endl;
		Sleep(1000);
		//cout<<(GetTickCount()/3600000)%24<<":"<<(GetTickCount()/60000)%60<<":"<<(GetTickCount()/1000)%60<<"."<<GetTickCount()%1000<<endl;
		//Sleep(1000);
	}
}
/*
//精度时控函数     
//在要求误差不大于1毫秒的情况下,可以采用GetTickCount()函数,
//该函数的返回值是DWORD型,表示以毫秒为单位的计算机启动后经历的时间间隔。
//使用下面的编程语句,可以实现50毫秒的精确定时,其误差小于1毫秒。 

	DWORD dwStart,dwStop;
	// 起始值和终止值 
	
   dwStop=GetTickCount();
   // 上一次的终止值变成新的起始值  
  // 此处添加相应控制语句 
   while(true)
   {
	   dwStart=dwStop;
	   do
	   {
		   dwStop=GetTickCount();
	   }while(dwStop-50<dwStart);
   }
   */


//  return 0;
//}


/*
#include "stdio.h"
#include "stdlib.h"
#include "time.h"

int main( void )
{
	long    i = 10000000L;
	clock_t start, finish;
	double  duration;
	// 测量一个事件持续的时间
	printf( "Time to do %ld empty loops is ", i );
	start = clock();
	while( i-- )      ;
	finish = clock();
	duration = (double)(finish - start) / CLOCKS_PER_SEC;
	printf( "%f seconds\n", duration );
	system("pause");
	return 0;
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -