Introduction: Why Standard Time Handling in C++ Has Become More Complex (and Better)
For a long time, working with dates and time in C++ was limited to using functions from <ctime>: time(), localtime(), strftime(). This was sufficient for simple tasks, but as project complexity grew (distributed systems, high-load servers, financial applications), more precise, safe, and convenient tools became necessary.
The C++11 standard gave us the <chrono> library, which solves problems of overflow, unit confusion, and platform dependency. C++20 took a significant step forward by adding support for time zones, calendar types, and formatting via std::format. In this article, we will explore advanced techniques that go beyond simply getting the current time.
1. The chrono Library: From Milliseconds to Nanoseconds and Calendars
The core of std::chrono consists of three components: clock, duration, and time_point. At an advanced level, it is important to know how to choose the right clock and work with time arithmetic.
1.1. Choosing a Clock: system_clock, steady_clock, high_resolution_clock
system_clock— represents system time (calendar time). It can be adjusted by the user or a synchronization service (NTP). Used for displaying dates to the user.steady_clock— monotonic clock. It is never adjusted backwards (no rollback). Ideal for measuring intervals (timers, profiling).high_resolution_clock— the clock with the smallest possible tick period. On most platforms, this is a synonym forsteady_clock.
Advanced tip: Never use high_resolution_clock for profiling if you need stability. Use steady_clock.
1.2. Working with duration and time_point
Advanced work involves not just measuring time, but converting between units, arithmetic, and custom types.
#include <iostream>#include <chrono>#include <thread>
int main() { using namespace std::chrono_literals; // for suffixes ms, s, us
// Advanced duration creation auto work_time = 2h + 30min + 15s; // 2 hours 30 minutes 15 seconds std::cout << "Work time in seconds: " << work_time.count() << " s";
// Conversion with rounding (duration_cast) auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(work_time); std::cout << "In milliseconds: " << ms.count() << " ms";
// Measuring function execution time auto start = std::chrono::steady_clock::now(); std::this_thread::sleep_for(100ms); // simulating work auto end = std::chrono::steady_clock::now(); auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start); std::cout << "Function executed in: " << elapsed.count() << " µs";
return 0;}1.3. Calendar Types (C++20)
C++20 added year_month_day, year_month, weekday, and others. This allows safe manipulation of dates without working with raw numbers.
#include <iostream>#include <chrono>
int main() { using namespace std::chrono;
// Creating a date auto date = 2023y / October / 15d; // October 15, 2023 std::cout << "Date: " << date << '';
// Adding days auto next_week = date + days(7); std::cout << "In a week: " << next_week << '';
// Getting the day of the week weekday wd{date}; std::cout <&l