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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發布時間:2023/11/30 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 日期与时间(C/C++) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C++繼承了C語言用于日期和時間操作的結構和函數,使用之前程序要引用<ctime>頭文件

有四個與時間相關的類型:clock_t、time_t、size_t、和tm。類型clock_t、size_t、和time_t能夠把系統時間和日期表示為某種整數。

結構體tm把時間和日期以C結構的形式保存,tm結構的定義如下:

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年起的年數int tm_wday; //一周中的第幾天int tm_yday; //一年中的第幾天int tm_isdst; //夏令時 }

相關函數:

函數

描述

time_t time(time_t *time);

該函數返回系統的當前日歷時間。自1970年1月1日以來經過的秒數,如果系統沒有時間,返回-1

char *ctime(const time_t *time);

該函數返回一個表示當地時間的字符串指針,字符串形式day month year hours:minutes:seconds year\n\0

struct tm *localtime(const time_t *time);

該函數返回一個指向表示本地時間的tm結構的指針。

clock_t clock(void);

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

char *asctime(const struct tm *time);

該函數返回一個指向字符串的指針,字符串包含了time所指向結構中存儲的信息,返回的形式為:day month year hours:minutes:seconds year\n\0

struct tm *gmtime(const time_t *time);

該函數返回一個指向time的指針,time為tm結構,用協調世界時(UTC)也被稱為格林尼治標準時間(GMT)表示

time_t mktime(struct tm *time);

該函數返回日歷時間,相當于time所指向結構中存儲的時間

double difftime(time_t time2,time_t time1);

該函數返回time1和time2之間相差的秒數

size_t strftime();

該函數可用于格式化日期和時間為指定的格式

實例:

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

運行結果:

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

?

使用結構體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; }

運行結果:

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格式輸出結果:

#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; }

運行結果:

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

2019-08-05 15:00:14

轉載于:https://www.cnblogs.com/wanghao-boke/p/11305023.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。