std::chrono::system_clock::to_time_t

From cppreference.com
< cpp‎ | chrono‎ | system clock
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
 
 
static std::time_t to_time_t( const time_point& t ) noexcept;
(since C++11)

Converts t to a std::time_t type.

If std::time_t has lower precision, it is implementation-defined whether the value is rounded or truncated.

Parameters

t - system clock time point to convert

Return value

A std::time_t value representing t.

Example

Get the current time as a std::time_t two ways.

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

Possible output:

oldt-newt == -3 s

See also

[static]
converts std::time_t to a system clock time point
(public static member function)