名稱空間
變體
操作

std::chrono::system_clock::to_time_t

來自 cppreference.com
< cpp‎ | chrono‎ | system_clock
 
 
 
 
static std::time_t to_time_t( const time_point& t ) noexcept;
(C++11 起)

t 轉換為 std::time_t 型別。

std::time_t 精度較低,則實現定義值是舍入還是截斷。

目錄

[編輯] 引數

t - 要轉換的系統時鐘時間點

[編輯] 返回值

表示 tstd::time_t 值。

[編輯] 示例

以兩種方式獲取當前時間為 std::time_t

#include <chrono>
#include <ctime>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;
 
int main()
{
    // The old way
    std::time_t oldt = std::time({});
 
    std::this_thread::sleep_for(2700ms);
 
    // The new way
    auto const now = std::chrono::system_clock::now();
    std::time_t newt = std::chrono::system_clock::to_time_t(now);
 
    std::cout << "newt - oldt == " << newt - oldt << " s\n";
}

可能的輸出

newt - oldt == 3 s

[編輯] 參閱

[靜態]
std::time_t 轉換為系統時鐘時間點
(public static member function) [編輯]