4 Time points, duration and literals

 

This chapter covers

  • Using std::chrono’s time points and durations
  • Using ratios
  • Using literal suffixes
  • Using the overloaded operator / to create dates
  • Input and output of time points and durations
  • Using different time zones

In this chapter we will create a short program to give a countdown to an event. To achieve this, we will use time points and durations from the chrono header. This was introduced in C++11 and though the essence has remained unchanged, several useful additions have been made over time. Howard Hinnant is the main author and designer of this feature. His Meeting C++ talk in 2019 gave a lot of background to its design (https://www.youtube.com/watch?v=adSAN282YIw). As we use chrono, we will therefore learn several important idioms and approaches that apply in many other situations.

We will build a simple countdown in the first section, then dig deeper into the types we used. We will discover how to use the ratio templates so that we can understand durations. We then learn how to read dates in, so we can countdown to any event, and print out countdowns in various units. We will learn about literal suffixes to specify days, months and so on, and why they are useful. We will also encounter the idea of requirements, and so touch on concepts.

4.1 How long until the last day of the year?

 

4.2 Understanding durations in detail

 
 
 

4.2.1 Ratios

 
 

4.2.2 Durations

 
 
 
 

4.2.3 Literal suffixes and operator / for readable code

 
 
 

4.2.4 Requirements and concepts

 
 

4.2.5 How many days until the last day of the year?

 
 
 

4.2.6 Using last to find how long to pay day

 
 
 

4.2.7 Writing testable code

 
 
 
 

4.3 Input, output and formatting

 
 
 
 

4.3.1 Parsing a date

 
 

4.3.2 Formatting time points and durations

 
 

4.4 Time zones

 
 
 

4.5 Summary

 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest