void gettime() { time_t rawtime; struct tm *ptminfo; time(&rawtime); ptminfo = localtime(&rawtime); printf("current: %02d-%02d-%02d %02d:%02d:%02d\n", ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday, ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec); }
比較常用,保存一下。
(相關(guān)資料圖)
C++中與系統(tǒng)時(shí)間相關(guān)的函數(shù)定義在頭文件中。
詳細(xì)介紹
函數(shù)定義如下:
time_t time (time_t* timer);
獲取系統(tǒng)當(dāng)前日歷時(shí)間 UTC 1970-01-01 00:00:00開始的unix時(shí)間戳
Coordinated Universal Time(UTC):
協(xié)調(diào)世界時(shí),又稱為世界標(biāo)準(zhǔn)時(shí)間,也就是大家所熟知的格林威治標(biāo)準(zhǔn)時(shí)間(Greenwich Mean Time,GMT)。比如,中國內(nèi)地的時(shí)間與UTC的時(shí)差為+8,也就是UTC+8。美國是UTC-5。
參數(shù):
timer 存取結(jié)果的時(shí)間指針變量,類型為time_t,指針變量可以為null。
如果timer指針非null,則time()函數(shù)返回值變量與timer指針一樣,都指向同一個(gè)內(nèi)存地址;
否則如果timer指針為null,則time()函數(shù)返回一個(gè)time_t變量時(shí)間。
返回值,如果成功,獲取當(dāng)前系統(tǒng)日歷時(shí)間,否則返回 -1。
二、結(jié)構(gòu)體 struct tm中的屬性
struct tm { int tm_sec; /* seconds after the minute - [0,59] */ int tm_min; /* minutes after the hour - [0,59] */ int tm_hour; /* hours since midnight - [0,23] */ int tm_mday; /* day of the month - [1,31] */ int tm_mon; /* months since January - [0,11] */ int tm_year; /* years since 1900 */ int tm_wday; /* days since Sunday - [0,6] */ int tm_yday; /* days since January 1 - [0,365] */ int tm_isdst; /* daylight savings time flag */};
這結(jié)構(gòu)體,編譯器內(nèi)置。
這里有幾個(gè)地方要注意:
tm_sec 在C89的范圍是[0-61],在C99更正為[0-60]。通常范圍是[0-59],只是某些系統(tǒng)會(huì)出現(xiàn)60秒的跳躍。
tm_mon 是從零開始的,所以一月份為0,十二月份為11,計(jì)算月份的時(shí)候需要加1。
tm_year 計(jì)算年份的時(shí)候需要加上1900
也可以使用本地時(shí)間函數(shù)p=localtime(time_t* nowtime)將nowtime變量中的日歷時(shí)間轉(zhuǎn)化為本地時(shí)間
使用方法:
sec1:上面的方法
#include#include #using namespace std;int main(){ time_t nowtime; //首先創(chuàng)建一個(gè)time_t 類型的變量nowtime struct tm* p; //然后創(chuàng)建一個(gè)新時(shí)間結(jié)構(gòu)體指針 p time(&nowtime); //使用該函數(shù)就可得到當(dāng)前系統(tǒng)時(shí)間,使用該函數(shù)需要將傳入time_t類型變量nowtime的地址值。 p = localtime(&nowtime); //由于此時(shí)變量nowtime中的系統(tǒng)時(shí)間值為日歷時(shí)間,我們需要調(diào)用本地時(shí)間函數(shù)p=localtime(time_t* nowtime)將nowtime變量中的日歷時(shí)間轉(zhuǎn)化為本地時(shí)間,存入到指針為p的時(shí)間結(jié)構(gòu)體中。不改的話,可以參照注意事項(xiàng)手動(dòng)改。 printf("%02d:%02d:%02d\n",p->tm_hour,p->tm_min,p->tm_sec); //控制格式輸出 return 0;}
sec2:借助 strftime()函數(shù)
size_t strftime(char *strDest, //目標(biāo)字符串size_t maxsize,//最多傳出字符數(shù)量const char *format,//格式化方式const struct tm *timeptr //tm指針,上文講的那個(gè));
我們單獨(dú)拿出 *format來
%a 星期幾的簡寫%A 星期幾的全稱%b 月份的簡寫%B 月份的全稱%c 標(biāo)準(zhǔn)的日期的時(shí)間串%C 年份的前兩位數(shù)字%d 十進(jìn)制表示的每月的第幾天%D 月/天/年%e 在兩字符域中,十進(jìn)制表示的每月的第幾天%F 年-月-日%g 年份的后兩位數(shù)字,使用基于周的年%G 年份,使用基于周的年%h 簡寫的月份名%H 24小時(shí)制的小時(shí)%I 12小時(shí)制的小時(shí)%j 十進(jìn)制表示的每年的第幾天%m 十進(jìn)制表示的月份%M 十時(shí)制表示的分鐘數(shù)%n 新行符%p 本地的AM或PM的等價(jià)顯示%r 12小時(shí)的時(shí)間%R 顯示小時(shí)和分鐘:hh:mm%S 十進(jìn)制的秒數(shù)%t 水平制表符%T 顯示時(shí)分秒:hh:mm:ss%u 每周的第幾天,星期一為第一天 (值從1到7,星期一為1)%U 第年的第幾周,把星期日作為第一天(值從0到53)%V 每年的第幾周,使用基于周的年%w 十進(jìn)制表示的星期幾(值從0到6,星期天為0)%W 每年的第幾周,把星期一做為第一天(值從0到53)%x 標(biāo)準(zhǔn)的日期串%X 標(biāo)準(zhǔn)的時(shí)間串%y 不帶世紀(jì)的十進(jìn)制年份(值從0到99)%Y 帶世紀(jì)部分的十制年份%z,%Z 時(shí)區(qū)名稱,如果不能得到時(shí)區(qū)名稱則返回空字符。
舉例子
strftime(tmpbuf,128,"Todayis%A,day%dof%Bintheyear%Y.\n",newtime);//Today is Saturday, day 30 of July in the year 2005.strftime(timE,80,"Data:\n%Y-%m-%d\nTime:\n%I:%M:%S\n",timeinfo);//Data://2010-09-02//Time://04:22:11strftime(str,sizeof(str),"Itisnow%I%p",ptr);//It is now 4PM
具體調(diào)用方式
#include#include using namespace std;int main(){ time_t timep; time(&timep); char tmp[256]; strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&timep)); cout << tmp << endl; return 0;}
標(biāo)簽: 系統(tǒng)時(shí)間 標(biāo)準(zhǔn)時(shí)間 指針變量