Working with Dates and Time in C++: A Complete Guide to chrono

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 and time in C++: from <ctime> to modern <chrono>



Working with dates and time is one of the key tasks in any serious application: from logs and backups to timers and financial calculations. In C++, two approaches have historically developed: the classic C-style through the <ctime> header file and the modern, type-safe approach through the <chrono> library, which appeared in C++11 and was significantly expanded in C++20. In this article, we will cover both methods, learn how to get the current time, measure intervals, format dates, and avoid common mistakes.



1. Classic approach: <ctime> and std::time_t



Before C++11, the only standard way to work with time was C functions: time(), localtime(), strftime(). This approach is still used in legacy code and simple tasks.



Getting the current time

The std::time(nullptr) function returns the number of seconds since January 1, 1970 (Unix epoch) as an integer std::time_t.



#include <iostream>#include <ctime>

int main() { std::time_t now = std::time(nullptr); std::cout << "Current time (seconds since epoch): " << now << std::endl; return 0;}


Converting to a readable format

To get a structure with components (year, month, day, hours, etc.), use std::localtime() or std::gmtime().



#include <iostream>#include <ctime>

int main() { std::time_t now = std::time(nullptr); std::tm* local = std::localtime(&now);

std::cout << "Year: " << (local->tm_year + 1900) << std::endl; std::cout << "Month: " << (local->tm_mon + 1) << std::endl; std::cout << "Day: " << local->tm_mday << std::endl; return 0;}


Formatting a string with strftime

The std::strftime() function allows you to flexibly format a date into a string according to a given template.



#include <iostream>#include <ctime>

int main() { std::time_t now = std::time(nullptr); std::tm* local = std::localtime(&now);

char buffer[80]; std::strftime(buffer, sizeof(buffer), "%d.%m.%Y %H:%M:%S", local); std::cout << "Current date and time: " << buffer << std::endl; return 0;}


⚠️ Important: std::localtime() and std::gmtime() return a pointer to a static buffer — this is not thread-safe. In C++11, reentrant versions localtime_s (Windows) or localtime_r (POSIX) appeared.



2. Modern C++: the <chrono> library



The <chrono> library offers strong typing, work with different time units, and high precision. Main components:

  • Clocks: system_clock, steady_clock, high_resolution_clock
  • Time points: binding to a specific moment
  • Durations: time intervals (seconds, milliseconds, etc.)


Getting the current time point

#include <iostream>#include <chrono>#include <ctime> // for std::time_t

int main() { auto now = std::chrono::system_clock::now(); std::time_t now_time = std::chrono::system_clock::to_time_t(now); std::cout << "Current time: " << std::ctime(&now_time); return 0;}


Measuring time intervals (timer)

One of the most common tasks is to measure code execution time. For this, it is perfect to use

Blogs

Book Recommendations