{"id":9560,"date":"2024-05-20T10:09:52","date_gmt":"2024-05-20T10:09:52","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=9560"},"modified":"2024-05-20T10:09:53","modified_gmt":"2024-05-20T10:09:53","slug":"chrono-i-o","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/chrono-i-o\/","title":{"rendered":"Chrono I\/O"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"411\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/TimelineCpp20CoreLanguage.png\" alt=\"\" class=\"wp-image-9222\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/TimelineCpp20CoreLanguage.png 960w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/TimelineCpp20CoreLanguage-300x128.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/TimelineCpp20CoreLanguage-768x329.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/TimelineCpp20CoreLanguage-705x302.png 705w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>This post is the ninth in my detailed journey through the chrono extension in C++20:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-basic-chrono-terminology\/\">Basic Chrono Terminology<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-basic-chrono-terminology-with-time-duration-and-time-point\/\">Basic Chrono Terminology with Time Duration and Time Point<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-time-of-day\/\">Time of Day: Details<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-creating-calendar-dates\/\">Creating Calendar Dates<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-displaying-and-checking-calendar-dates\/\">Displaying and Checking Calendar Dates<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-query-calendar-dates-and-ordinal-dates\/\">Query Calendar Dates and Ordinal Dates<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/time-zones-details\/\">Time Zones: Details<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/time-zones-online-classes\/\">Time Zones: Online Classes<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<p>Most chrono types, such as time duration, time points, and calendar dates, support direct writing without format specification.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Unformatted <\/h3>\n\n\n\n<p>The following tables show the default output format. Let&#8217;s start with time durations.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Time Durations<\/h4>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"367\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/TimeDuration-1030x367.png\" alt=\"\" class=\"wp-image-9570\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/TimeDuration-1030x367.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/TimeDuration-300x107.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/TimeDuration-768x274.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/TimeDuration-705x251.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/TimeDuration.png 1158w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<p>The program displays values for each time duration.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ timeDurationsOutput.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> <span style=\"color: #006699; font-weight: bold\">namespace<\/span> std<span style=\"color: #555555\">::<\/span>chrono_literals;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;5ns: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #FF6600\">5<\/span>ns <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::nanoseconds(5): &quot;<\/span> \n              <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>nanoseconds(<span style=\"color: #FF6600\">5<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;5ms: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #FF6600\">5<\/span>ms <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::microseconds(5): &quot;<\/span> \n              <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>microseconds(<span style=\"color: #FF6600\">5<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;5us: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #FF6600\">5u<\/span>s <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::milliseconds(5): &quot;<\/span> \n              <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>milliseconds(<span style=\"color: #FF6600\">5<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;5s: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #FF6600\">5<\/span>s <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::seconds(5): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>seconds(<span style=\"color: #FF6600\">5<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;5min: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #FF6600\">5<\/span>min <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::minutes(5): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>minutes(<span style=\"color: #FF6600\">5<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;5h: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #FF6600\">5<\/span>h <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::hours(5): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>hours(<span style=\"color: #FF6600\">5<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::days(5): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>days(<span style=\"color: #FF6600\">5<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::weeks(5): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>weeks(<span style=\"color: #FF6600\">5<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::months(5): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>months(<span style=\"color: #FF6600\">5<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::years(5): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>years(<span style=\"color: #FF6600\">5<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;  \n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"440\" height=\"512\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/timeDurationsOutput.png\" alt=\"\" class=\"wp-image-9573\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/timeDurationsOutput.png 440w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/timeDurationsOutput-258x300.png 258w\" sizes=\"auto, (max-width: 440px) 100vw, 440px\" \/><\/figure>\n\n\n\n<p>The natural numbers in the square braces of <code>std::chrono::week<\/code>s, <code>std::chrono::months<\/code>, and <code>std::chrono<\/code>::<code>years<\/code> represent the number of seconds.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Time Points<\/h4>\n\n\n\n<p>When you use the C++20 clocks static member function <code>now<\/code>, you get the date and the time in the following format.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">year<span style=\"color: #555555\">-<\/span>month<span style=\"color: #555555\">-<\/span>day hours<span style=\"color: #555555\">:<\/span>minutes<span style=\"color: #555555\">:<\/span>seconds\n<\/pre><\/div>\n\n\n\n<p>The following program shows the current time using all C++20 clocks.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ timePointsOutput.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> nowSystemClock <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>system_clock<span style=\"color: #555555\">::<\/span>now();\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;nowSystemClock: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> nowSystemClock <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> nowSteadyClock <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>steady_clock<span style=\"color: #555555\">::<\/span>now();\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ std::cout &lt;&lt; &quot;nowSteadyClock: &quot; &lt;&lt; nowSteadyClock &lt;&lt; &#39;\\n&#39;;    ERROR<\/span>\n \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> nowFileClock <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>file_clock<span style=\"color: #555555\">::<\/span>now();\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;nowFileClock:   &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> nowFileClock <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> nowGPSClock <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>gps_clock<span style=\"color: #555555\">::<\/span>now();\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;nowGPSClock:    &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> nowGPSClock <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ auto nowlocal_tClock = std::chrono::local_t::now();           ERROR<\/span>\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> nowTAIClock <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>tai_clock<span style=\"color: #555555\">::<\/span>now();\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;nowTAIClock:    &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> nowTAIClock <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> nowUTCClock <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>utc_clock<span style=\"color: #555555\">::<\/span>now();\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;nowUTCClock:    &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> nowUTCClock <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<p>The program shows two interesting facts. First, the current time given by the <code>std::chrono::steady_clock::now()<\/code> cannot be displayed. Second, the pseudo clock <code>std::chrono::local_t<\/code> has no static member function <code>now()<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"479\" height=\"252\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/timePointsOutput.png\" alt=\"\" class=\"wp-image-9580\" style=\"width:400px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/timePointsOutput.png 479w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/timePointsOutput-300x158.png 300w\" sizes=\"auto, (max-width: 479px) 100vw, 479px\" \/><\/figure>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Thanks to the C++17 function <code>std::chrono::floor<\/code>, you can display the time point in different granularitiy. In this case, the time point has to be of type <code>std::chrono::local_time<\/code>.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ timePointsOutputGranularity.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> now <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>system_clock<span style=\"color: #555555\">::<\/span>now();\n    \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> zonedTime <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>zoned_time(std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>current_zone(), now); \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> localTime <span style=\"color: #555555\">=<\/span> zonedTime.get_local_time();\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;local_time:                                               &quot;<\/span> \n              <span style=\"color: #555555\">&lt;&lt;<\/span> localTime <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::floor&lt;std::chrono::microseconds&gt;(localTime): &quot;<\/span> \n              <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>floor<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>microseconds<span style=\"color: #555555\">&gt;<\/span>(localTime) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>; \n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::floor&lt;std::chrono::milliseconds&gt;(localTime): &quot;<\/span>\n              <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>floor<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>milliseconds<span style=\"color: #555555\">&gt;<\/span>(localTime) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::floor&lt;std::chrono::seconds&gt;(localTime):      &quot;<\/span>\n              <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>floor<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>seconds<span style=\"color: #555555\">&gt;<\/span>(localTime) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::floor&lt;std::chrono::minutes&gt;(localTime):      &quot;<\/span>\n              <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>floor<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>minutes<span style=\"color: #555555\">&gt;<\/span>(localTime) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::floor&lt;std::chrono::hours&gt;(localTime):        &quot;<\/span> \n              <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>floor<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>hours<span style=\"color: #555555\">&gt;<\/span>(localTime) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::floor&lt;std::chrono::days&gt;(localTime):         &quot;<\/span>\n              <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>floor<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>days<span style=\"color: #555555\">&gt;<\/span>(localTime) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::floor&lt;std::chrono::weeks&gt;(localTime):        &quot;<\/span>\n              <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>floor<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>weeks<span style=\"color: #555555\">&gt;<\/span>(localTime) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ std::cout &lt;&lt; std::chrono::floor&lt;std::chrono::months&gt;(localTime) &lt;&lt; &#39;\\n&#39;;    ERROR<\/span>\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ std::cout &lt;&lt; std::chrono::floor&lt;std::chrono::years&gt;(localTime) &lt;&lt; &#39;\\n&#39;;     ERROR<\/span>\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<p>The program displays <code>localTime<\/code> in different accuracies, starting with the time duration <code>std::chrono::microseconds<\/code> and ending with <code>std::chrono::weeks<\/code>. Curiously, the time durations for <code>std::chrono::months<\/code>, and<code> std::chrono::years<\/code> cannot be displayed, but this will be fixed with C++23.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"904\" height=\"359\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/timePointsOutputGranularity.png\" alt=\"\" class=\"wp-image-9585\" style=\"width:700px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/timePointsOutputGranularity.png 904w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/timePointsOutputGranularity-300x119.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/timePointsOutputGranularity-768x305.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/timePointsOutputGranularity-705x280.png 705w\" sizes=\"auto, (max-width: 904px) 100vw, 904px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p>Additionally, you can display Calendar Dates unformatted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":9222,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[453],"class_list":["post-9560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-time"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=9560"}],"version-history":[{"count":26,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9560\/revisions"}],"predecessor-version":[{"id":9590,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9560\/revisions\/9590"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/9222"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=9560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=9560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=9560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}