{"id":9531,"date":"2024-05-13T09:39:11","date_gmt":"2024-05-13T09:39:11","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=9531"},"modified":"2024-05-15T09:13:35","modified_gmt":"2024-05-15T09:13:35","slug":"time-zones-online-classes","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/time-zones-online-classes\/","title":{"rendered":"Time Zones: Online Classes"},"content":{"rendered":"\n<p>Today, I will continue to present the functionality of the time zones of the C++20 chrono extension.<\/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>This post is the eight 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<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Various Time Zones for Online Classes<\/h2>\n\n\n\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\n\n\n<p>The online class should start on February 1st, 2021, and last 4 hours. Because of daylight savings time, the calendar date is essential to get the correct answer.<\/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: #009999\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;algorithm&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iomanip&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/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<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>\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>\n    <span style=\"color: #006699; font-weight: bold\">return<\/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>(zonedTime.get_local_time());\n}\n\n<span style=\"color: #007788; font-weight: bold\">void<\/span> printStartEndTimes(<span style=\"color: #006699; font-weight: bold\">const<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>local_days<span style=\"color: #555555\">&amp;<\/span> localDay,           <span style=\"color: #0099FF; font-style: italic\">\/\/ (2) <\/span>\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, \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,\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 ) {\n    \n    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>zoned_time startDate{std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>current_zone(), localDay <span style=\"color: #555555\">+<\/span> h}; <span style=\"color: #0099FF; font-style: italic\">\/\/ (4)  <\/span>\n    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>zoned_time endDate{std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>current_zone(), \n                                    localDay <span style=\"color: #555555\">+<\/span> h <span style=\"color: #555555\">+<\/span> durationClass};\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;Local time: [{}, {}]<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>, getMinutes(startDate), getMinutes(endDate));\n                                                                                \n    <span style=\"color: #006699; font-weight: bold\">auto<\/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: #0099FF; font-style: italic\">\/\/ (5) <\/span>\n                       <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();\n\n    std<span style=\"color: #555555\">::<\/span>string formatTimeZones <span style=\"color: #555555\">=<\/span> <span style=\"color: #CC3300\">&quot;  {0:&lt;{3}} [{1}, {2}]<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;              <span style=\"color: #0099FF; font-style: italic\">\/\/ (7) <\/span>\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>\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>vformat(formatTimeZones, std<span style=\"color: #555555\">::<\/span>make_format_args(timeZone, \n                                                            getMinutes(std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>zoned_time(timeZone, startDate)),\n                                                            getMinutes(std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>zoned_time(timeZone, endDate)),\n                                                            longestStringSize <span style=\"color: #555555\">+<\/span> <span style=\"color: #FF6600\">1<\/span>));                             \n    }\n }\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> main() {\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>string_literals;\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    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> classDay{std<span style=\"color: #555555\">::<\/span>chrono<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>};\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> durationClass <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">4<\/span>h;\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> timeZones <span style=\"color: #555555\">=<\/span> {<span style=\"color: #CC3300\">&quot;America\/Los_Angeles&quot;<\/span>s, <span style=\"color: #CC3300\">&quot;America\/Denver&quot;<\/span>s, \n                      <span style=\"color: #CC3300\">&quot;America\/New_York&quot;<\/span>s, <span style=\"color: #CC3300\">&quot;Europe\/London&quot;<\/span>s, \n                      <span style=\"color: #CC3300\">&quot;Europe\/Minsk&quot;<\/span>s, <span style=\"color: #CC3300\">&quot;Europe\/Moscow&quot;<\/span>s, \n                      <span style=\"color: #CC3300\">&quot;Asia\/Kolkata&quot;<\/span>s, <span style=\"color: #CC3300\">&quot;Asia\/Novosibirsk&quot;<\/span>s, \n                      <span style=\"color: #CC3300\">&quot;Asia\/Singapore&quot;<\/span>s, <span style=\"color: #CC3300\">&quot;Australia\/Perth&quot;<\/span>s, \n                      <span style=\"color: #CC3300\">&quot;Australia\/Sydney&quot;<\/span>s};\n\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>\n        printStartEndTimes(std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>local_days{classDay}, startTime, \n                           durationClass, timeZones);\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    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>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\n\n\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>std::floor&lt;std::chrono::minutes&gt;(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) 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.<\/p>\n\n\n\n<p>The format string in line 7 &#8221; <code>{0:&lt;{3}} [{1}, {2}]\\n<\/code>&#8221; deserves a few words. This lvalue requires the functions <code>std::vformat<\/code> and<code> std::make_format_args<\/code>. The number in the format string stands for the position in the arguments. The <code>{3} <\/code>is a placeholder for the longest time-zone name.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"794\" height=\"1005\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/onlineClass.png\" alt=\"\" class=\"wp-image-9544\" style=\"width:600px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/onlineClass.png 794w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/onlineClass-237x300.png 237w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/onlineClass-768x972.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/onlineClass-557x705.png 557w\" sizes=\"auto, (max-width: 794px) 100vw, 794px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p>I\/O consists of the reading and writing of the chrono types. The various chrono types support the unformatted writing and the formatted one with the new formatting library. This library also has the function <code>std::chrono::parse() <\/code>that makes reading from a stream quite powerful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I will continue to present the functionality of the time zones of the C++20 chrono extension. This post is the eight in my detailed journey through the chrono extension in C++20: Various Time Zones for Online Classes The program onlineClass.cpp answers the following question: How late is it in given time zones when I [&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-9531","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\/9531","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=9531"}],"version-history":[{"count":21,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9531\/revisions"}],"predecessor-version":[{"id":9557,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9531\/revisions\/9557"}],"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=9531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=9531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=9531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}