{"id":1405,"date":"2023-08-24T14:51:00","date_gmt":"2023-08-24T13:51:00","guid":{"rendered":"https:\/\/ibex.tech\/c\/?p=1405"},"modified":"2023-08-25T17:51:46","modified_gmt":"2023-08-25T16:51:46","slug":"modifying-datetime","status":"publish","type":"post","link":"https:\/\/ibex.tech\/c\/c\/datetime\/modifying-datetime","title":{"rendered":"Modifying Date and Time"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Adjusting tm<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tstruct tm Tm1 = {0};\n\tchar TextBuffer&#91;80];\n\n\n\t\/\/Convert UTC string to tm\n\tstrptime(\"2020-08-29 11:05:13\", \"%Y-%m-%d %H:%M:%S\", &amp;Tm1);\t\t\/\/Convert the character string pointed to by buf to values which are stored in the tm structure pointed to by tm, using the format specified by format\n\n\t\/\/Convert tm to string\n\t\/\/strftime(TextBuffer, sizeof(TextBuffer), \"%Y-%m-%d %H:%M:%S\", &amp;Tm1);\n\t\/\/printf(\"UTC: %s\\n\", TextBuffer);\n\n\n\t\/\/----- ADD SOME DAYS -----\n\tTm1.tm_mday += 1;\n\tTm1.tm_mday += 1;\n\tTm1.tm_mday += 1;\n\tmktime(&amp;Tm1);\t\t\t\t\t\t\/\/Normalise the structure (before you do this, Tm1 will output as \"2020-08-32 11:05:13\", after you do this \"2020-09-01 11:05:13\")\n\n\tstrftime(TextBuffer, sizeof(TextBuffer), \"%Y-%m-%d %H:%M:%S\", &amp;Tm1);\n\tprintf(\"UTC: %s\\n\", TextBuffer);\n\n\n\t\/\/----- SUBTRACT SOME HOURS -----\n\tTm1.tm_hour -= 12;\n\tmktime(&amp;Tm1);\t\t\t\t\t\t\/\/Normalise the structure (before you do this, Tm1 will output as \"2020-09-01 -01:05:13\", after you do this \"2020-08-31 23:05:13\")\n\n\tstrftime(TextBuffer, sizeof(TextBuffer), \"%Y-%m-%d %H:%M:%S\", &amp;Tm1);\n\tprintf(\"UTC: %s\\n\", TextBuffer);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Adjusting time_t<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/******************************************************\n\/\/********** ADD OR SUBTRACT DAYS FROM A DATE **********\n\/\/******************************************************\nvoid AddSubtractDaysFromDate(struct tm *Date, int DaysToAddOrSubtract)\n{\n\tconst time_t ONE_DAY = 24 * 60 * 60 ;\n\n\ttime_t DateSeconds = mktime(Date) + (DaysToAddOrSubtract * ONE_DAY) ;\t\t\/\/Seconds since start of epoch\n\n\t*Date = *localtime(&amp;DateSeconds);\t\t\t\t\/\/Use localtime because mktime converts to UTC so may change date\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Adjusting tm Adjusting time_t<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118],"tags":[],"class_list":["post-1405","post","type-post","status-publish","format-standard","hentry","category-datetime"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/comments?post=1405"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1405\/revisions"}],"predecessor-version":[{"id":1479,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1405\/revisions\/1479"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/media?parent=1405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/categories?post=1405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/tags?post=1405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}