Working with Dates and Time in C++: Advanced Level

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Advanced Work with Dates and Time in C++: From chrono to Time Zones

\n\n

Working with dates and time is one of the key tasks when developing high-load systems, logging, financial applications, and schedulers. In C++, the standard library offers a powerful toolkit, ranging from simple time_t to the modern <chrono>. In this article, we will cover advanced techniques: precise measurements, time zones, formatting, and interval calculations.

\n\n

1. The chrono Library: duration, time_point, and clocks

\n\n

The standard library <chrono> (C++11 and later) provides types for representing points in time (time_point), durations (duration), and clocks (clock). This allows you to write platform-independent code.

\n\n
#include <iostream>\n#include <chrono>\n#include <thread>\n\nint main() {\n    using namespace std::chrono;\n\n    // Record the start time\n    auto start = high_resolution_clock::now();\n\n    // Simulate work\n    std::this_thread::sleep_for(milliseconds(500));\n\n    auto end = high_resolution_clock::now();\n\n    // Calculate the duration\n    auto duration = duration_cast<milliseconds>(end - start);\n    std::cout << "Elapsed: " << duration.count() << " ms\\n";\n\n    // Working with duration in different units\n    seconds sec(1);\n    milliseconds ms = sec; // implicit conversion (lossless)\n    // milliseconds ms2 = duration_cast<milliseconds>(sec); // explicit\n    std::cout << "1 second = " << ms.count() << " ms\\n";\n\n    return 0;\n}
\n\n

Key points:

\n
    \n
  • duration_cast — conversion between units (may lose precision).
  • \n
  • high_resolution_clock — the most precise clock (usually a wrapper over the system timer).
  • \n
  • system_clock — system time tied to the calendar.
  • \n
  • steady_clock — monotonic clock, ideal for measuring intervals.
  • \n
\n\n

2. System Time and Conversion to Calendar Dates

\n\n

To work with calendar dates (year, month, day), use system_clock and the functions to_time_t / from_time_t. Starting from C++20, more convenient tools have appeared, but we will cover the classic approach.

\n\n
#include <iostream>\n#include <chrono>\n#include <ctime>\n\nint main() {\n    using namespace std::chrono;\n\n    // Current system time\n    auto now = system_clock::now();\n    std::time_t now_c = system_clock::to_time_t(now);\n\n    // Convert to local time\n    std::tm* local = std::localtime(&now_c);\n    std::cout << "Local time: " \n              << std::put_time(local, "%Y-%m-%d %H:%M:%S") << "\\n";\n\n    // Create a time_point from a calendar date (C++20 will simplify this)\n    std::tm date = {};\n    date.tm_year = 2025 - 1900; // years since 1900\n    date.tm_mon = 3;            // April (0-11)\n    date.tm_mday = 15;\n    date.tm_hour = 12;\n    date.tm_min = 30;\n    date.tm_sec = 0;\n    std::time_t date_c = std::mktime(&date);\n    auto date_point = system_clock::from_time_t(date_c);\n\n    // Difference in days\n    auto diff = duration_cast<hours>(now - date_point).count() / 24;\n    std::cout << "Days since 15.04.2025: " << diff << "\\n";\n\n    return 0;\n}
\n\n

Important: std::put_time is available from C++11. Format specifiers are similar to strftime.

\n\n

3. Time Zones and C++20

\n\n

Before C++20, working with time zones was non-trivial. In C++20, the <chrono> library introduced support for time_zone and zoned_time. If your compiler supports C++20 (GCC 12+, Clang 15+, MSVC 2022 17.4+), you can use

Blogs

Book Recommendations