精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
C語言獲取系統(tǒng)時間的幾種方式

C語言獲取系統(tǒng)時間的幾種方式

 

C語言中如何獲取時間?精度如何?
1 使用time_t time( time_t * timer ) 精確到秒
2 使用clock_t clock() 得到的是CPU時間 精確到1/CLOCKS_PER_SEC秒
3 計算時間差使用double difftime( time_t timer1, time_t timer0 )
4 使用DWORD GetTickCount() 精確到毫秒
5 如果使用MFC的CTime類,可以用CTime::GetCurrentTime() 精確到秒
6 要獲取高精度時間,可以使用
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
獲取系統(tǒng)的計數(shù)器的頻率
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
獲取計數(shù)器的值
然后用兩次計數(shù)器的差除以Frequency就得到時間。
7 Multimedia Timer Functions
The following functions are used with multimedia timers.
timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime
//*********************************************************************
//用標準C實現(xiàn)獲取當前系統(tǒng)時間的函數(shù)

一.time()函數(shù)

     time(&rawtime)函數(shù)獲取當前時間距1970年1月1日的秒數(shù),以秒計數(shù)單位,存于rawtime 中。
#include "time.h"
void main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "/007The current date/time is: %s", asctime (timeinfo) );
exit(0);
}
=================

#include -- 必須的時間函數(shù)頭文件
time_t -- 時間類型(time.h 定義是typedef long time_t; 追根溯源,time_t是long)

struct tm -- 時間結(jié)構(gòu),time.h 定義如下:
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
time ( &rawtime ); -- 獲取時間,以秒計,從1970年1月一日起算,存于rawtime
localtime ( &rawtime ); -- 轉(zhuǎn)為當?shù)貢r間,tm 時間結(jié)構(gòu)
asctime ()-- 轉(zhuǎn)為標準ASCII時間格式:
星期 月 日 時:分:秒 年

-----------------------------------------------------------------------------
二.clock()函數(shù),用clock()函數(shù),得到系統(tǒng)啟動以后的毫秒級時間,然后除以CLOCKS_PER_SEC,就可以換成“秒”,標準c函數(shù)。
clock_t clock ( void );
#include
clock_t t = clock();
long sec = t / CLOCKS_PER_SEC;
他是記錄時鐘周期的,實現(xiàn)看來不會很精確,需要試驗驗證;
---------------------------------------------------------------------------
三.gettime(&t); 據(jù)說tc2.0的time結(jié)構(gòu)含有毫秒信息
#include
#include
int main(void)
{
struct time t;
gettime(&t);
printf("The current time is: %2d:%02d:%02d.%02d/n",
t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
return 0;
}
time 是一個結(jié)構(gòu)體,, 其中成員函數(shù) ti_hund 是毫秒。。。

--------------------------------------------------------------------------------
四.GetTickCount(),這個是windows里面常用來計算程序運行時間的函數(shù);
DWORD dwStart = GetTickCount();
//這里運行你的程序代碼
DWORD dwEnd = GetTickCount();
則(dwEnd-dwStart)就是你的程序運行時間, 以毫秒為單位
這個函數(shù)只精確到55ms,1個tick就是55ms。
--------------------------------------------------------------------------------
五.timeGetTime()t,imeGetTime()基本等于GetTickCount(),但是精度更高
DWORD dwStart = timeGetTime();
//這里運行你的程序代碼
DWORD dwEnd = timeGetTime();
則(dwEnd-dwStart)就是你的程序運行時間, 以毫秒為單位
雖然返回的值單位應該是ms,但傳說精度只有10ms。
=========================================
//*****************************************************************Unix
##unix時間相關,也是標準庫的
//*********************************************************************
1.timegm函數(shù)只是將struct tm結(jié)構(gòu)轉(zhuǎn)成time_t結(jié)構(gòu),不使用時區(qū)信息;
time_t timegm(struct tm *tm);
2.mktime使用時區(qū)信息
time_t mktime(struct tm *tm);
timelocal 函數(shù)是GNU擴展的與posix函數(shù)mktime相當
time_t timelocal (struct tm *tm);
3.gmtime函數(shù)只是將time_t結(jié)構(gòu)轉(zhuǎn)成struct tm結(jié)構(gòu),不使用時區(qū)信息;
struct tm * gmtime(const time_t *clock);
4.localtime使用時區(qū)信息
struct tm * localtime(const time_t *clock);
1.time獲取時間,stime設置時間
time_t t;
t = time(&t);
2.stime其參數(shù)應該是GMT時間,根據(jù)本地時區(qū)設置為本地時間;
int stime(time_t *tp)
3.UTC=true 表示采用夏時制;
4.文件的修改時間等信息全部采用GMT時間存放,不同的系統(tǒng)在得到修改時間后通過localtime轉(zhuǎn)換成本地時間;
5.設置時區(qū)推薦使用setup來設置;
6.設置時區(qū)也可以先更變/etc/sysconfig/clock中的設置 再將ln -fs /usr/share/zoneinfo/xxxx/xxx /etc/localtime 才能重效
time_t只能表示68年的范圍,即mktime只能返回1970-2038這一段范圍的time_t
看看你的系統(tǒng)是否有time_t64,它能表示更大的時間范圍
//***************************************************************windows
##Window里面的一些不一樣的
//*********************************************************************

一.CTime () 類
VC編程一般使用CTime類 獲得當前日期和時間

CTime t = GetCurrentTime();
SYSTEMTIME 結(jié)構(gòu)包含毫秒信息
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;
SYSTEMTIME t1;
GetSystemTime(&t1)
CTime curTime(t1);
WORD ms = t1.wMilliseconds;
SYSTEMTIME sysTm;
::GetLocalTime(&sysTm);
在time.h中的_strtime() //只能在windows中用
char t[11];
_strtime(t);
puts(t);

//*****************************
獲得當前日期和時間
CTime tm=CTime::GetCurrentTime();
CString str=tm.Format("%Y-%m-%d");
在VC中,我們可以借助CTime時間類,獲取系統(tǒng)當前日期,具體使用方法如下:
CTime t = CTime::GetCurrentTime(); //獲取系統(tǒng)日期,存儲在t里面
int d=t.GetDay(); //獲得當前日期
int y=t.GetYear(); //獲取當前年份
int m=t.GetMonth(); //獲取當前月份
int h=t.GetHour(); //獲取當前為幾時
int mm=t.GetMinute(); //獲取當前分鐘
int s=t.GetSecond(); //獲取當前秒
int w=t.GetDayOfWeek(); //獲取星期幾,注意1為星期天,7為星期六

二.CTimeSpan類
如果想計算兩段時間的差值,可以使用CTimeSpan類,具體使用方法如下:
CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //計算當前系統(tǒng)時間與時間t1的間隔
int iDay=span.GetDays(); //獲取這段時間間隔共有多少天
int iHour=span.GetTotalHours(); //獲取總共有多少小時
int iMin=span.GetTotalMinutes();//獲取總共有多少分鐘
int iSec=span.GetTotalSeconds();//獲取總共有多少秒


------------------------------------------------------------------------------

三._timeb()函數(shù)
_timeb定義在SYS/TIMEB.H,有四個fields
dstflag
millitm
time
timezone
void _ftime( struct _timeb *timeptr );
struct _timeb timebuffer;
_ftime( &timebuffer );
取當前時間:文檔講可以到ms,有人測試,好象只能到16ms!

 

 

四.設置計時器
定義TIMER ID
#define TIMERID_JISUANFANGSHI 2
在適當?shù)牡胤皆O置時鐘,需要開始其作用的地方;
SetTimer(TIMERID_JISUANFANGSHI,200,NULL);
在不需要定時器的時候的時候銷毀掉時鐘
KillTimer(TIMERID_JISUANFANGSHI);
對應VC程序的消息映射
void CJisuan::OnTimer(UINT nIDEvent)
{switch(nIDEvent)}
---------------------------------------------------------------------------------------
##如何設定當前系統(tǒng)時間---------------------------------------windows
SYSTEMTIME m_myLocalTime,*lpSystemTime;
m_myLocalTime.wYear=2003;
m_myLocalTime.wM;
m_myLocalTime.wDay=1;
m_myLocalTime.wHour=0;
m_myLocalTime.wMinute=0;
m_myLocalTime.wSec;
m_myLocalTime.wMillisec;
lpSystemTime=&m_myLocalTime;
if( SetLocalTime(lpSystemTime) ) //此處換成 SetSystemTime( )也不行
MessageBox("OK !");
else
MessageBox("Error !");
SYSTEMTIME m_myLocalTime,*lpSystemTime;
m_myLocalTime.wYear=2003;
m_myLocalTime.wM;
m_myLocalTime.wDay=1;
lpSystemTime=&m_myLocalTime;
if( SetDate(lpSystemTime) ) //此處換成 SetSystemTime( )也不行
MessageBox("OK !");
else
MessageBox("Error !");

 

本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C/C++獲取系統(tǒng)時間
c語言中時間的獲取
c++ 如何獲取系統(tǒng)時間
c/c++中的日期和時間
Windows 獲取當前系統(tǒng)時間函數(shù)總結(jié)
Python中time模塊詳解
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服

主站蜘蛛池模板: 桂阳县| 临泽县| 镇康县| 灵台县| 肇东市| 闸北区| 辉县市| 综艺| 依兰县| 邵武市| 会宁县| 和田县| 巴彦淖尔市| 灵石县| 四平市| 赤城县| 郧西县| 册亨县| 广昌县| 吴江市| 绥阳县| 获嘉县| 房产| 聊城市| 汪清县| 邻水| 宁津县| 酉阳| 龙游县| 临桂县| 阿克苏市| 广水市| 马山县| 从江县| 南岸区| 波密县| 文安县| 滕州市| 霍城县| 广宗县| 山东|