0Pricing
C++ Academy · Lesson

Time Points

Represent moments in time.

What Is a Time Point?

A std::chrono::time_point represents a specific moment measured from a clock's epoch. It pairs a clock with a duration since that epoch.

  • Returned by clock::now().
  • Subtracting two of them yields a duration.
#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    time_point<steady_clock> tp = steady_clock::now();
    std::cout << "Captured a moment: " << (tp.time_since_epoch().count() != 0) << '\n';
    return 0;
}

time_since_epoch

Every time point can report its distance from the clock epoch with time_since_epoch(), which returns a duration.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    auto tp = system_clock::now();
    auto d = tp.time_since_epoch();
    auto s = duration_cast<seconds>(d);
    std::cout << "Positive epoch seconds: " << (s.count() > 0) << '\n';
    return 0;
}

All lessons in this course

  1. Durations and Clocks
  2. Time Points
  3. Measuring Code Performance
  4. Calendar and Time Zones
← Back to C++ Academy