Working with Dates in C++: Overview of Libraries for Time Manipulation

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

Working with Dates in C++: Overview of Key Libraries



Managing dates and time is one of the fundamental tasks when developing any serious application. From event logging to calculating time intervals and task scheduling, C++ provides developers with several powerful tools. In this article, we will look at three main approaches: the modern standard library std::chrono, the extension by Howard Hinnant (partially included in C++20), and the time-tested Boost.DateTime.



1. Standard Library: std::chrono (C++11 and later)



Starting with C++11, working with time in the standard library has become much more convenient. std::chrono provides types for representing time points (time_point), durations (duration), and clocks (clock). However, until C++20, it did not have convenient tools for working with calendar dates (day, month, year).



Main Components:

  • Clocks: system_clock (system time), steady_clock (monotonic time for measurements), high_resolution_clock (usually an alias for one of the first two).
  • Durations: std::chrono::seconds, milliseconds, microseconds, etc. Allow arithmetic operations.
  • Time points: Binding a duration to a specific clock epoch.


Example: Measuring Execution Time

#include <iostream>#include <chrono>#include <thread>

int main() { auto start = std::chrono::high_resolution_clock::now(); std::this_thread::sleep_for(std::chrono::milliseconds(500)); auto end = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); std::cout << "Execution time: " << duration.count() << " ms"; return 0;}


This code demonstrates precise time measurement with millisecond accuracy. For working with calendar dates, new features appeared in C++20, but if you are using an older standard, the following libraries will be useful.



2. "date" Library by Howard Hinnant (Predecessor to C++20)



Before the release of C++20, Howard Hinnant created the date library, which became the de facto standard for working with calendar dates. It is lightweight, does not require Boost, and works on top of std::chrono. Most of its functionality was incorporated into C++20, but the library itself remains relevant for projects using C++11/14/17.



Main Features:

  • Types year_month_day, year_month, weekday.
  • Parsing dates from strings (parse).
  • Formatting dates (format).
  • Addition/subtraction of days, months, years.
  • Time zone support (extension tz.h).


Example: Working with Dates

#include <iostream>#include <date/date.h>#include <chrono>

int main() { using namespace date; using namespace std::chrono; // Create a date: March 15, 2025 auto d1 = 2025_y / March / 15; std::cout << "Date: " << d1 << ""; // Add 30 days auto d2 = d1 + days{30}; std::cout << "In 30 days: " << d2 << ""; // Difference in days auto diff = (d2 - d1).count(); std::cout << "Difference: " << diff << " days"; // Get the current date auto today = floor<days>(system_clock::now()); std::cout << "Today: " << year_month_day{today} << ""; return 0;}


This

Recommendations