{"id":2052,"date":"2015-06-11T07:24:47","date_gmt":"2015-06-11T07:24:47","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2052"},"modified":"2020-04-07T07:38:40","modified_gmt":"2020-04-07T07:38:40","slug":"struct-tm","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/datetime\/struct-tm","title":{"rendered":"struct tm"},"content":{"rendered":"<p>A good way of storing and working with\u00a0dates and time in C++. \u00a0A structure containing a calendar date and time broken down into its components.<\/p>\n<h4>Creating a tm<\/h4>\n<pre><code>\n#include &lt;ctime&gt;\n\t\n\ttm my_date_time;\n\n\tmy_date_time.tm_year = (2015 - 1900);\t\/\/years since 1900 (we must be &gt;= 1970 for time_t based calcs)\n\tmy_date_time.tm_mon = 1;\t\t\/\/months since January (0-11)\n\tmy_date_time.tm_mday = 18;\n\tmy_date_time.tm_hour = 12;\n\tmy_date_time.tm_min = 03;\n\tmy_date_time.tm_sec = 48;\n<\/code><\/pre>\n<p><span style=\"color: #ff0000;\"><em><strong>Note:<\/strong><\/em><\/span><\/p>\n<p style=\"margin-left: 40px;\"><span style=\"color: #ff0000;\"><em><strong>tm_year is\u00a0years since 1900!!<\/strong><\/em><\/span><br \/>\n<em style=\"color: #ff0000;\"><strong>tm_month\u00a0is\u00a0months\u00a0since January (0-11)!!<br \/>\nNote when converting to time_t your year must be &gt;= 1970 as time_t is usually based from 00:00, Jan 1 1970 UTC!<\/strong><\/em><\/p>\n<h4>Loading with current time<\/h4>\n<pre><code>\n#include &lt;time.h&gt;\n#include &lt;sys\/time.h&gt;\n\n\ttimeval curTime;\n\ttm *my_date_time;\n\tgettimeofday(&amp;curTime, NULL);\n\tmy_date_time = localtime(&amp;curTime.tv_sec);\n\t = my_date_time-&gt;tm_year - 100;\n\t = my_date_time-&gt;tm_mon + 1;\n\t = my_date_time-&gt;tm_mday;\n\t = my_date_time-&gt;tm_hour;\n\t = my_date_time-&gt;tm_min;\n\t = my_date_time-&gt;tm_sec;\n<\/code><\/pre>\n<h4>Converting tm to Universal Time String<\/h4>\n<pre><code>\n\tchar my_date_time_string[22];\n\tstrftime(my_date_time_string, sizeof(my_date_time_string), \"%Y-%m-%dT%H:%M:%S\", &amp;my_date_time);\n<\/code><\/pre>\n<h4>Convert tm to StringStream<\/h4>\n<pre><code>\n\t\/\/Create as yyyymmddhh\n\tstringstream ss_archive_file_timestamp;\n\tss_archive_file_timestamp &lt;&lt; (tmFromDate.tm_year + 1900);\t\t\t\t\t\t\t\/\/2000 - 2099\n\tss_archive_file_timestamp &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; (tmFromDate.tm_mon + 1);\t\/\/1-12\n\tss_archive_file_timestamp &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; tmFromDate.tm_mday;\t\t\t\/\/1-31\n\tss_archive_file_timestamp &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; tmFromDate.tm_hour;\t\t\t\/\/0-23\n\n\t\/\/Create in UTC format\n\tstringstream ss_archive_file_timestamp_utc;\n\tss_archive_file_timestamp_utc &lt;&lt; (tmFromDate.tm_year + 1900) &lt;&lt; \"-\";\t\t\t\t\t\t\t\/\/2000 - 2099\n\tss_archive_file_timestamp_utc &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; (tmFromDate.tm_mon + 1) &lt;&lt; \"-\";\t\t\/\/1-12\n\tss_archive_file_timestamp_utc &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; tmFromDate.tm_mday &lt;&lt; \" \";\t\t\t\/\/1-31\n\tss_archive_file_timestamp_utc &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; tmFromDate.tm_hour &lt;&lt; \":00:00\";\t\t\/\/0-23\n<\/code><\/pre>\n<h4>Convert String To\u00a0DateTime<\/h4>\n<pre><code>\n\tstd::tm tm1 = {};\n\tif (strptime(MyDateCharString, \"%Y-%m-%d\", &amp;tm1) == NULL)    \/\/Can include time characters too if wanted \"%Y-%m-%d %H:%M:%S\"\n\t{\n\t\t\/\/Failed\n\n\t}\n<\/code><\/pre>\n<h4>Compare tm&#8217;s<\/h4>\n<pre><code>\n\tif (mktime(&amp;tm1) &gt; mktime(&amp;tm2))\t\t\/\/Larger values of mktime() are more recent in time\n\t{\n\t}\n<\/code><\/pre>\n<h4>Add, Subtract with tm&#8217;s<\/h4>\n<p><a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/datetime\/datetime-calculations\">https:\/\/raspberry-projects.com\/pi\/programming-in-c\/datetime\/datetime-calculations<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A good way of storing and working with\u00a0dates and time in C++. \u00a0A structure containing a calendar date and time broken down into its components. Creating a tm #include &lt;ctime&gt; tm my_date_time; my_date_time.tm_year = (2015 &#8211; 1900); \/\/years since 1900 (we must be &gt;= 1970 for time_t based calcs) my_date_time.tm_mon = 1; \/\/months since January [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[],"class_list":["post-2052","post","type-post","status-publish","format-standard","hentry","category-datetime"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2052"}],"version-history":[{"count":21,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2052\/revisions"}],"predecessor-version":[{"id":3209,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2052\/revisions\/3209"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}