日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

日期与时间(C/C++)

發(fā)布時(shí)間:2023/11/30 c/c++ 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 日期与时间(C/C++) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

C++繼承了C語(yǔ)言用于日期和時(shí)間操作的結(jié)構(gòu)和函數(shù),使用之前程序要引用<ctime>頭文件

有四個(gè)與時(shí)間相關(guān)的類(lèi)型:clock_t、time_t、size_t、和tm。類(lèi)型clock_t、size_t、和time_t能夠把系統(tǒng)時(shí)間和日期表示為某種整數(shù)。

結(jié)構(gòu)體tm把時(shí)間和日期以C結(jié)構(gòu)的形式保存,tm結(jié)構(gòu)的定義如下:

struct tm {int tm_sec; //秒,正常范圍0 ~59,但是允許到61int tm_min; //分 范圍 0~59int tm_hour; //小時(shí) 0~23int tm_mday; //一月中的第幾天int tm_mon; //月 0~11int tm_year; //自1900年起的年數(shù)int tm_wday; //一周中的第幾天int tm_yday; //一年中的第幾天int tm_isdst; //夏令時(shí) }

相關(guān)函數(shù):

函數(shù)

描述

time_t time(time_t *time);

該函數(shù)返回系統(tǒng)的當(dāng)前日歷時(shí)間。自1970年1月1日以來(lái)經(jīng)過(guò)的秒數(shù),如果系統(tǒng)沒(méi)有時(shí)間,返回-1

char *ctime(const time_t *time);

該函數(shù)返回一個(gè)表示當(dāng)?shù)貢r(shí)間的字符串指針,字符串形式day month year hours:minutes:seconds year\n\0

struct tm *localtime(const time_t *time);

該函數(shù)返回一個(gè)指向表示本地時(shí)間的tm結(jié)構(gòu)的指針。

clock_t clock(void);

該函數(shù)返回程序執(zhí)行起,處理器時(shí)間所使用的時(shí)間,如果時(shí)間不可用,則返回-1

char *asctime(const struct tm *time);

該函數(shù)返回一個(gè)指向字符串的指針,字符串包含了time所指向結(jié)構(gòu)中存儲(chǔ)的信息,返回的形式為:day month year hours:minutes:seconds year\n\0

struct tm *gmtime(const time_t *time);

該函數(shù)返回一個(gè)指向time的指針,time為tm結(jié)構(gòu),用協(xié)調(diào)世界時(shí)(UTC)也被稱(chēng)為格林尼治標(biāo)準(zhǔn)時(shí)間(GMT)表示

time_t mktime(struct tm *time);

該函數(shù)返回日歷時(shí)間,相當(dāng)于time所指向結(jié)構(gòu)中存儲(chǔ)的時(shí)間

double difftime(time_t time2,time_t time1);

該函數(shù)返回time1和time2之間相差的秒數(shù)

size_t strftime();

該函數(shù)可用于格式化日期和時(shí)間為指定的格式

實(shí)例:

#include<iostream> #include<ctime> using namespace std;int main() {//基于當(dāng)前系統(tǒng)日期和時(shí)間 初始化0time_t now = time(0);/把now轉(zhuǎn)換成字符串形式char *dt = ctime(&now);cout << "local date and time: " << dt << endl;//把now轉(zhuǎn)化成tm結(jié)構(gòu)tm *gmtm = gmtime(&now);dt = asctime(gmtm);cout << "UTC date and time : " << dt << endl;return 0; }

運(yùn)行結(jié)果:

exbot@ubuntu:~/wangqinghe/C++/time$ ./time

local date and time:? Mon Aug? 5 14:54:25 2019

?

UTC date and time :? Mon Aug? 5 06:54:25 2019

?

使用結(jié)構(gòu)體tm格式化時(shí)間

#include<iostream> #include<ctime> using namespace std;int main() {time_t now = time(0);cout << "from 1970 then the seconds passed : " << now << endl;tm* ltm = localtime(&now);cout << "year : " << 1900 + ltm->tm_year << endl;cout << "month : " << 1 + ltm->tm_mon << endl;cout << "day : " << ltm->tm_mday << endl;cout << "hour : " << ltm->tm_hour << ":";cout << ltm->tm_min << ":";cout << ltm->tm_sec << endl;return 0; }

運(yùn)行結(jié)果:

exbot@ubuntu:~/wangqinghe/C++/time$ ./time1

from 1970? then the seconds passed : 1564988067

year : 2019

month : 8

day : 5

hour : 14:54:27

?

以20xx-xx-xx xx:xx:xx格式輸出結(jié)果:

#include<iostream> #include<ctime> #include<cstdlib> #include<cstdio>using namespace std;string Get_Current_Date();int main() {cout << Get_Current_Date().c_str() << endl;return 0; }string Get_Current_Date() {time_t nowtime;nowtime = time(NULL);char tmp[64];strftime(tmp,sizeof(tmp),"%Y-%m-%d %H:%M:%S",localtime(&nowtime));return tmp; }

運(yùn)行結(jié)果:

exbot@ubuntu:~/wangqinghe/C++/time$ ./time2

2019-08-05 15:00:14

轉(zhuǎn)載于:https://www.cnblogs.com/wanghao-boke/p/11305023.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的日期与时间(C/C++)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。