{"id":6020,"date":"2020-10-23T15:39:58","date_gmt":"2020-10-23T15:39:58","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/calendar-and-time-zone-in-c-20-time-zones\/"},"modified":"2023-06-26T09:37:03","modified_gmt":"2023-06-26T09:37:03","slug":"calendar-and-time-zone-in-c-20-time-zones","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/calendar-and-time-zone-in-c-20-time-zones\/","title":{"rendered":"Calendar and Time-Zones in C++20: Time-Zones"},"content":{"rendered":"<p>This post concludes my introduction to the chrono extension in C++20. Today I present the time-zones functionality.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5945\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/TimelineCpp20CoreLanguage.png\" alt=\"TimelineCpp20CoreLanguage\" width=\"650\" height=\"278\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/TimelineCpp20CoreLanguage.png 950w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/TimelineCpp20CoreLanguage-300x128.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/TimelineCpp20CoreLanguage-768x327.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>I should have written today I mainly present the time-zones functionality in C++20. Before I write about the time zones in C++20, I want to present the online resource <a href=\"https:\/\/github.com\/HowardHinnant\/date\/wiki\/Examples-and-Recipes\">Examples and Recipes <\/a>from Howard Hinnant, which has about 40 examples of the new chrono functionality. Presumably, the chrono extension in C++20 is not easy to get; therefore it&#8217;s pretty essential to have so many examples. You should use these examples as a starting point for further experiments and sharpen your understanding. You can also add your recipes.<\/p>\n<p>To get an idea of&nbsp;<a href=\"https:\/\/github.com\/HowardHinnant\/date\/wiki\/Examples-and-Recipes\">Examples and Recipes<\/a>, I want to present a program for<a href=\"https:\/\/github.com\/rbock\"> Roland Bock<\/a> that calculates ordinal dates.<\/p>\n<h2>Calculating Ordinal Dates<\/h2>\n<p>&#8220;<em>An <a href=\"https:\/\/en.wikipedia.org\/wiki\/Ordinal_date\" rel=\"nofollow\">ordinal date<\/a> consists of a year and a day of year (1st of January being day 1, 31st of December being day 365 or day 366). The year can be obtained directly from year_month_day. And calculating the day is wonderfully easy. In the code below we make us of the fact that year_month_day can deal with invalid dates like the 0th of January<\/em>:&#8221; (Roland Bock)<\/p>\n<p>I added the necessary headers to Roland&#8217;s program.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ ordinalDate.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include \"date.h\"<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iomanip&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>()\r\n{\r\n   <span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> date;\r\n   \r\n   <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> time <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();\r\n   <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> daypoint <span style=\"color: #555555;\">=<\/span> floor<span style=\"color: #555555;\">&lt;<\/span>days<span style=\"color: #555555;\">&gt;<\/span>(time);                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span> \r\n   <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> ymd <span style=\"color: #555555;\">=<\/span> year_month_day{daypoint};         \r\n   \r\n   <span style=\"color: #0099ff; font-style: italic;\">\/\/ calculating the year and the day of the year<\/span>\r\n   <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> year <span style=\"color: #555555;\">=<\/span> ymd.year();\r\n   <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> year_day <span style=\"color: #555555;\">=<\/span> daypoint <span style=\"color: #555555;\">-<\/span> sys_days{year<span style=\"color: #555555;\">\/<\/span>January<span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">0<\/span>}; <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span> \r\n                                                              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n   std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> year <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'-'<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>setfill(<span style=\"color: #cc3300;\">'0'<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>setw(<span style=\"color: #ff6600;\">3<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> year_day.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n   \r\n   <span style=\"color: #0099ff; font-style: italic;\">\/\/ inverse calculation and check<\/span>\r\n   assert(ymd <span style=\"color: #555555;\">==<\/span> year_month_day{sys_days{year<span style=\"color: #555555;\">\/<\/span>January<span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">0<\/span>} <span style=\"color: #555555;\">+<\/span> year_day});\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I want to add a few remarks to the program. Line (1) truncates the current time point. The value is used in the following line to initialize a calendar date. Line (2) calculates the time duration between the two time points. Both time points have the resolution day. Finally, <code>year_day.count()<\/code> inline (3) returns the time duration in days.<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6016\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/ordinalDate2.png\" alt=\"ordinalDate2\" width=\"350\" height=\"181\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/ordinalDate2.png 450w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/ordinalDate2-300x155.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>My following examples of time zones are also inspired by the already mentioned web resource <a href=\"https:\/\/github.com\/HowardHinnant\/date\/wiki\/Examples-and-Recipes\">Examples and Recipes<\/a>.<\/p>\n<\/p>\n<h2>Times-Zones<\/h2>\n<p>First, a time zone is a region and its entire history of the date, such as daylight saving time or leap seconds. The time-zone library in C++20 is a complete parser of the<a href=\"https:\/\/www.iana.org\/time-zones\"> IANA timezone database<\/a>. The following table should give you a first idea of the new functionality.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6017\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/timeZone.png\" alt=\"timeZone\" width=\"600\" height=\"277\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/timeZone.png 1031w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/timeZone-300x139.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/timeZone-1024x473.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/timeZone-768x355.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>I use in my examples the function <code>std::chrono::zones_time<\/code> , which is essentially a time zone combined with a time point.<\/p>\n<p>Before I show you two examples, I want to make a short remark. To compile a program using the time-zone library, you have to compile the<code> tz.cpp<\/code> file and link it against the <code>curl<\/code> library. The curl library is necessary to get the current <a href=\"https:\/\/www.iana.org\/time-zones\"> IANA timezone database<\/a>. The following command line for G++ should give you the idea:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">g<span style=\"color: #555555;\">++<\/span> localTime.cpp <span style=\"color: #555555;\">-<\/span>I <span style=\"color: #555555;\">&lt;<\/span>Path to data<span style=\"color: #555555;\">\/<\/span>tz.h<span style=\"color: #555555;\">&gt;<\/span> tz.cpp <span style=\"color: #555555;\">-<\/span>std<span style=\"color: #555555;\">=<\/span>c<span style=\"color: #555555;\">++<\/span><span style=\"color: #ff6600;\">17<\/span> <span style=\"color: #555555;\">-<\/span>lcurl <span style=\"color: #555555;\">-<\/span>o localTime\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>My first program is straightforward. It displays the UTC and the local time.<\/p>\n<h3>UTC Time and Local Time<\/h3>\n<p>The<a href=\"https:\/\/en.wikipedia.org\/wiki\/Coordinated_Universal_Time\"> UTC or Coordinated Univeral Time<\/a> is the primary time standard worldwide. A computer uses <a href=\"https:\/\/en.wikipedia.org\/wiki\/Unix_time\">Unix time<\/a> which is a very close approximation of UTC. The UNIX time is the number of seconds since the Unix epoch. The Unix epoch is 00:00:00 UTC on 1 January 1970.<\/p>\n<p><code><\/code><code>std::chrono::system_clock::now()<\/code> inline (1) returns in the following program<code> localTime.cpp <\/code> the Unix time.<code><br \/><\/code><\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ localTime.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include \"date\/tz.h\"<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> date;\r\n \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"UTC  time\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> utcTime <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();\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> utcTime <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> date<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>(utcTime) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span><span style=\"color: #555555;\">:<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Local time\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> localTime <span style=\"color: #555555;\">=<\/span> date<span style=\"color: #555555;\">::<\/span>make_zoned(date<span style=\"color: #555555;\">::<\/span>current_zone(), utcTime);\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> localTime <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> date<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.get_local_time()) \r\n            <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> offset <span style=\"color: #555555;\">=<\/span> localTime.get_info().offset;         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"  UTC offset: \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> offset <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I have not added too much to the program. The code block beginning with line (1) gets the current time point, truncates it to seconds, and displays it. The call<code> date::make_zoned<\/code> creates&nbsp;&nbsp;<code>std::chrono::zoned_time localTime. T<\/code>he following call<code> localTime.get_local_time() <\/code>returns the stored time point as a local time. This time point is also truncated to seconds. <code>localTime<\/code> (line 3) can also be used to get information about the time zone. In this case, I&#8217;m interested in the offset to the UTC.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6018\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/localTime.png\" alt=\"localTime\" width=\"400\" height=\"297\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/localTime.png 472w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/localTime-300x222.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>My last program answers a crucial question when I teach in a different time zone: When should I start my online class?<\/p>\n<h3>Various Time Zones for Online Classes<\/h3>\n<p>The program <code>onlineClass.cpp<\/code> answers the following question: How late is it in given time zones, when I start an online class at the <code>7h, 13h,<\/code> or <code>17h<\/code> local time (Germany)?<\/p>\n<p>The online class should start on the 1st of February 2021, taking 4 hours. Because daylight saves time, the calendar date is essential to get the correct answer.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ onlineClass.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include \"date\/tz.h\"<\/span>\r\n<span style=\"color: #009999;\">#include &lt;algorithm&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iomanip&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> ZonedTime<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> getMinutes(<span style=\"color: #006699; font-weight: bold;\">const<\/span> ZonedTime<span style=\"color: #555555;\">&amp;<\/span> zonedTime) {                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> date<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>(zonedTime.get_local_time());\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> printStartEndTimes(<span style=\"color: #006699; font-weight: bold;\">const<\/span> date<span style=\"color: #555555;\">::<\/span>local_days<span style=\"color: #555555;\">&amp;<\/span> localDay,    <span style=\"color: #0099ff; font-style: italic;\">        \/\/ (2)<\/span>\r\n                        <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>hours<span style=\"color: #555555;\">&amp;<\/span> h, \r\n                        <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>hours<span style=\"color: #555555;\">&amp;<\/span> durationClass,\r\n                        <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>initializer_list<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;&amp;<\/span> timeZones ){\r\n    \r\n    date<span style=\"color: #555555;\">::<\/span>zoned_time startDate{date<span style=\"color: #555555;\">::<\/span>current_zone(), localDay <span style=\"color: #555555;\">+<\/span> h}; <span style=\"color: #0099ff; font-style: italic;\"> \/\/ (4)<\/span>\r\n    date<span style=\"color: #555555;\">::<\/span>zoned_time endDate{date<span style=\"color: #555555;\">::<\/span>current_zone(), localDay <span style=\"color: #555555;\">+<\/span> h <span style=\"color: #555555;\">+<\/span> durationClass};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Local time: [\"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> getMinutes(startDate) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\", \"<\/span> \r\n                                  <span style=\"color: #555555;\">&lt;&lt;<\/span> getMinutes(endDate) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"]\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;<br \/><span style=\"color: #0099ff; font-style: italic;\">                                                                    \/\/ (5)<\/span><br \/>   <span style=\"color: #0099ff; font-style: italic;\"><\/span>longestStringSize <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>max(timeZones, [](<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> a, <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> b) { <span style=\"color: #006699; font-weight: bold;\">return<\/span> a.size() <span style=\"color: #555555;\">&lt;<\/span> b.size(); }).size();\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> timeZone<span style=\"color: #555555;\">:<\/span> timeZones) {                                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span>\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>setw(longestStringSize <span style=\"color: #555555;\">+<\/span> <span style=\"color: #ff6600;\">1<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>left \r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> timeZone \r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"[\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> getMinutes(date<span style=\"color: #555555;\">::<\/span>zoned_time(timeZone, startDate))\r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\", \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> getMinutes(date<span style=\"color: #555555;\">::<\/span>zoned_time(timeZone, endDate)) \r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"]\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    }\r\n }\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\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>string_literals;\r\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;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> classDay{date<span style=\"color: #555555;\">::<\/span>year(<span style=\"color: #ff6600;\">2021<\/span>)<span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">2<\/span><span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">1<\/span>};\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> durationClass <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">4<\/span>h;\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> timeZones <span style=\"color: #555555;\">=<\/span> {<span style=\"color: #cc3300;\">\"America\/Los_Angeles\"<\/span>s, <span style=\"color: #cc3300;\">\"America\/Denver\"<\/span>s, <span style=\"color: #cc3300;\">\"America\/New_York\"<\/span>s, \r\n                      <span style=\"color: #cc3300;\">\"Europe\/London\"<\/span>s, <span style=\"color: #cc3300;\">\"Europe\/Minsk\"<\/span>s, <span style=\"color: #cc3300;\">\"Europe\/Moscow\"<\/span>s, \r\n                      <span style=\"color: #cc3300;\">\"Asia\/Kolkata\"<\/span>s, <span style=\"color: #cc3300;\">\"Asia\/Novosibirsk\"<\/span>s, <span style=\"color: #cc3300;\">\"Asia\/Singapore\"<\/span>s,\r\n                      <span style=\"color: #cc3300;\">\"Australia\/Perth\"<\/span>s, <span style=\"color: #cc3300;\">\"Australia\/Sydney\"<\/span>s};\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> startTime<span style=\"color: #555555;\">:<\/span> {<span style=\"color: #ff6600;\">7<\/span>h, <span style=\"color: #ff6600;\">13<\/span>h, <span style=\"color: #ff6600;\">17<\/span>h}) {    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n        printStartEndTimes(date<span style=\"color: #555555;\">::<\/span>local_days{classDay}, startTime, \r\n                           durationClass, timeZones);\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Before I dive into the functions<code> getMinutes<\/code> (line 1) and <code>printStartEndTimes<\/code> (line 2), let me say a few words about the <code>main<\/code> function. The <code>main<\/code> function defines the day of the class, the duration of the class, and all time zones. Finally, the range-based for-loop (line 3) iterates through all potential starting points for an online class. All necessary information is displayed thanks to the function<br \/>(line 2).<\/p>\n<p>The few lines beginning with line (4) calculate the<code> startDate<\/code> and <code>endDate<\/code> of my training by adding the start and class duration to the calendar date. Both values are displayed with the help of the function <code>getMinutes<\/code> (line 1). <code>date<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>(zonedTime.get_local_time())<\/code> gets the stored time point out of the<code> std::chrono::zoned_time<\/code> and truncates the value to the minute resolution. To properly align the output of the program, line (5) <code> <\/code><code><\/code>determines the size of the longest of all time-zone names. Line (6) iterates through all time zones and displays the name of the time-zone and the beginning and end of each online class. A few calendar dates even cross the day boundaries.<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6019\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/onlineClass.png\" alt=\"onlineClass\" width=\"600\" height=\"760\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/onlineClass.png 794w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/onlineClass-237x300.png 237w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/onlineClass-768x972.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>There is more to write about in the extended Chrono library. For example, C++20 offers new clocks such as<code> std::chrono::utc_clock<\/code> that include leap seconds, or the<code> std::chrono::tai_clock<\/code> that represents the International Atomic Time (TAI). Additionally, thanks to the new formatting library in C++20, time durations can be nicely formatted. This feature is not available so far. If you want to study the formatting rules for time durations, here are they: <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/chrono\/system_clock\/formatter#Format_specification\">std::formatter<\/a>.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>You may have a lot of fun if you want to compare signed and unsigned integrals. This fun ends with C++20.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post concludes my introduction to the chrono extension in C++20. Today I present the time-zones functionality.<\/p>\n","protected":false},"author":21,"featured_media":5945,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[453],"class_list":["post-6020","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\/6020","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=6020"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6020\/revisions"}],"predecessor-version":[{"id":9465,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6020\/revisions\/9465"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5945"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}