掃二維碼與項(xiàng)目經(jīng)理溝通
我們在微信上24小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
C++編程語言中對于時(shí)間的操作時(shí)一個(gè)比較基礎(chǔ)的操作方法,在實(shí)際編程中經(jīng)常會(huì)用到這一應(yīng)用技巧。今天我們就為大家選擇了C++獲得系統(tǒng)時(shí)間的實(shí)現(xiàn)方式,大家可以以此為參考對象對這方面的應(yīng)用技巧有所掌握。

C++獲得系統(tǒng)時(shí)間方案— 優(yōu)點(diǎn):僅使用C標(biāo)準(zhǔn)庫;缺點(diǎn):只能精確到秒級
- #include < time.h>
- #include < stdio.h>
- int main( void )
- {
- time_t t = time(0);
- char tmp[64];
- strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );
- puts( tmp );
- return 0;
- }
- size_t strftime(char *strDest, size_t maxsize,
const char *format, const struct tm *timeptr);
根據(jù)格式字符串生成字符串。
- struct tm *localtime(const time_t *timer);
取得當(dāng)?shù)貢r(shí)間,localtime獲取的結(jié)果由結(jié)構(gòu)tm返回,返回的字符串可以依下列的格式而定: #t#
C++獲得系統(tǒng)時(shí)間方案二 優(yōu)點(diǎn):能精確到毫秒級;缺點(diǎn):使用了windows API
- #include < windows.h>
- #include < stdio.h>
- int main( void )
- {
- SYSTEMTIME sys;
- GetLocalTime( &sys );
- printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n",
sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,
sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);- return 0;
- }
C++獲得系統(tǒng)時(shí)間方案三,優(yōu)點(diǎn):利用系統(tǒng)函數(shù),還能修改系統(tǒng)時(shí)間,此文件必須是c++文件
- #include< stdlib.h>
- #include< iostream>
- using namespace std;
- void main()
- {
- system("time");
- }
C++獲得系統(tǒng)時(shí)間方案四,將當(dāng)前時(shí)間折算為秒級,再通過相應(yīng)的時(shí)間換算即可,此文件必須是c++文件
- #include< iostream>
- #include< ctime>
- using namespace std;
- int main()
- {
- time_t now_time;
- now_time = time(NULL);
- cout< < now_time;
- return 0;
- }

我們在微信上24小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流