Namespaces
Variants
Actions

std::chrono::system_clock::to_time_t

From cppreference.com
< cpp‎ | chrono‎ | system clock
 
C++
 
Date and time library
Time point
(C++11)
(C++20)
Duration
(C++11)
Clocks
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Time of day
(C++20)(C++20)
(C++20)(C++20)
(C++20)
Calendar
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
chrono I/O
(C++20)

 
std::chrono::system_clock
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.

Contents

[edit] Parameters

t - system clock time point to convert

[edit] Return value

A std::time_t value representing t.

[edit] Example

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

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

Possible output:

newt - oldt == 3 s

[edit] See also

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

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant