Chrono I/O: Unformatted and Formatted

Chrono I/O consists of reading and writing chrono types. The various chrono types support unformatted writing and formatted one with the new formatting library.

This post is the tenth in my detailed journey through the chrono extension in C++20:


Most chrono types, such as time duration, time points, and calendar dates, support direct writing without format specification. Today, I continue with unformatted calendar dates.


The following tables show the default output format of calendar dates.

Calendar Dates

The following table shows the format specifiers, including a short description and an example. For the full description, refer to the page.

// createCalendar.cpp

#include <chrono>
#include <iostream>
int main() {

    std::cout << '\n';

    using namespace std::chrono_literals; 

    using std::chrono::last;

    using std::chrono::year;
    using std::chrono::month;
    using std::chrono::day;

    using std::chrono::year_month;
    using std::chrono::year_month_day;
    using std::chrono::year_month_day_last;
    using std::chrono::year_month_weekday;
    using std::chrono::year_month_weekday_last;
    using std::chrono::month_weekday;
    using std::chrono::month_weekday_last;
    using std::chrono::month_day;
    using std::chrono::month_day_last;
    using std::chrono::weekday_last;
    using std::chrono::weekday;

    using std::chrono::January;
    using std::chrono::February;
    using std::chrono::June;
    using std::chrono::March;
    using std::chrono::October;

    using std::chrono::Monday;
    using std::chrono::Thursday;
    using std::chrono::Sunday;

    constexpr auto yearMonthDay{year(1940)/month(6)/day(26)};   
    std::cout << yearMonthDay << " ";
    std::cout << year_month_day(1940y, June, 26d) << '\n';                   

    std::cout << '\n';

    constexpr auto yearMonthDayLast{year(2010)/March/last};                  
    std::cout << yearMonthDayLast << " ";
    std::cout << year_month_day_last(2010y, month_day_last(month(3))) << '\n';

    constexpr auto yearMonthWeekday{year(2020)/March/Thursday[2]};           
    std::cout << yearMonthWeekday << " ";
    std::cout << year_month_weekday(2020y, month(March), Thursday[2]) << '\n';

    constexpr auto yearMonthWeekdayLast{year(2010)/March/Monday[last]};        
    std::cout << yearMonthWeekdayLast << " ";
    std::cout << year_month_weekday_last(2010y, month(March), weekday_last(Monday));

    std::cout << "\n\n";

    constexpr auto day_{day(19)};      
    std::cout << day_  << " ";
    std::cout << day(19) << '\n';

    constexpr auto month_{month(1)};  
    std::cout << month_  << " ";
    std::cout << month(1) << '\n';

    constexpr auto year_{year(1988)}; 
    std::cout << year_  << " ";
    std::cout << year(1988) << '\n';

    constexpr auto weekday_{weekday(5)};
    std::cout << weekday_  << " ";
    std::cout << weekday(5) << '\n';
    constexpr auto yearMonth{year(1988)/1};
    std::cout << yearMonth  << " ";
    std::cout << year_month(year(1988), January) << '\n';
    constexpr auto monthDay{10/day(22)};
    std::cout << monthDay <<  " ";
    std::cout << month_day(October, day(22)) << '\n';

    constexpr auto monthDayLast{June/last};
    std::cout << monthDayLast << " ";
    std::cout << month_day_last(month(6)) << '\n';
    constexpr auto monthWeekday{2/Monday[3]};
    std::cout << monthWeekday << " ";
    std::cout << month_weekday(February, Monday[3]) << '\n';
    constexpr auto monthWeekDayLast{June/Sunday[last]};
    std::cout << monthWeekDayLast << " ";
    std::cout << month_weekday_last(June, weekday_last(Sunday)) << '\n';

    std::cout << '\n';


Here is the output of the program.


The following tables show the format specifiers, including a short description and an example. Refer to the page for the full description.

The following program uses the time specifier and calendar date specifiers.

// formattedOutputChrono.cpp

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

int main() {

    std::cout << '\n';

    using namespace std::literals;

    auto start = std::chrono::steady_clock::now();   // (1)
    auto end = std::chrono::steady_clock::now();     // (2)
    std::cout << std::format("The job took {} seconds\n", end - start);
    std::cout << std::format("The job took {:%S} seconds\n", end - start);

    std::cout << '\n';

    auto now = std::chrono::system_clock::now();    // (3)  
    std::cout << "now: " << now << '\n';
    std::cout << "Specifier {:%c}: " << std::format("{:%c}\n", now);
    std::cout << "Specifier {:%x}: " << std::format("{:%x}\n", now);
    std::cout << "Specifier {:%F}: " << std::format("{:%F}\n", now);
    std::cout << "Specifier {:%D}: " << std::format("{:%D}\n", now);
    std::cout << "Specifier {:%Y}: " << std::format("{:%Y}\n", now);
    std::cout << "Specifier {:%y}: " << std::format("{:%y}\n", now);
    std::cout << "Specifier {:%b}: " << std::format("{:%b}\n", now);
    std::cout << "Specifier {:%B}: " << std::format("{:%B}\n", now);
    std::cout << "Specifier {:%m}: " << std::format("{:%m}\n", now);
    std::cout << "Specifier {:%W}: " << std::format("{:%W}\n", now);
    std::cout << "Specifier {:%U}: " << std::format("{:%U}\n", now);
    std::cout << "Specifier {:%a}: " << std::format("{:%a}\n", now);
    std::cout << "Specifier {:%A}: " << std::format("{:%A}\n", now);
    std::cout << "Specifier {:%w}: " << std::format("{:%w}\n", now);
    std::cout << "Specifier {:%u}: " << std::format("{:%u}\n", now);
    std::cout << "Specifier {:%e}: " << std::format("{:%e}\n", now);
    std::cout << "Specifier {:%d}: " << std::format("{:%d}\n", now);

    std::cout << '\n';


The call std::chrono::steady_clock::now() (lines 1 and 2) determines the current time. You should use the std::chrono::steady_clock for measurements because this clock is monotonic and cannot be adjusted, such as std::chrono::system_clock (line 3).


Rainer D 6 P2 500x500Modernes C++ Mentoring

Be part of my mentoring programs:

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (starts March 2024)
  • Do you want to stay informed: Subscribe.


    What’s Next?

    You can also apply the format specifier for formatted input.

    Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Stephen Kelley, Kyle Dean, Tusar Palauri, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery,and Matt Godbolt.

    Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.

    My special thanks to Embarcadero
    My special thanks to PVS-Studio
    My special thanks to 
    My special thanks to Take Up Code
    My special thanks to SHAVEDYAKS


    I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

    Standard Seminars (English/German)

    Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

    • C++ – The Core Language
    • C++ – The Standard Library
    • C++ – Compact
    • C++11 and C++14
    • Concurrency with Modern C++
    • Design Pattern and Architectural Pattern with C++
    • Embedded Programming with Modern C++
    • Generic Programming (Templates) with C++
    • Clean Code with Modern C++
    • C++20

    Online Seminars (German)

    Contact Me

    Modernes C++ Mentoring,