Time in C++

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

int main() {
    auto p = [](const auto& x) { std::cout << x << std::endl; };

    // We'll start by getting the current time.
    auto now = std::chrono::system_clock::now();
    p(std::chrono::system_clock::to_time_t(now));

    // You can build a time point by providing the
    // year, month, day, etc. Times are always associated
    // with a time zone, but C++ doesn't have built-in
    // time zone support, so we'll use UTC.
    std::tm tm{};
    tm.tm_year = 2009 - 1900;
    tm.tm_mon = 11 - 1;
    tm.tm_mday = 17;
    tm.tm_hour = 20;
    tm.tm_min = 34;
    tm.tm_sec = 58;
    auto then = std::chrono::system_clock::from_time_t(std::mktime(&tm));
    p(std::chrono::system_clock::to_time_t(then));

    // You can extract the various components of the time
    // value as expected.
    std::time_t t = std::chrono::system_clock::to_time_t(then);
    std::tm* timeinfo = std::localtime(&t);
    p(timeinfo->tm_year + 1900);
    p(timeinfo->tm_mon + 1);
    p(timeinfo->tm_mday);
    p(timeinfo->tm_hour);
    p(timeinfo->tm_min);
    p(timeinfo->tm_sec);

    // The Monday-Sunday Weekday is also available.
    p(timeinfo->tm_wday);

    // These methods compare two times, testing if the
    // first occurs before, after, or at the same time
    // as the second, respectively.
    p(then < now);
    p(then > now);
    p(then == now);

    // We can compute the duration between two time points.
    auto diff = now - then;
    p(std::chrono::duration_cast<std::chrono::hours>(diff).count());

    // We can compute the length of the duration in
    // various units.
    p(std::chrono::duration_cast<std::chrono::hours>(diff).count());
    p(std::chrono::duration_cast<std::chrono::minutes>(diff).count());
    p(std::chrono::duration_cast<std::chrono::seconds>(diff).count());
    p(std::chrono::duration_cast<std::chrono::nanoseconds>(diff).count());

    // You can use addition to advance a time by a given
    // duration, or subtraction to move backwards by a
    // duration.
    p(std::chrono::system_clock::to_time_t(then + diff));
    p(std::chrono::system_clock::to_time_t(then - diff));

    return 0;
}

This C++ code demonstrates various time-related operations using the <chrono> library, which is the standard way to handle time in modern C++. Here’s an explanation of the key parts:

  1. We use std::chrono::system_clock::now() to get the current time.

  2. To create a specific time point, we use a std::tm structure and std::chrono::system_clock::from_time_t().

  3. We can extract components of a time point by converting it to a std::time_t and then to a std::tm structure.

  4. Comparison between time points is done using standard comparison operators.

  5. We can compute the duration between two time points and convert it to various units using std::chrono::duration_cast.

  6. We can add or subtract durations from time points to move forward or backward in time.

Note that C++ doesn’t have built-in support for time zones, so all times are effectively in the local time zone or UTC. For more advanced time zone handling, you might need to use a third-party library.

To compile and run this program:

$ g++ -std=c++14 time_example.cpp -o time_example
$ ./time_example

The output will show various time-related values and comparisons, similar to the original example.