The purpose of the article is to demonstrate the benefits that the recent features of modern C++ and std::chrono have on various fields, including mathematical modeling applications for Quantitative Finance. Introduction Dealing with time is a common task in various modern industries such as technology, medicine, and finance. It is especially important for Quantitative Finance where time can be an important factor, for instance with Fixed Income Securities. For illustration, we can consider credit products, such as Bonds and CDS (Credit Default Swap). CDS are made fungible nowadays with the introduction of standard coupons. The practical meaning of this is that it should be easy to replace one standard CDS contract with another, and the standard payment CDS dates were introduced with respect to the 20th of March, June, September, and December each year. The periodic payments are quarterly with the payment amounts computed following the ACT/360-day count convention when valuing the premium leg of the CDS. To illustrate this, suppose is the coupon and are accrual (end) dates. C t_0, t_1, ..., t_n According to ACT/360, the amounts of cash flows can be calculated as follows: C_i = C * (t_i - t_{i - 1}) / 360 This simple example demonstrates the crucial role of date and time computations for modern financial pricing applications. The canonical programming language for building pricing libraries is C++, which has to work with time since C++11, and since C++20 it has new features which could help quants to leverage the power of C++ more effectively in their everyday job. std::chrono Those features could be: new clocks: , , utc_clock tai_clock gps_clock year_month_day operator sys_days clock_cast The purpose of this article is to demonstrate, with simple mock examples, how to work with some of these new features. The code was tested on MS Visual Studio 2022 but is expected to be compilable with both GCC and Clang as soon as the corresponding library features are implemented. Let's move on to technical details. Some useful std::chrono features In the first part of the article, we will consider the use of , and . Then, we will create our own clock class to mimic those in . Finally, we will make out clock class convertible to , which is one of the most widely used clock classes. utc_clock year_month_day operator sys_days std::chrono system_clock One must include header into the code to start using Chrono. Next, we will introduce an alias for std::chrono namespace which we will use in the following examples: <chrono> namespace chr = std::chrono; C++20 has a reach set of literals that helps to define a date. We can do this with overload operator / using chr::March; chr::year_month_day const cds_date = March / 20 / 2023; or this way using namespace std::literals; chr::year_month_day const cds_date = 2023y / 03 / 20d; It is possible to easily convert an instance of class into with new C++20 operator: year_month_day system_clock::time_point sys_days chr::system_clock::time_point const tp_sys = chr::sys_days(cds_date); and to cast it, for example, to for : time_point utc_clock chr::time_point<chr::utc_clock> const tp_utc = chr::clock_cast<chr::utc_clock>(tp_sys); Obviously, we can leverage here all the machinery from the previous C++ standard, and a good example here is . duration_cast In the next chapter, we will learn how to create our own clock class compatible with all the above C++20 features for Chrono. How to create your own clock Further, we won't concentrate much on financial details and will create a very simple sample clock that starts at the beginning of the millennium, i.e. on . But the techniques we will use are completely relevant for the financial problems when dealing with dates -- we just want to keep things as simple as possible for demonstration purposes: January 1, 2000 #include <cstdint> #include <chrono> class millennium_clock final { public: using rep = std::int32_t; using period = std::ratio<1, 1>; using duration = std::chrono::duration<rep, period>; using time_point = std::chrono::time_point<millennium_clock>; static constexpr bool is_steady = false; static time_point now() { return time_point{ std::chrono::duration_cast<duration>(std::chrono::system_clock::now() - epoch) }; } private: static std::chrono::sys_time<duration> const epoch; }; std::chrono::sys_time<millennium_clock::duration> const millennium_clock::epoch { std::chrono::sys_days(std::chrono::January / 1 / 2000) }; static_assert(std::chrono::is_clock_v<millennium_clock>); Here we mimic the methods we have for class and then checking on compile time that the class we created completely satisfies all requirements for clocks. Please also note that the must be and that the basic units for it are . system_clock chrono period std::ratio seconds Potentially, we would like to be able to use casts in our code from our clock to, let's say, : system_clock auto const& now_mil = millennium_clock::now(); auto const& now_sys = chr::clock_cast<chr::system_clock>(now_mil); To do that, we need to make a specialization for class: clock_time_conversion namespace std::chrono { // specialization for std::chrono::clock_cast template <> struct clock_time_conversion<system_clock, millennium_clock> { template <typename Duration> sys_time<Duration> operator()(time_point<millennium_clock, Duration> const& tp) const { return sys_time<Duration>(tp.time_since_epoch() + millennium_clock::epoch); } }; } The last step is to give the access for class to private members of class: clock_time_conversion millennium_clock class millennium_clock final { friend struct std::chrono::clock_time_conversion<std::chrono::system_clock, millennium_clock>; That's it, now we can cast a using ! time_point clock_cast Conclusion The article demonstrates some good practices for modern C++ use when working with with emphasis on the potential applications for financial libraries development. It clearly demonstrates that it is possible to write elegant and efficient C++ code and how to leverage these practices with the latest C++ standard. <chrono> Lead image generated with stable diffusion.