{"id":2073,"date":"2015-06-17T15:20:46","date_gmt":"2015-06-17T15:20:46","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2073"},"modified":"2020-04-07T09:47:10","modified_gmt":"2020-04-07T09:47:10","slug":"datetime-calculations","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/datetime\/datetime-calculations","title":{"rendered":"DateTime calculations"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Calculate&nbsp;the difference&nbsp;between two struct tm values<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\ttm event_date_time;\n\ttimeval curTime;\n\ttime_t time_now;\n\ttime_t time_event;\n\t\/\/Note time_t is num of seconds since 1970 so your tm_year must be >= 1970 or you'll get a -1 value from time_t!!\n\n\tgettimeofday(&amp;curTime, NULL);\t\t\/\/Get the RPi's current time\n\n\tevent_date_time.tm_year = 20;\n\tevent_date_time.tm_mon = 1;\n\tevent_date_time.tm_mday = 1;\n\tevent_date_time.tm_hour = 1;\n\tevent_date_time.tm_min = 1;\n\tevent_date_time.tm_sec = 1;\n\n\ttime_now = mktime(localtime(&amp;curTime.tv_sec));\t\/\/Convert tm to time_t\n\ttime_event = mktime(&amp;event_date_time);\t\t\t\t\/\/Convert tm to time_t\n\n\tint difference = (int)difftime(time_now, time_event);\t\/\/Get the difference in seconds (If positive, then time_now > time_event)\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Adjusting tm values (add\/subtract secs, mins, days, etc)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\ttm1.tm_mday += 7;\n\tmktime(&amp;tm1);\t\t\/\/A call to this mktime() automatically adjusts the values of the members of timeptr it is called with if they are off-range (except tm_wday and tm_yday)<\/code><\/pre>\n\n\n\n<p>Adjusting the struct tm values won&#8217;t cause roll overs (e.g. if you -100 from .tm_second you will get a negative .tm_second value), you get left with the values adjusted but a potentially out of range value. mktime() is used to convert but it will correct the tm poitner it is called with so the vcalues are in range, so we don&#8217;t bother getting its result and instead use this handy side effect of calling it.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Add Or Subtract Days To A Date<\/h4>\n\n\n\n<p><em>This uses the answer suggested by @tenfour <a href=\"http:\/\/stackoverflow.com\/questions\/2344330\/algorithm-to-add-or-subtract-days-from-a-date\/2344845#2344845\">here<\/a>.<\/em><\/p>\n\n\n\n<p><em>NOT SURE ABOUT THIS SOLUTIONS USE OF LOCALTIME&#8211;_NEEDS CONFIRMING IS GOOD??<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/******************************************************\n\/\/******************************************************\n\/\/********** ADD OR SUBTRACT DAYS FROM A DATE **********\n\/\/******************************************************\n\/\/******************************************************\nvoid add_subtract_days_from_date(struct tm *date, int days_to_add_or_subtract)\n{\n\tconst time_t ONE_DAY = 24 * 60 * 60 ;\n\n\ttime_t date_seconds = mktime(date) + (days * ONE_DAY) ;\t\t\/\/Seconds since start of epoch\n\n\t*date = *localtime(&amp;date_seconds);\t\t\t\t\/\/Use localtime because mktime converts to UTC so may change date\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Calculate&nbsp;the difference&nbsp;between two struct tm values Adjusting tm values (add\/subtract secs, mins, days, etc) Adjusting the struct tm values won&#8217;t cause roll overs (e.g. if you -100 from .tm_second you will get a negative .tm_second value), you get left with the values adjusted but a potentially out of range value. mktime() is used to convert [&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-2073","post","type-post","status-publish","format-standard","hentry","category-datetime"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2073","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=2073"}],"version-history":[{"count":11,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2073\/revisions"}],"predecessor-version":[{"id":3213,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2073\/revisions\/3213"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}