{"id":1551,"date":"2013-12-19T20:37:33","date_gmt":"2013-12-19T20:37:33","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=1551"},"modified":"2018-10-16T08:34:00","modified_gmt":"2018-10-16T08:34:00","slug":"get-current-time","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/datetime\/get-current-time","title":{"rendered":"Get Current Time"},"content":{"rendered":"<h4>\nFor a tm Strict<br \/>\n<\/h4>\n<p>\n<em><strong>Loading current time into a tm struct &#8211; see&nbsp;<a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/datetime\/struct-tm\">here<\/a><\/strong><\/em>\n<\/p>\n<h4>\nGet The Current Time And Display In A String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n#include &lt;sys\/time.h&gt;\r\n<\/code><\/pre>\n<pre>\r\n<code>\r\n\tstatic int seconds_last = 99;\r\n\tchar TimeString[128];\r\n\r\n\ttimeval curTime;\r\n\tgettimeofday(&amp;curTime, NULL);\r\n\tif (seconds_last == curTime.tv_sec)\r\n\t\treturn;\r\n\t\r\n\tseconds_last = curTime.tv_sec;\r\n\t\r\n\tstrftime(TimeString, 80, &quot;%Y-%m-%d %H:%M:%S&quot;, localtime(&amp;curTime.tv_sec));\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\ntimeval and&nbsp;gettimeofday&nbsp;gives you tvsec and tv_usec which is the number of seconds the &nbsp;Epoch. &nbsp;You need to use local time to convert into the current time.\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For a tm Strict Loading current time into a tm struct &#8211; see&nbsp;here Get The Current Time And Display In A String #include &lt;sys\/time.h&gt; static int seconds_last = 99; char TimeString[128]; timeval curTime; gettimeofday(&amp;curTime, NULL); if (seconds_last == curTime.tv_sec) return; seconds_last = curTime.tv_sec; strftime(TimeString, 80, &quot;%Y-%m-%d %H:%M:%S&quot;, localtime(&amp;curTime.tv_sec)); &nbsp; timeval and&nbsp;gettimeofday&nbsp;gives you tvsec and tv_usec [&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-1551","post","type-post","status-publish","format-standard","hentry","category-datetime"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1551","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=1551"}],"version-history":[{"count":7,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1551\/revisions"}],"predecessor-version":[{"id":2927,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1551\/revisions\/2927"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=1551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=1551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=1551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}