C++獲取本地時(shí)間常見方法匯總
1.跨平臺(tái)方法
1.1方法一:手動(dòng)暴力法
#include <iostream>
using namespace std;
#include <time.h>
time_t t = time(NULL);
struct tm* stime=localtime(&t);
char tmp[32]={NULL};
sprintf(tmp, "%04d-%02d-%02d %02d:%02d:%02d",1900+stime->tm_year,1+stime->tm_mon,stime->tm_mday, stime->tm_hour,stime->tm_min,stime->tm_sec);
cout<<tmp<<endl;
輸出結(jié)果:
2015-04-02 23:12:56
1.2方法二:投機(jī)取巧法
#include <iostream>
using namespace std;
#include <time.h>
time_t t = time(0);
char tmp[32]={NULL};
strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&t));
cout<<tmp<<endl;
輸出結(jié)果:
2015-04-02 23:12:56
1.3方法三:簡(jiǎn)獲日歷時(shí)間法
#include <iostream>
using namespace std;
#include <time.h>
#include <string>
time_t tm;
time(&tm);
char tmp[128]={NULL};
strcpy(tmp,ctime(&tm));
//或者
//struct tm* stime=localtime(&tm);
//strcpy(tmp,asctime(stime));
cout<<tmp<<endl;
輸出結(jié)果:
Fri Aug 14 23:19:42 2015
2.Windows平臺(tái)獲取時(shí)間
#include <iostream>
using namespace std;
#include <time.h>
#include <windows.h>
SYSTEMTIME sys;
GetLocalTime(&sys);
char tmp[64]={NULL};
sprintf(tmp,"%4d-%02d-%02d %02d:%02d:%02d ms:%03d",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds);
cout<<tmp<<endl;
輸出結(jié)果:
2015-08-14 23:41:56 ms:354
3.Unix平臺(tái)獲取時(shí)間
#include <sys/time.h>
#include <unistd.h>
struct timeval now_time;
gettimeofday(&now_time, NULL);
time_t tt = now_time.tv_sec;
tm *temp = localtime(&tt);
char time_str[32]={NULL};
sprintf(time_str,"%04d-%02d-%02d%02d:%02d:%02d",temp->tm_year+ 1900,temp->tm_mon+1,temp->tm_mday,temp->tm_hour,temp->tm_min, temp->tm_sec);
cout<<tmp<<endl;
輸出時(shí)間:
2015-08-14 23:41:56
4.需知知識(shí)點(diǎn)
(1)UTC (Coordinated Universal Time):協(xié)調(diào)世界時(shí),又稱世界標(biāo)準(zhǔn)時(shí)間。曾由格林威治平均時(shí)間(Greenwich Mean Time,GMT)提供,現(xiàn)在由原子鐘提供。比如,中國(guó)內(nèi)地的時(shí)間與UTC的時(shí)差為+8,也就是UTC+8。美國(guó)是UTC-5。
(2)Calendar Time:日歷時(shí)間,是用“從一個(gè)標(biāo)準(zhǔn)時(shí)間點(diǎn)到此時(shí)的時(shí)間經(jīng)過的秒數(shù)”來表示的時(shí)間,由time()函數(shù)獲取。這個(gè)標(biāo)準(zhǔn)時(shí)間點(diǎn)對(duì)不同的編譯器來說會(huì)有所不同,但對(duì)一個(gè)編譯系統(tǒng)來說,這個(gè)標(biāo)準(zhǔn)時(shí)間點(diǎn)是不變的,該編譯系統(tǒng)中的時(shí)間對(duì)應(yīng)的日歷時(shí)間都通過該標(biāo)準(zhǔn)時(shí)間點(diǎn)來衡量,所以可以說日歷時(shí)間是“相對(duì)時(shí)間”,但是無論你在哪一個(gè)時(shí)區(qū),在同一時(shí)刻對(duì)同一個(gè)標(biāo)準(zhǔn)時(shí)間點(diǎn)來說,日歷時(shí)間都是一樣的。
(3)Epoch指的是一個(gè)特定的時(shí)間點(diǎn):1970-01-01 00:00:00 UTC,即Unix 時(shí)間戳。
(4)clock tick:時(shí)鐘計(jì)時(shí)單元(而不把它叫做時(shí)鐘滴答次數(shù)),一個(gè)時(shí)鐘計(jì)時(shí)單元的時(shí)間長(zhǎng)短是由CPU控制的。一個(gè)clock tick不是CPU的一個(gè)時(shí)鐘周期,而是C/C++的一個(gè)基本計(jì)時(shí)單位。
在VC++的time.h文件中,我們可以找到相關(guān)的定義:
#ifndef _CLOCK_T_DEFINED typedef long clock_t; #define _CLOCK_T_DEFINED #endif #define CLOCKS_PER_SEC ((clock_t)1000) clock_t clock( void );
這個(gè)函數(shù)返回從“開啟這個(gè)程序進(jìn)程”到“程序中調(diào)用clock()函數(shù)”時(shí)之間的CPU時(shí)鐘計(jì)
時(shí)單元(clock tick)數(shù),在MSDN中稱之為掛鐘時(shí)間(wal-clock)。
//獲取逝去時(shí)間 clock_t start, finish; start=clock(); … finish=clock(); //逝去多少秒 long duration=(finish- start)/ CLOCKS_PER_SEC;
(5) time.h還提供了兩種不同的函數(shù)將日歷時(shí)間(一個(gè)用time_t表示的整數(shù))轉(zhuǎn)換為我們平時(shí)看到的年月日時(shí)分秒分開顯示的時(shí)間格式tm:
struct tm * gmtime(const time_t *timer); struct tm * localtime(const time_t * timer);
其中g(shù)mtime()函數(shù)是將日歷時(shí)間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時(shí)間(即格林尼治時(shí)間),并返回一個(gè)tm結(jié)構(gòu)體來保存這個(gè)時(shí)間,而localtime()函數(shù)是將日歷時(shí)間轉(zhuǎn)化為本地時(shí)間。比如現(xiàn)在用gmtime()函數(shù)獲得的世界標(biāo)準(zhǔn)時(shí)間是2005年7月30日7點(diǎn)18分20秒,那么我用localtime()函數(shù)在中國(guó)地區(qū)獲得的本地時(shí)間會(huì)比世界標(biāo)準(zhǔn)時(shí)間晚8個(gè)小時(shí)。
(6)分解時(shí)間就是以年、月、日、時(shí)、分、秒等分量保存的時(shí)間結(jié)構(gòu),在C/C++中是tm結(jié)構(gòu)。我們可以使用mktime()函數(shù)將用tm結(jié)構(gòu)表示的時(shí)間轉(zhuǎn)化為日歷時(shí)間。其函數(shù)原型如下:
time_t mktime(struct tm * timeptr);
該函數(shù)與gmtime和localtime函數(shù)具有相反的作用。
以上就是C++獲取本地時(shí)間常見方法匯總的詳細(xì)內(nèi)容,更多關(guān)于C++ 獲取本地時(shí)間的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++中volatile關(guān)鍵字的使用詳解以及常見的誤解
volatile 關(guān)鍵字是一種類型修飾符,用它聲明的類型變量表示可以被某些編譯器未知的因素更改,比如:操作系統(tǒng),硬件或者其他線程等2020-01-01
C語言標(biāo)準(zhǔn)庫(kù)<math.h>和<setjmp.h>的實(shí)現(xiàn)
本文主要介紹了C語言標(biāo)準(zhǔn)庫(kù)<math.h>和<setjmp.h>的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11

