CppCon 2016: Howard Hinnant “A <chrono> Tutorial"

CppCon · 65K views · 2016 · 1:00:32

Open on YouTube

C, C++, Rust, Go & systems languagesSoftware architecture & designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering or asking about std::chrono durations, clocks (steady vs system vs high_resolution) and time points, since the author explains the design and pitfalls directly.

Recommended by 6 people across 28 different months, 2013 to 2024.

What it is

A one-hour CppCon 2016 talk by Howard Hinnant, lead author of C++11 chrono, that builds up the <chrono> library from a single second to durations, custom units, conversions and time points. It stresses chrono's design goal of failing at compile time and common beginner mistakes.

What people said

Strong rec Qaz · 2019-05 · Stack Overflow · on “C++: setting time using suffixes”

Yes, as of C++14, you can use the user-defined literals described here [LINK] to create durations: #include <chrono> using namespace std::literals; auto time1 = 1s; // std::chrono::seconds{1} auto time2 = 2min; // std::chrono::minutes{2} auto time3 = 7ms; // std::chrono::milliseconds{7} These create type-safe objects that store an integral value. You can use double internally fairly easily, but those specializations don't come with a pretty type alias out of the box: namespace chr = std::chrono; using dbl_seconds = chr::duration<double, chr::seconds::period>; // Likewise for other units dbl_seconds time1 = 1s; If you absolutely need the internal value (usually a bad idea), you can access it

Strong rec Qaz · 2020-04 · Stack Overflow · on “C++ Converting nanoseconds to milliseconds”

I'd highly recommend watching Howard's talk [this video] on the matter. He no doubt does a much better job than I do at explaining how the library helps the user to prevent mistakes. The talk starts with durations, the most straightforward part of the API. (And yes, I'm aware I keep saying "you know" in relation to the basic guarantees the library gives you. If you're using an API, you should _at least_ know that. Not knowing that is like not knowing that integer division truncates when you go to use it.)

Recommends Howard Hinnant · 2017-04 · Stack Overflow · on “std::chrono::system_clock vs std::chrono::high_resolution_clock behavior”

The reason it asserts for you is because high_resolution_clock is allowed to (and often does) have a different epoch than system_clock . It is the de facto standard (not specified but portable) that system_clock 's epoch is measuring time since 1970-01-01 00:00:00 UTC, neglecting leap seconds 1 . There is no de facto standard for high_resolution_clock . On gcc high_resolution_clock is a typedef for system_clock , and so on gcc platforms, you'll notice perfect synchronization. On VS and libc++ high_resolution_clock is a typedef for steady_clock . For me, the epoch of steady_clock is whenever the computer booted up. Here is a video tutorial for . [this video] It covers lots of issues, including thi

Recommends Howard Hinnant · 2017-04 · Stack Overflow · on “std::chrono::high_resolution_clock - time measurement”

No. It's just that different clocks can have different precisions, and timing something so short might not take more than 1 tick of a clock. You can find out a clock's precision by inspecting clock::duration::period::num and clock::duration::period::den . Additionally, some implementations of <chrono> have had "quality of implementation" issues over the years. So your experience might change as you switch compilers, and even versions of the same compiler. Your experience will generally be better if you can work with the latest version of your compiler. Here is a video tutorial for . [this video]

Recommends Howard Hinnant · 2017-06 · Stack Overflow · on “C++ parse date/time with microseconds”

Using Howard Hinnant's free, open-source, C++11, header-only datetime library [LINK] , you can easily parse any precision you want. You just can't put it into a tm since that is limited to seconds precision. For example: #include "date.h" #include <cassert> #include <sstream> int main() { std::chrono::system_clock::time_point tp; std::istringstream ss{"2010-12-30T01:20:30.123456Z"}; ss >> date::parse("%FT%TZ", tp); assert(!ss.fail()); using namespace date; using namespace std::chrono_literals; assert(tp == sys_days(2010_y/dec/30) + 1h + 20min + 30s + 123456us); } This uses C++14 chrono literals. If you're in C++11, the last two lines would look like: using namespace std::chrono; assert(tp ==

Recommends Carl · 2017-10 · Stack Overflow · on “C++ How to make timer accurate in Linux”

High Resolution Clock is just a typedef of the steady clock. Check out. This video [this video] for more information on the reason you should use steady_clock over high resolution as well as more good information on chrono.

Recommends Howard Hinnant · 2017-11 · Stack Overflow · on “ratio math not working with std::chrono::duration”

You have identified the issue correctly. You can use return time_point_cast<steady_clock::duration>(ref + NTSC_FPS_t(frames)); which will truncate towards zero to the next steady_clock::duration (nanosecond). In C++17, you will have other rounding modes: floor ceil round You're welcome to use them from here if you want them prior to C++17: [LINK] If it helps, here is a video tutorial for <chrono> : [this video] You could also use "date.h" [LINK] to explore the units that do result from steady_clock::time_point + NTSC_FPS_t like this: #include "date/date.h" #include <iostream> typedef std::chrono::duration<int, std::ratio<1001,30000>> NTSC_FPS_t; int main() { using date::operator<<; auto tp = std:

Recommends JohnFilleau · 2020-06 · Stack Overflow · on “How do I parse with respect to char array indices (C++)”

Unrelated to your specific question, but maybe related to a related underlying issue, watch this youtube video about zoned time, a whole library for parsing and using time. [LINK] builds upon the chrono talk [this video] watch the latter if you don't have an understanding of `chrono`.

Recommends Adrià Arrufat · 2020-09 · Stack Overflow · on “Difference between std::system_clock and std::steady_clock?”

Relevant talk about chrono by Howard Hinnant [this video] , author of chrono : don't use high_resolution_clock , as it's an alias for one of these: system_clock : it's like a regular clock, use it for time/date related stuff steady_clock : it's like a stopwatch, use it for timing things.

Recommends Wiley Rush · 2024-08 · Stack Overflow · on “What is a Rep in a time_point duration?”

The other answers are very good, but I wanted to write an answer in language that would have helped me the most. Quick but not quite accurate answer: in c# terms, the type of Ret would be Type . Duration , the type introduced by chrono , was written for extreme flexibility, allowing the coder to store its data as int, long, float, double, or any type that supports the arithmetic operations [LINK] that duration uses. You assign your chosen type to Rep and it compiles your duration to store that type. You can even specify that it uses a char . The Period in a Duration is a std::ratio (two integers) which represent whatever amount of seconds that Rep is counting, ie std::ratio<1,1000> for 1/100