{"id":9508,"date":"2024-05-06T09:22:00","date_gmt":"2024-05-06T09:22:00","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=9508"},"modified":"2024-05-06T09:22:00","modified_gmt":"2024-05-06T09:22:00","slug":"time-zones-details","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/time-zones-details\/","title":{"rendered":"Time Zones: Details"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Today, I present the time-zones functionality of the C++20 extension.<\/p>\n\n\n\n<!--more-->\n\n\n\n<figure class=\"wp-block-image 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 seventh 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\/\">C++20: Creating Calendar Dates<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-displaying-and-checking-calendar-dates\/\">C++20: 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<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Times Zones<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First, a time zone is a region and its entire date history, such as daylight saving time or leap seconds. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Challenges<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Dealing with time zones has a few inherent challenges.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Wintertime and summertime: Many European countries, such as Germany, use summertime (daylight saving time) and wintertime. The daylight saving<br>time is one hour ahead of the wintertime in Germany.<\/li>\n\n\n\n<li>More time zones: Countries like China or the United States have different time<br>zones. For example, in the United States, between the Hawaii Standard Time<br>(UTC-10) and the Easter Daylight Time (UTC-4) is a difference of six hours.<\/li>\n\n\n\n<li>Time zone differences: Time zone differences are often fractions of hours, such<br>as 30 or 45 minutes. The Australian Central Time is UTC+9:30, and the Australian<br>Central Western Standard Time is UTC+8:45.<\/li>\n\n\n\n<li>Time zone abbreviations are ambiguous: The time zone abbreviations are not<br>unique. ADT can be Arabic Daylight Time (UTC+4) or Atlantic Daylight Time (UTC-3).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">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\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"772\" height=\"668\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/db.png\" alt=\"\" class=\"wp-image-9517\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/db.png 772w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/db-300x260.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/db-768x665.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/db-705x610.png 705w\" sizes=\"auto, (max-width: 772px) 100vw, 772px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-left wp-block-paragraph\">The use of the time zone database requires an operating system. Consequently, using the time zone database on a freestanding system typically results in an exception. The time-zone database is updated during the operating system\u2019s update, such as a reboot. When your system supports updating the IANA time-zone database without rebooting, you can use<code> std::chrono::reload_tzdb()<\/code>. The new database is atomically added to the front of the linked list. Calls such as <code>std::chrono::get_tzdb_list() <\/code>or<code> std::chrono::get_tzdb() <\/code>parse the front of the list. Consequently, the database queries get the updated database entries.<code> std::chrono::get_tzdb().version<\/code> returns the version of the used database.<\/p>\n\n\n\n<p class=\"has-text-align-left wp-block-paragraph\"><br>The two elementary types for time zones are <code>std::chrono::time_zone<\/code> and<code> std::chrono::zoned_time<\/code>.<\/p>\n\n\n\n<p class=\"has-text-align-left wp-block-paragraph\"><br>The possible time zones are predefined by the IANA time-zone database. The calls<code> std::chrono::current_zone()<\/code>, and<code> std::chrono::locate_zone(name)<\/code> return a pointer to the current or by name requested time zone. The call<code> std::chrono::locate_zone(name)<\/code> causes a search for the name in the database. If the search is unsuccessful, you get a <code>std::runtime_error <\/code>exception.<\/p>\n\n\n\n<p class=\"has-text-align-left wp-block-paragraph\"><br><code>std::chrono::zoned_time() <\/code>represents a time zone combined with a time point. You can use a system time point, or a local time point as time point. A system time point uses<code> std::chrono::system_clock<\/code> and a local time point uses the pseudo clock<code> std::chrono::local_t<\/code>.<br><\/p>\n\n\n\n<p class=\"has-text-align-left wp-block-paragraph\">My first example is straightforward. It displays the UTC time and the local time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">UTC Time and Local Time<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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\n\n\n<p class=\"wp-block-paragraph\"><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\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\">\/\/ localTime.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> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>floor;\n \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;UTC  time&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;                             <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)             <\/span>\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();\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;  &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> utcTime <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;  &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/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\">&#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;Local time&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;                           <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)                <\/span>\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> localTime <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(), utcTime);\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;  &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> localTime <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;  &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/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()) \n                      <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> offset <span style=\"color: #555555\">=<\/span> localTime.get_info().offset;                 <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)    <\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;  UTC offset: &quot;<\/span>  <span style=\"color: #555555\">&lt;&lt;<\/span> offset <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 class=\"wp-block-paragraph\">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> std::chrono::zoned_time<\/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\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"639\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/localTime-1030x639.png\" alt=\"\" class=\"wp-image-9523\" style=\"width:400px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/localTime-1030x639.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/localTime-300x186.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/localTime-768x476.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/localTime-705x437.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/05\/localTime.png 1066w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">My next example answers a crucial question when I teach in a different time zone: When should I start my online class?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I present the time-zones functionality of the C++20 extension.<\/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-9508","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\/9508","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=9508"}],"version-history":[{"count":17,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9508\/revisions"}],"predecessor-version":[{"id":9529,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9508\/revisions\/9529"}],"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=9508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=9508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=9508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}