{"id":24,"date":"2017-09-21T13:36:51","date_gmt":"2017-09-21T12:36:51","guid":{"rendered":"https:\/\/blog.inplico.uk\/?p=24"},"modified":"2017-09-21T13:38:06","modified_gmt":"2017-09-21T12:38:06","slug":"overloading-functions-when-using-time_t","status":"publish","type":"post","link":"https:\/\/blog.inplico.uk\/?p=24","title":{"rendered":"Overloading functions when using <ctime> time_t"},"content":{"rendered":"<p>One of the problems that we have when using postgresql is the management of time stamps.\u00a0 This is especially true when using <strong>PQexecparams() <\/strong>for reasons that I will not go in to in this post; suffice it to say that when building <strong>INSERT<\/strong> statements that use time stamps you may need to get creative.<\/p>\n<p>One of the problems that I have encountered is when overloading functions.\u00a0 This is because in essence <strong>time_t<\/strong> is of type <strong>int<\/strong> so if you have 2 overload functions thus, you will get a compile error.<\/p>\n<pre class=\"lang:c++ decode:true \" title=\"Compile Error\">void my_func(int i) {\r\n\t\/\/do_something.....\r\n}\r\n\r\nvoid myfunc(time_t i) {\r\n\t\/\/do_something.....\r\n}\r\n<\/pre>\n<p>Using typedef is not an option because the compiler just sees it as an alias, resulting in the same error, so if we want to do this in a clean and easily readable manner then we need to get creative and use a struct which we are simply going to call tm_epoch.<\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;ctime&gt;\r\n\r\nstruct tm_epoch { \r\n\tconst time_t te;\r\n\ttm_epoch(time_t te)\r\n\t:\r\n\tte(te){}\r\n};<\/pre>\n<p>All that we are doing here is creating a struct that we can pass a value that will be used to initialise a local time_t variable to use later.<\/p>\n<p>Now when we declare our overloaded functions we will no longer have a compile error because one variable is of type <strong>int<\/strong> and the other is of type <strong>tm_epoch<\/strong>.<\/p>\n<pre class=\"lang:c++ mark:1-6 decode:true\">struct tm_epoch { \r\n\tconst time_t te;\r\n\ttm_epoch(time_t te)\r\n\t:\r\n\tte(te){}\r\n};\r\n\r\nvoid my_func(tm_epoch t) {\r\n  \t\/\/do_something... \r\n}\r\n\r\nvoid myfunc(int i) {\r\n\t\/\/do_something...\r\n}<\/pre>\n<p>Now when we declare a variable of type <strong>tm_epoch<\/strong> and a variable of type <strong>int<\/strong>, when we call our overloaded function with\u00a0 the appropriate argument, the program will know which version of the function to call.<\/p>\n<p>Here is a little proof of concept application that demonstrates how this works in practice:<\/p>\n<pre class=\"lang:c++ mark:30-34 decode:true \" title=\"A working proof of concept\">#include &lt;ctime&gt;\r\n#include &lt;iostream&gt;\r\n\r\nusing namespace std;\r\n\r\nstruct tm_epoch { \r\n\tconst time_t te;\r\n\ttm_epoch(time_t te)\r\n\t:\r\n\tte(te){}\r\n};\r\n\r\nvoid myfunc(tm_epoch curTime) {\r\n\r\n  \tstruct tm *ts {0};\r\n  \tts = localtime(&amp;curTime.te); \t\r\n  \tchar buf[20];\r\n    strftime(buf, sizeof(buf), \"%d-%m-%Y %H:%M:%S\", ts);\r\n    cout &lt;&lt; buf &lt;&lt; endl;\r\n  \t\r\n}\r\n\r\nvoid myfunc(int i) {\r\n\tcout &lt;&lt; i &lt;&lt; endl;\r\n}\r\n\r\nint main() {\r\n\r\n\t\/\/Should be 2017-09-21 09:44:05\r\n\ttm_epoch t = 1505983445;\r\n\tint i = 1505983445;\r\n\t\r\n\tmyfunc(t);\r\n\tmyfunc(i);\r\n\t\r\n\treturn(0);\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the problems that we have when using postgresql is the management of time stamps.\u00a0 This is especially true when using PQexecparams() for reasons that I will not go in to in this post; suffice it to say that when building INSERT statements that use time stamps you may need to get creative. One [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-dates-times","category-hints-tricks"],"_links":{"self":[{"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24"}],"version-history":[{"count":2,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":26,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions\/26"}],"wp:attachment":[{"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.inplico.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}