{"id":9658,"date":"2024-06-24T09:36:10","date_gmt":"2024-06-24T09:36:10","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=9658"},"modified":"2024-06-24T09:36:10","modified_gmt":"2024-06-24T09:36:10","slug":"chrono-input","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/chrono-input\/","title":{"rendered":"Chrono: Input"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">You can also apply the format specifier for formatted input.<\/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\/03\/TimelineCpp20CoreLanguage-1.png\" alt=\"\" class=\"wp-image-9316\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-1.png 960w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-1-300x128.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-1-768x329.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-1-705x302.png 705w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This post is the eleventh 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\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/chrono-i-o\/\">Chrono: I\/O<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/chrono-i-o-unformatted-and-formatted\/\">Chrono: I\/O: Unformatted<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The chrono library supports formatted input in two ways. You can use the function <code><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/chrono\/system_clock\/from_stream)\">std::chrono::from_stream<\/a><\/code> or <code><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/chrono\/parse\">std::chrono::parse<\/a><\/code>. Both functions require an input stream and parse the input into a time point according to the format specification. All format specifier except <code>%q<\/code> for unit suffixed according to the literals for time durations can be used.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>std::chrono::from_stream<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>std::chrono::from_stream <\/code>has overloads for the various clocks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;<strong>Clocks<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>std::chrono::system_time<\/code><\/li>\n\n\n\n<li><code>std::chrono::utc_time<\/code><\/li>\n\n\n\n<li><code>std::chrono::tai_time<\/code><\/li>\n\n\n\n<li><code>std::chrono::gps_time<\/code><\/li>\n\n\n\n<li><code>std::chrono::file_time<\/code><\/li>\n\n\n\n<li><code>std::chrono::local_time<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Calendar dates<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>std::chrono::year_month_day<\/code><\/li>\n\n\n\n<li><code>std::chrono::year_month<\/code><\/li>\n\n\n\n<li><code>std::chrono::month_day<\/code><\/li>\n\n\n\n<li><code>std::chrono::weekday<\/code><\/li>\n\n\n\n<li><code>std::chrono::year<\/code><\/li>\n\n\n\n<li><code>std::chrono::month<\/code><\/li>\n\n\n\n<li><code>std::chrono::day<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The various overloads require in the elementary form an input stream <code>is<\/code>, a format string <code>fmt<\/code>, and a time point or a calendar object <code>chro<\/code>: <code>std::chrono::from_stream(is, fmt, chro)<\/code>. The chrono object from the input stream is then parsed according to the format string.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also provide an abbreviation <code>abb<\/code> for a time zone and an offset <code>off<\/code> to the UTC time: <code>std::chrono::from_stream(is, fmt, chro, abb, off).<\/code> The offset has the type <code>std::chrono::minutes.<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The program<code> inputChrono.cpp <\/code>uses formatted input to read a time point and a calendar date from an input stream.<\/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\">\/\/ inputChrono.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;sstream&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    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>sys_seconds timePoint;\n    std<span style=\"color: #555555\">::<\/span>istringstream iStream1{<span style=\"color: #CC3300\">&quot;2021-08-11 21:49:35&quot;<\/span>};              <span style=\"color: #0099FF; font-style: italic\">\/\/(1)<\/span>\n    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>from_stream(iStream1, <span style=\"color: #CC3300\">&quot;%F %T&quot;<\/span>, timePoint);          <span style=\"color: #0099FF; font-style: italic\">\/\/(2)<\/span>\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> (iStream1) std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;timePoint: &quot;<\/span>  <span style=\"color: #555555\">&lt;&lt;<\/span> timePoint <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">else<\/span> std<span style=\"color: #555555\">::<\/span>cerr <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;timepoint: Reading failed<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_day date1;\n    std<span style=\"color: #555555\">::<\/span>istringstream iStream2{<span style=\"color: #CC3300\">&quot;11\/08\/21&quot;<\/span>};                         <span style=\"color: #0099FF; font-style: italic\">\/\/(3)<\/span>\n    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>from_stream(iStream2, <span style=\"color: #CC3300\">&quot;%x&quot;<\/span>, date1);                 <span style=\"color: #0099FF; font-style: italic\">\/\/(4)<\/span>\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> (iStream2) std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;date1: &quot;<\/span>  <span style=\"color: #555555\">&lt;&lt;<\/span> date1 <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">else<\/span> std<span style=\"color: #555555\">::<\/span>cerr <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;date1: Reading failed<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_day date2;\n    std<span style=\"color: #555555\">::<\/span>istringstream iStream3{<span style=\"color: #CC3300\">&quot;11\/15\/21&quot;<\/span>};\n    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>from_stream(iStream3, <span style=\"color: #CC3300\">&quot;%x&quot;<\/span>, date2);                  <span style=\"color: #0099FF; font-style: italic\">\/\/(5)<\/span>\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> (iStream3) std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;date2: &quot;<\/span>  <span style=\"color: #555555\">&lt;&lt;<\/span> date2 <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">else<\/span> std<span style=\"color: #555555\">::<\/span>cerr <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;date2: Reading failed<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/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 class=\"wp-block-paragraph\">On lines 1 and 2, the data on the input stream (<code>iStream1<\/code>) matches the format string (&#8220;<code>%F %T\"<\/code>). The same holds for input stream <code>iStream2<\/code> (line 3) and the corresponding format string &#8220;<code>%x\"<\/code> (line 4). On the contrary, there is no 15th month, and the parse step in line 5 fails. Consequentially, the failbit of the <code>iStream3<\/code> is set. Using the <code>iStream3<\/code> in a boolean expression evaluates to false.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"424\" height=\"203\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/06\/inputChrono.png\" alt=\"\" class=\"wp-image-9666\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/06\/inputChrono.png 424w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/06\/inputChrono-300x144.png 300w\" sizes=\"auto, (max-width: 424px) 100vw, 424px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><code>std::chrono::parse<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Accordingly to <code>std::chrono::from_stream<\/code>, you can use the function <code>std::chrono::parse <\/code>for parsing input. The following code snippet shows their equivalence.<\/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%\">std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>from_stream(is, fmt, chro)\nis <span style=\"color: #555555\">&gt;&gt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>parse(fmt, chro)\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of <code>std::chrono::from_stream<\/code>, <code>std::chrono::parse<\/code> is directly invoked on the input stream <code>is<\/code>. <code>std::chrono::parse<\/code> also needs a format string <code>fmt<\/code> and a chrono object <code>chro<\/code>. &nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consequently, I can directly rewrite the previous program <code>inputChrono.cpp<\/code> using <code>std::chrono::from_stream <\/code>into the program <code>inputChronoParse.cpp <\/code>using <code>std::chrono::parse.<\/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\">\/\/ inputChronoParse.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;sstream&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    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>sys_seconds timePoint;\n    std<span style=\"color: #555555\">::<\/span>istringstream iStream1{<span style=\"color: #CC3300\">&quot;2021-08-11 21:49:35&quot;<\/span>};\n    iStream1 <span style=\"color: #555555\">&gt;&gt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>parse(<span style=\"color: #CC3300\">&quot;%F %T&quot;<\/span>, timePoint);\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> (iStream1) std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;timePoint: &quot;<\/span>  <span style=\"color: #555555\">&lt;&lt;<\/span> timePoint <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">else<\/span> std<span style=\"color: #555555\">::<\/span>cerr <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;timepoint: Reading failed<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_day date1;\n    std<span style=\"color: #555555\">::<\/span>istringstream iStream2{<span style=\"color: #CC3300\">&quot;11\/08\/21&quot;<\/span>};\n    iStream2 <span style=\"color: #555555\">&gt;&gt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>parse(<span style=\"color: #CC3300\">&quot;%x&quot;<\/span>, date1);\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> (iStream2) std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;date1: &quot;<\/span>  <span style=\"color: #555555\">&lt;&lt;<\/span> date1 <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">else<\/span> std<span style=\"color: #555555\">::<\/span>cerr <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;date1: Reading failed<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_day date2;\n    std<span style=\"color: #555555\">::<\/span>istringstream iStream3{<span style=\"color: #CC3300\">&quot;11\/15\/21&quot;<\/span>};\n    iStream3 <span style=\"color: #555555\">&gt;&gt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>parse(<span style=\"color: #CC3300\">&quot;%x&quot;<\/span>, date2);\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> (iStream3) std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;date2: &quot;<\/span>  <span style=\"color: #555555\">&lt;&lt;<\/span> date2 <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">else<\/span> std<span style=\"color: #555555\">::<\/span>cerr <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;date2: Reading failed<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/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<h2 class=\"wp-block-heading\"> What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;m done with my deep dive into the chrono library. In my next post, I will discuss concurrency.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can also apply the format specifier for formatted input. This post is the eleventh in my detailed journey through the chrono extension in C++20. The chrono library supports formatted input in two ways. You can use the function std::chrono::from_stream or std::chrono::parse. Both functions require an input stream and parse the input into a time [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":9316,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[453],"class_list":["post-9658","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\/9658","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=9658"}],"version-history":[{"count":17,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9658\/revisions"}],"predecessor-version":[{"id":9677,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9658\/revisions\/9677"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/9316"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=9658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=9658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=9658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}