C++ Academy · Lesson

Time Points

Represent moments in time.

Lesson 2 of 413 steps

Time Points is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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

Subtracting Time Points

Subtracting one time point from another gives a duration, the elapsed time between the two moments.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    auto a = steady_clock::now();
    auto b = steady_clock::now();
    auto diff = b - a;
    std::cout << "Elapsed ns is non-negative: " << (diff.count() >= 0) << '\n';
    return 0;
}

Adding a Duration

Adding a duration to a time point produces a new time point in the future (or past for subtraction). This is how you compute deadlines.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    using namespace std::chrono_literals;
    auto now = steady_clock::now();
    auto deadline = now + 10s;
    std::cout << "Deadline is after now: " << std::boolalpha << (deadline > now) << '\n';
    return 0;
}

Comparing Time Points

Time points from the same clock support all comparison operators, letting you check whether one moment is before or after another.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    auto first = steady_clock::now();
    auto second = steady_clock::now();
    std::cout << std::boolalpha << (first <= second) << '\n';
    return 0;
}

Clock-Specific Time Points

A time point is tied to its clock's type. You cannot directly compare a system_clock time point with a steady_clock one because their epochs differ.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    system_clock::time_point sys = system_clock::now();
    steady_clock::time_point mono = steady_clock::now();
    std::cout << "Both captured: " << ((sys.time_since_epoch().count() != 0) && (mono.time_since_epoch().count() != 0)) << '\n';
    return 0;
}

Converting to time_t

system_clock::to_time_t turns a calendar time point into a C-style time_t you can format for human display.

#include <iostream>
#include <chrono>
#include <ctime>

int main() {
    using namespace std::chrono;
    auto tp = system_clock::now();
    std::time_t t = system_clock::to_time_t(tp);
    std::cout << "time_t is positive: " << (t > 0) << '\n';
    return 0;
}

from_time_t

The reverse, system_clock::from_time_t, builds a time point from a time_t value such as a stored timestamp.

#include <iostream>
#include <chrono>
#include <ctime>

int main() {
    using namespace std::chrono;
    std::time_t t = 1000000000;
    auto tp = system_clock::from_time_t(t);
    auto back = system_clock::to_time_t(tp);
    std::cout << "Round trip ok: " << std::boolalpha << (back == t) << '\n';
    return 0;
}

Computing a Deadline

A common pattern: capture now(), add a timeout duration, then later compare against a fresh now() to see whether the deadline passed.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    using namespace std::chrono_literals;
    auto deadline = steady_clock::now() + 0ms;
    auto current = steady_clock::now();
    bool expired = current >= deadline;
    std::cout << "Expired: " << std::boolalpha << expired << '\n';
    return 0;
}

Time Point Precision

A time point's precision matches its clock's duration type. You can cast a time point to a coarser unit using time_point_cast.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    auto tp = system_clock::now();
    auto secs_tp = time_point_cast<seconds>(tp);
    std::cout << "Cast preserved sign: " << (secs_tp.time_since_epoch().count() > 0) << '\n';
    return 0;
}

Storing Timestamps

To persist a moment, convert it to a count of seconds since the epoch, store the integer, and reconstruct the time point later when needed.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    auto tp = system_clock::now();
    long long stored = duration_cast<seconds>(tp.time_since_epoch()).count();
    std::cout << "Stored value usable: " << (stored > 0) << '\n';
    return 0;
}

Quick Check

Test your understanding of time point arithmetic.

Recap

You learned that a time_point:

  • marks a specific moment relative to a clock's epoch
  • reports its offset via time_since_epoch()
  • supports adding/subtracting durations and comparisons
  • converts to and from time_t for display and storage

Next, you will use these tools to benchmark and measure code performance.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Time Points” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Time Points”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “Time Points”?

Represent moments in time. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Time Points” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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