{"id":5153,"date":"2023-06-19T11:34:46","date_gmt":"2023-06-19T10:34:46","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=5153"},"modified":"2024-07-26T13:59:33","modified_gmt":"2024-07-26T12:59:33","slug":"how-to-convert-from-utc-to-local-time-in-c","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=5153","title":{"rendered":"C++ Guns: How to convert from UTC to local time in C++?"},"content":{"rendered":"<p>Convert from a broken down date time structure from UTC to localtime in C++.<\/p>\n<p>See <a href=\"https:\/\/stackoverflow.com\/questions\/9076494\/how-to-convert-from-utc-to-local-time-in-c\">Stackoverflow: How to convert from UTC to local time in C?<\/a><br \/>\nI converted the code from C to C++ and make it shorter.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/ replace non std function strptime with std::get_time\r\n\r\n#include &lt;iostream&gt;\r\n#include &lt;sstream&gt;\r\n#include &lt;iomanip&gt;\r\n#include &lt;ctime&gt;\r\n\r\n\/*\r\n  Convert from a broken down date time structure from UTC to localtime.\r\n  See https:\/\/stackoverflow.com\/questions\/9076494\/how-to-convert-from-utc-to-local-time-in-c\r\n*\/\r\nstd::tm UTC2localtime(std::tm tp) {\r\n  \/\/ make sure Daylight saving time flag is not accidentally switched on in UTC time\r\n  tp.tm_isdst = 0;\r\n\r\n  \/\/ get seconds since EPOCH for this time\r\n  const time_t utc = std::mktime(&amp;tp);\r\n  std::cout &lt;&lt; &quot;UTC date and time in seconds since EPOCH: &quot; &lt;&lt; utc &lt;&lt; &quot;\\n&quot;;\r\n\r\n  \/\/ convert UTC date and time (Jan. 1, 1970) to local date and time\r\n  std::tm e0{};\r\n  e0.tm_mday = 1;\r\n  e0.tm_year = 70;\r\n\r\n  \/\/ get time_t EPOCH value for e0. This handles daylight saving stuff.\r\n  \/\/ The value is e.g. -3600 for 1h difference between the timezones\r\n  const time_t diff = std::mktime(&amp;e0);\r\n\r\n  \/\/ calculate local time in seconds since EPOCH\r\n  const time_t local = utc - diff;\r\n  std::cout &lt;&lt; &quot;local date and time in seconds since EPOCH: &quot; &lt;&lt; local &lt;&lt; &quot;\\n&quot;;\r\n\r\n  \/\/ convert seconds since EPOCH for local time into local_tm time structure\r\n  std::tm local_tm;\r\n  if(localtime_r(&amp;local, &amp;local_tm) == nullptr) {\r\n    throw std::system_error(errno, std::generic_category(), &quot;UTC2localtime(): in conversion vom UTC to localtime&quot;);\r\n  }\r\n  return local_tm;\r\n}\r\n\r\nint main() {\r\n  \/\/ hard coded date and time in UTC\r\n  std::string datetime = &quot;2013 11 30 23 30 26&quot;;\r\n  std::cout &lt;&lt; &quot;UTC date and time to be converted in local time: &quot; &lt;&lt; datetime &lt;&lt; &quot;\\n&quot;;\r\n\r\n  \/\/ put values of datetime into time structure\r\n  std::tm UTC_tm{};\r\n  std::istringstream ss(datetime);\r\n  ss &gt;&gt; std::get_time(&amp;UTC_tm, &quot;%Y %m %d %H %M %S&quot;);\r\n\r\n  if(ss.fail()) {\r\n    throw std::runtime_error(&quot;Can not parse datetime from datetime &#039;&quot; + datetime + &quot;&#039; to format %Y %m %d %H %M %S&quot;);\r\n  }\r\n\r\n  const std::tm local_tm = UTC2localtime(UTC_tm);\r\n  std::cout &lt;&lt; &quot;local date and time: &quot; &lt;&lt; std::put_time(&amp;local_tm, &quot;%Y-%m-%d %H:%M:%S %Z&quot;) &lt;&lt; &quot;\\n&quot;;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Convert from a broken down date time structure from UTC to localtime in C++. See Stackoverflow: How to convert from UTC to local time in C? I converted the code from C to C++ and make it shorter. \/\/ replace non std function strptime with std::get_time #include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;iomanip&gt; #include &lt;ctime&gt; \/* [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-5153","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/5153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5153"}],"version-history":[{"count":4,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/5153\/revisions"}],"predecessor-version":[{"id":5317,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/5153\/revisions\/5317"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5153"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}