時(shí)間操作

1, 時(shí)間戳轉(zhuǎn)換為字符串

#define _CRT_SECURE_NO_WARNINGS  // localtime()需要這個(gè)宏。
#include <iostream>
#include <chrono>
#include <iomanip>   // put_time()函數(shù)需要包含的頭文件。
#include <sstream>
using namespace std;

int main()
{
    // 1)靜態(tài)成員函數(shù)chrono::system_clock::now()用于獲取系統(tǒng)時(shí)間。(C++時(shí)間)
    auto now = chrono::system_clock::now();

    // 2)靜態(tài)成員函數(shù)chrono::system_clock::to_time_t()把系統(tǒng)時(shí)間轉(zhuǎn)換為time_t。(UTC時(shí)間)
    auto t_now = chrono::system_clock::to_time_t(now);

    // t_now = t_now + 24*60*60;   // 把當(dāng)前時(shí)間加1天。
    // t_now = t_now + -1*60*60;   // 把當(dāng)前時(shí)間減1小時(shí)。
    // t_now = t_now + 120;           // 把當(dāng)前時(shí)間加120秒。

    // 3)std::localtime()函數(shù)把time_t轉(zhuǎn)換成本地時(shí)間。(北京時(shí))
    // localtime()不是線程安全的,VS用localtime_s()代替,Linux用localtime_r()代替。
    auto tm_now = std::localtime(&t_now);

    // 4)格式化輸出tm結(jié)構(gòu)體中的成員。
    std::cout << std::put_time(tm_now, "%Y-%m-%d %H:%M:%S") << std::endl;
    std::cout << std::put_time(tm_now, "%Y-%m-%d") << std::endl;
    std::cout << std::put_time(tm_now, "%H:%M:%S") << std::endl;
    std::cout << std::put_time(tm_now, "%Y%m%d%H%M%S") << std::endl;

    stringstream ss;   // 創(chuàng)建stringstream對(duì)象ss,需要包含<sstream>頭文件。
    ss << std::put_time(tm_now, "%Y-%m-%d %H:%M:%S");    // 把時(shí)間輸出到對(duì)象ss中。
    string timestr = ss.str();     // 把ss轉(zhuǎn)換成string的對(duì)象。
    cout << timestr << endl;
}

2, 計(jì)時(shí)器

#include <iostream>
#include <chrono>
using namespace std;

int main()
{
    // 靜態(tài)成員函數(shù)chrono::steady_clock::now()獲取開始的時(shí)間點(diǎn)。
    auto start = chrono::steady_clock::now();

    // 執(zhí)行一些代碼,讓它消耗一些時(shí)間。
    cout << "計(jì)時(shí)開始 ...... \n";
    for (int ii = 0; ii < 1000000; ii++) {
        // cout << "我是一只傻傻鳥。\n";
    }
    cout << "計(jì)時(shí)完成 ...... \n";

    // 靜態(tài)成員函數(shù)chrono::steady_clock::now()獲取結(jié)束的時(shí)間點(diǎn)。
    auto end = chrono::steady_clock::now();

    // 計(jì)算消耗的時(shí)間,單位是納秒。
    auto dt = end - start;
    cout << "耗時(shí): " << dt.count() << "納秒("<<(double)dt.count()/(1000*1000*1000)<<"秒)";
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,525評(píng)論 19 139
  • JS中的數(shù)據(jù)類型在JS中一共有六種數(shù)據(jù)類型* String 字符串* Number 數(shù)值* Boolean ...
    ARGM10閱讀 480評(píng)論 0 0
  • 一、基礎(chǔ)信息 1.1 簡(jiǎn)介 分布式的大數(shù)據(jù)計(jì)算引擎。支持 無(wú)界數(shù)據(jù)流 和 無(wú)限數(shù)據(jù)流 進(jìn)行有狀態(tài)計(jì)算。 對(duì)于fli...
    每天積累多一點(diǎn)閱讀 231評(píng)論 0 0
  • 我們都知道,在 Python 中有各種數(shù)據(jù)類型,例如整數(shù)、浮點(diǎn)數(shù)、字符串等。同時(shí)在開發(fā)腳本或各種算法當(dāng)中,我們應(yīng)該...
    蘿卜大雜燴閱讀 359評(píng)論 0 0
  • Go內(nèi)置的time包提供了時(shí)間顯示和測(cè)量的函數(shù) 時(shí)間可分為時(shí)間點(diǎn)和時(shí)間段,為此Go提供了兩種基礎(chǔ)數(shù)據(jù)類型time....
    JunChow520閱讀 342評(píng)論 0 0

友情鏈接更多精彩內(nèi)容