C++20: Basic Chrono Terminology with Time Duration and Time Point

Today, I continue my journey through the basic types with time durations and time points.

Time Durations

C++14 introduced helper types such as std::chrono::seconds for time durations and corresponding time literals such as 5s. C++20 added new helper types. The following table shows all for completeness.

Often the time duration std::chrono::days and the calendar date std::chrono::day are mixed up. The same holds for the time duration std::chrono::years and the calendar date std::chrono::year.  

Distinguish between the time durations std::chrono::days, std::chrono::years, and the calendar types std::chrono::day, std::chrono::year

C++20 added two new literals for new calendar types std::chrono::day and std::chrono::year. The literals d and y refer to a std::chrono::day and std::chrono::year.

  • The day literal represents a day of the month and is unspecified if outside the range [0, 255].
  • The year literal represents a year in the Gregorian calendar and is unspecified if outside the range [-32767, 32767].

The following program emphasizes the difference between std::chrono::days and std::chrono::day and, accordingly, std::chrono::years and std::chrono::year.

// dayDays.cpp

#include <iostream>
#include <chrono>

int main() {

    std::cout << '\n';

    using namespace std::chrono_literals; 

    std::chrono::days days1 = std::chrono::day(30) - std::chrono::day(25);          // (1)
    std::chrono::days days2 = 30d - 25d;                                            // (3)
    if ( days1 == days2 && 
         days1 == std::chrono::days(5)) std::cout << "Five days\n";

    std::chrono::years years1 = std::chrono::year(2021) - std::chrono::year(1998);  // (2)
    std::chrono::years years2= 2021y - 1998y;                                       // (4)
    if ( years1 == years2 && 
         years1 == std::chrono::years(23)) std::cout << "Twenty-three years\n";

    std::cout << '\n';

}

When you subtract two objects of type std::chrono::day (line 1), you get an object of type std::chrono::days. The same holds for the std::chrono::year (lines 2) and std::chrono::years. Thanks to the using declaration using namespace std::chrono_literals, I can directly specify the time literals for std::chrono::day and std::chrono::year (lines 3 and 4).

Include Literals

There are various ways to include the literals.

using namespace std::literals;  
using namespace std::chrono;
using namespace std::chrono_literals;
using namespace std::literals::chrono_literals;
  • using namespace std::literals: includes all C++ literals
  • using namespace std::chrono: includes the entire namespace std::chrono
  • using namespace std::chrono_literals: includes all time literals
  • using namespace std::literals::chrono_literals: includes all time literals

The program literals.cpp shows the use of different using declarations.

// literals.cpp

#include <chrono>
#include <string>

int main() {

    {
        using namespace std::literals;

        std::string cppString = "C++ string literal"s;     // (1)
        auto aMinute = 60s;                                // (2)
        // duration aHour = 0.25h + 15min + 1800s;
    }

    {
        using namespace std::chrono;

        // std::string cppString = "C++ string literal"s;
        auto aMinute = 60s;
        duration aHour = 0.25h + 15min + 1800s;            // (3)
    }

    {
        using namespace std::chrono_literals;

        // std::string cppString = "C++ String literal"s;
        auto aMinute = 60s;
        // duration aHour = 0.25h + 15min + 1800s;
    }

}

The using namespace std::literals declarations enable it to use all built-in literals such as string literal ("C++ string literal"s in line 1) or the time literal (60s in line 2). `std::chrono::duration`cannot be used unqualified. On the contrary, the using declaration using namespace std::chrono allows it to use the time literals and the type std::chrono::duration (line 3) unqualified: duration aHour =  0.25h + 15min + 1800s. Thanks to the using declaration using namespace::std::chrono::literals, all time literals are available.

Time Points

Besides the clock and the time duration, the third fundamental type in C++11 was std::chrono::time_point.

 

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.

     

    template<typename Clock, typename Duration = typename Clock::duration> 
    class time_point;
    

    A std::chrono::time_point depends on the clock and the time duration. C++20 provides aliases for additional time points.

    With the exception of std::chrono::steady_clock, you can define a time point with the specified time duration. All but not the clock std::chrono::file_clock enables it to specify it for seconds. Additionally, std::chrono::local_t and std::chrono::system_clock enables to specify it for days.

    What’s Next?

    std::chrono::hh_mm_ss is the time duration since midnight, split into hours, minutes, seconds, and fractional seconds. This new data type in C++20 stands for the time of day.

    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, Kris Kafka, 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, moon, Philipp Lenk, Hobsbawm, and Charles-Jianye Chen.

    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

    Seminars

    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,