日期与时间(C/C++)
C++繼承了C語言用于日期和時間操作的結(jié)構(gòu)和函數(shù),使用之前程序要引用<ctime>頭文件
有四個與時間相關(guān)的類型:clock_t、time_t、size_t、和tm。類型clock_t、size_t、和time_t能夠把系統(tǒng)時間和日期表示為某種整數(shù)。
結(jié)構(gòu)體tm把時間和日期以C結(jié)構(gòu)的形式保存,tm結(jié)構(gòu)的定義如下:
struct tm {int tm_sec; //秒,正常范圍0 ~59,但是允許到61int tm_min; //分 范圍 0~59int tm_hour; //小時 0~23int tm_mday; //一月中的第幾天int tm_mon; //月 0~11int tm_year; //自1900年起的年數(shù)int tm_wday; //一周中的第幾天int tm_yday; //一年中的第幾天int tm_isdst; //夏令時 }相關(guān)函數(shù):
| 函數(shù) | 描述 | 
| time_t time(time_t *time); | 該函數(shù)返回系統(tǒng)的當前日歷時間。自1970年1月1日以來經(jīng)過的秒數(shù),如果系統(tǒng)沒有時間,返回-1 | 
| char *ctime(const time_t *time); | 該函數(shù)返回一個表示當?shù)貢r間的字符串指針,字符串形式day month year hours:minutes:seconds year\n\0 | 
| struct tm *localtime(const time_t *time); | 該函數(shù)返回一個指向表示本地時間的tm結(jié)構(gòu)的指針。 | 
| clock_t clock(void); | 該函數(shù)返回程序執(zhí)行起,處理器時間所使用的時間,如果時間不可用,則返回-1 | 
| char *asctime(const struct tm *time); | 該函數(shù)返回一個指向字符串的指針,字符串包含了time所指向結(jié)構(gòu)中存儲的信息,返回的形式為:day month year hours:minutes:seconds year\n\0 | 
| struct tm *gmtime(const time_t *time); | 該函數(shù)返回一個指向time的指針,time為tm結(jié)構(gòu),用協(xié)調(diào)世界時(UTC)也被稱為格林尼治標準時間(GMT)表示 | 
| time_t mktime(struct tm *time); | 該函數(shù)返回日歷時間,相當于time所指向結(jié)構(gòu)中存儲的時間 | 
| double difftime(time_t time2,time_t time1); | 該函數(shù)返回time1和time2之間相差的秒數(shù) | 
| size_t strftime(); | 該函數(shù)可用于格式化日期和時間為指定的格式 | 
實例:
#include<iostream> #include<ctime> using namespace std;int main() {//基于當前系統(tǒng)日期和時間 初始化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; }運行結(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格式化時間
#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; }運行結(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; }運行結(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)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的日期与时间(C/C++)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 原神哥哥的宝藏鹤观幽灵怎么做
- 下一篇: mysql 怎么查询一年的数据 财
