Chrono I/O

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 ninth in my detailed journey through the chrono extension in C++20:

Output

Most chrono types, such as time duration, time points, and calendar dates, support direct writing without format specification.

Unformatted

The following tables show the default output format. Let’s start with time durations.

Time Durations

The program displays values for each time duration.

// timeDurationsOutput.cpp

#include <chrono>
#include <iostream>

int main() {

    std::cout << '\n';

    using namespace std::chrono_literals;

    std::cout << "5ns: " << 5ns << '\n';
    std::cout << "std::chrono::nanoseconds(5): " 
              << std::chrono::nanoseconds(5) << '\n';

    std::cout << '\n';

    std::cout << "5ms: " << 5ms << '\n';
    std::cout << "std::chrono::microseconds(5): " 
              << std::chrono::microseconds(5) << '\n';

    std::cout << '\n';

    std::cout << "5us: " << 5us << '\n';
    std::cout << "std::chrono::milliseconds(5): " 
              << std::chrono::milliseconds(5) << '\n';

    std::cout << '\n';

    std::cout << "5s: " << 5s << '\n';
    std::cout << "std::chrono::seconds(5): " << std::chrono::seconds(5) << '\n';

    std::cout << '\n';

    std::cout << "5min: " << 5min << '\n';
    std::cout << "std::chrono::minutes(5): " << std::chrono::minutes(5) << '\n';

    std::cout << '\n';

    std::cout << "5h: " << 5h << '\n';
    std::cout << "std::chrono::hours(5): " << std::chrono::hours(5) << '\n';

    std::cout << '\n';

    std::cout << "std::chrono::days(5): " << std::chrono::days(5) << '\n';

    std::cout << '\n';

    std::cout << "std::chrono::weeks(5): " << std::chrono::weeks(5) << '\n';

    std::cout << '\n';

    std::cout << "std::chrono::months(5): " << std::chrono::months(5) << '\n';

    std::cout << '\n';

    std::cout << "std::chrono::years(5): " << std::chrono::years(5) << '\n';  

    std::cout << '\n';

}

The natural numbers in the square braces of std::chrono::weeks, std::chrono::months, and std::chrono::years represent the number of seconds.

Time Points

When you use the C++20 clocks static member function now, you get the date and the time in the following format.

year-month-day hours:minutes:seconds

The following program shows the current time using all C++20 clocks.

// timePointsOutput.cpp

#include <chrono>
#include <iostream>

int main() {

    std::cout << '\n';

    auto nowSystemClock = std::chrono::system_clock::now();
    std::cout << "nowSystemClock: " << nowSystemClock << '\n';

    auto nowSteadyClock = std::chrono::steady_clock::now();
    // std::cout << "nowSteadyClock: " << nowSteadyClock << '\n';    ERROR
 
    auto nowFileClock = std::chrono::file_clock::now();
    std::cout << "nowFileClock:   " << nowFileClock << '\n';

    auto nowGPSClock = std::chrono::gps_clock::now();
    std::cout << "nowGPSClock:    " << nowGPSClock << '\n';

    // auto nowlocal_tClock = std::chrono::local_t::now();           ERROR

    auto nowTAIClock = std::chrono::tai_clock::now();
    std::cout << "nowTAIClock:    " << nowTAIClock << '\n';

    auto nowUTCClock = std::chrono::utc_clock::now();
    std::cout << "nowUTCClock:    " << nowUTCClock << '\n';

    std::cout << '\n';

}

The program shows two interesting facts. First, the current time given by the std::chrono::steady_clock::now() cannot be displayed. Second, the pseudo clock std::chrono::local_t has no static member function now().

The GPS time is 18 seconds ahead of the UTC time. The TAI time is 37 seconds ahead of the UTC time and 19 seconds ahead of the GPS time.

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (open)
  • "Generic Programming (Templates) with C++": October 2024
  • "Embedded Programming with Modern C++": October 2024
  • "Clean Code: Best Practices for Modern C++": March 2025
  • Do you want to stay informed: Subscribe.

     

    Thanks to the C++17 function std::chrono::floor, you can display the time point in different granularitiy. In this case, the time point has to be of type std::chrono::local_time.

    // timePointsOutputGranularity.cpp
    
    #include <chrono>
    #include <iostream>
    
    int main() {
    
        std::cout << '\n';
    
        auto now = std::chrono::system_clock::now();
        
        auto zonedTime = std::chrono::zoned_time(std::chrono::current_zone(), now); 
        auto localTime = zonedTime.get_local_time();
    
        std::cout << "local_time:                                               " 
                  << localTime << '\n';
    
        std::cout << "std::chrono::floor<std::chrono::microseconds>(localTime): " 
                  << std::chrono::floor<std::chrono::microseconds>(localTime) << '\n'; 
    
        std::cout << "std::chrono::floor<std::chrono::milliseconds>(localTime): "
                  << std::chrono::floor<std::chrono::milliseconds>(localTime) << '\n';
    
        std::cout << "std::chrono::floor<std::chrono::seconds>(localTime):      "
                  << std::chrono::floor<std::chrono::seconds>(localTime) << '\n';
        
        std::cout << "std::chrono::floor<std::chrono::minutes>(localTime):      "
                  << std::chrono::floor<std::chrono::minutes>(localTime) << '\n';
    
        std::cout << "std::chrono::floor<std::chrono::hours>(localTime):        " 
                  << std::chrono::floor<std::chrono::hours>(localTime) << '\n';
    
        std::cout << "std::chrono::floor<std::chrono::days>(localTime):         "
                  << std::chrono::floor<std::chrono::days>(localTime) << '\n';
    
        std::cout << "std::chrono::floor<std::chrono::weeks>(localTime):        "
                  << std::chrono::floor<std::chrono::weeks>(localTime) << '\n';
    
        // std::cout << std::chrono::floor<std::chrono::months>(localTime) << '\n';    ERROR
        // std::cout << std::chrono::floor<std::chrono::years>(localTime) << '\n';     ERROR
    
        std::cout << '\n';
    
    }
    

    The program displays localTime in different accuracies, starting with the time duration std::chrono::microseconds and ending with std::chrono::weeks. Curiously, the time durations for std::chrono::months, and std::chrono::years cannot be displayed, but this will be fixed with C++23.

    What’s Next?

    Additionally, you can display Calendar Dates unformatted.

    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, Matt Godbolt, and Honey Sukesan.

    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 Tipi.build 
    My special thanks to Take Up Code
    My special thanks to SHAVEDYAKS

    Modernes C++ GmbH

    Modernes C++ Mentoring (English)

    Do you want to stay informed about my mentoring programs? Subscribe Here

    Rainer Grimm
    Yalovastraße 20
    72108 Rottenburg

    Mobil: +49 176 5506 5086
    Mail: schulung@ModernesCpp.de
    Mentoring: www.ModernesCpp.org

    Modernes C++ Mentoring,