获取本机时间:
转至http://www.cppblog.com/amyvmiwei/archive/2009/02/23/55412.html
VC中获取得当前本机的时间方法
//方案— 优点:仅使用C标准库;缺点:只能精确到秒级
#include<time.h>
#include<stdio.h>
intmain( void )
{
time_tt = time( 0 );
chartmp[64];
strftime(tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天%z",
localtime(&t));
puts(tmp );
return0;
}
//方案二 优点:能精确到毫秒级;缺点:使用了windowsAPI
#include<windows.h>
#include<stdio.h>
intmain( void )
{
SYSTEMTIMEsys;
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);
return0;
}
//方案三,优点:利用系统函数
#include<stdlib.h>
#include<iostream>
using namespace std;
void main(){
system("time");
}
可以改变电脑的时间设定
方案4:
#include<iostream>
#include<ctime>
using namespace std;
int main(){
time_t now_time;
now_time = time(NULL);
cout<<now_time;
return 0;
}
将当前时间折算为秒级,再通过相应的时间换算即可。
方案5
//
void main() {
LARGE_INTEGER lv;
// 获取每秒多少CPU Performance Tick
QueryPerformanceFrequency( &lv );
// 转换为每个Tick多少秒
double secondsPerTick = 1.0 / lv.QuadPart;
for ( size_t i = 0; i < 100; ++i ) {
// 获取CPU运行到现在的Tick数
QueryPerformanceCounter( &lv );
// 计算CPU运行到现在的时间
// 比GetTickCount和timeGetTime更加精确
double timeElapsedTotal =secondsPerTick * lv.QuadPart;
cout.precision( 6 );
cout << fixed <<showpoint << timeElapsedTotal << endl;
//printf( "%lf \n",timeElapsedTotal ) ;
}
}
按照上面的方法试了一下,做精确延时:
一般的延时都直接用Sleep,http://blog.csdn.net/avagrant158/article/details/4736715 中说使用timeBeginPeriod(1)、timeEndPeriod(1)提高Sleep的精确度,但是调用了感觉木油神马变化,看了网上的一些方法,都是获取时间,然后while循环实现延时,如果不频繁延时还可以接受,若是做成像时钟的一直需要频繁延时的那还是将就着用Sleep吧,用while循环延时根本就不可取,CPU使用率高到爆了(有没有别的办法?)….
下面是各种while延时,我建立一个MFC对话框的工程,添加5个按钮,每个对应一个延时如果提示未声明或找不到标识符需添加#include "Mmsystem.h"
1 clock()
clock_t finish;
finish = clock()+ 1000; //延时1秒
while(clock() <finish)
{
}
2 GetTickCount()
DWORD start= GetTickCount();
DWORD finish = GetTickCount();
do
{
finish = GetTickCount()- start;
}while (finish< 1000); //1s延时
3 timeGetTime()
DWORD start = timeGetTime();
DWORD finish = timeGetTime();
do
{
finish = timeGetTime()- start;
}while (finish< 1000); //1s延时
4 GetLocalTime()
unsigned long i = 0;
SYSTEMTIME start;
SYSTEMTIME finish;
GetLocalTime(&start);
do
{
GetLocalTime(&finish);
i = ((finish.wHour-start.wHour)*3600+(finish.wMinute-start.wMinute)*60 +(finish.wSecond-start.wSecond))*1000+(finish.wMilliseconds-start.wMilliseconds);
}while(i<1000); //1S
5 QueryPerformanceCounter()
LARGE_INTEGER litmp;
LONGLONG QPart1,Qpart2;
double dfMinus,dfFreq,dfTim;
if (!QueryPerformanceFrequency(&litmp))
{
MessageBox("NotSupport!","Not Support",MB_ICONEXCLAMATION | MB_OK);
}
dfFreq = (double)litmp.QuadPart; //获取计数器的时钟频率(非CPU!)
QueryPerformanceCounter(&litmp);
QPart1 = litmp.QuadPart; //获取初始值
do
{
QueryPerformanceCounter(&litmp);
Qpart2 = litmp.QuadPart;
dfMinus = (double)(Qpart2- QPart1);
dfTim = dfMinus/ dfFreq; //获取对应的时间值(单位:秒)
}while (dfTim<1);
类似的用以上的方法也可做计时,第五个最精确。用定时器的话可以用SetTimer()、timeSetEvent(),都说后者比较精确。
MMRESULT timer_id = NULL;
timer_id= timeSetEvent(3000, 1, (LPTIMECALLBACK)onTimeFunc,(DWORD_PTR)this,TIME_PERIODIC);//每3秒调用一次
//响应函数
void WINAPI onTimeFunc(UINT wTimerID, UINT msg,DWORD dwUser,DWORD dwl,DWORD dw2)
{
CXXXDlg * proc= (CXXXDlg *)dwUser;
proc->MessageBox("onTimeFunc() ");
return;
}
//结束
timeKillEvent(timer_id);
参考:
RTDSC揭秘
http://hi.baidu.com/wooutstanding/blog/item/c997205ea9ea649d800a18e9.html
VC中基于 Windows 的精确定时
http://www.vckbase.com/document/viewdoc/?id=1301
C
http://wenku.baidu.com/view/69d4c18a84868762caaed5b5.html