{"id":37,"date":"2013-05-11T00:01:32","date_gmt":"2013-05-11T08:01:32","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=37"},"modified":"2013-05-04T18:50:11","modified_gmt":"2013-05-05T02:50:11","slug":"calculating-a-unix-epoch-date","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=37","title":{"rendered":"Calculating a Unix Epoch Date"},"content":{"rendered":"<p>Before you can embark upon the frightening topic of Time Math, you need to convert dates and times into Unix Epoch <code>time_t<\/code> values. It&#8217;s not that difficult of a task, assuming that you can accept that time programming in C isn&#8217;t complex and rude, which of course it is.<br \/>\n<!--more--><br \/>\nThe function that returns a Unix Epoch value is <em>mktime()<\/em>. It eats a <em>tm<\/em> structure filled with the relevant date and time values. And therein lies the rub.<\/p>\n<p>How can you fill in the <em>tm<\/em> structure? Here&#8217;s the definition of that structure from the man page:<\/p>\n<pre><code>int tm_sec;     \/* seconds (0 - 60) *\/\r\nint tm_min;     \/* minutes (0 - 59) *\/\r\nint tm_hour;    \/* hours (0 - 23) *\/\r\nint tm_mday;    \/* day of month (1 - 31) *\/\r\nint tm_mon;     \/* month of year (0 - 11) *\/\r\nint tm_year;    \/* year - 1900 *\/\r\nint tm_wday;    \/* day of week (Sunday = 0) *\/\r\nint tm_yday;    \/* day of year (0 - 365) *\/\r\nint tm_isdst;   \/* is summer time in effect? *\/\r\nchar *tm_zone;  \/* abbreviation of timezone name *\/\r\nlong tm_gmtoff; \/* offset from UTC in seconds *\/\r\n<\/code><\/pre>\n<p>That information is coupled with the notice that the structure includes &#8220;at least&#8221; those fields. Yikes.<\/p>\n<p>Filling that structure would take a lot of work, especially for the timezone string. But why do all that coding when you can have the computer do the work for you?<\/p>\n<p>The <em>localtime()<\/em> function fills the <em>tm<\/em> structure. So you could pass <em>localtime()<\/em> the current time, after which it fills the <em>tm<\/em> structure. Then you could reset specific values within the <em>tm<\/em> structure, and finally call <em>mktime()<\/em> to retrieve the proper Unix Epoch, or <em>time_t<\/em>, value. The process is exactly what I&#8217;ve done in the sample code listed below.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    int month,day,year;\r\n    struct tm *birthday;\r\n    time_t bday,today;\r\n\r\n    printf(\"Enter your birth month (1-12): \");\r\n    scanf(\"%d\",&month);\r\n    printf(\"Enter your birth day: \");\r\n    scanf(\"%d\",&day);\r\n    printf(\"Enter your birth year: \");\r\n    scanf(\"%d\",&year);\r\n\r\n    time(&today);\r\n    birthday = localtime(&today);\r\n    birthday-&gt;tm_mon = month-1;\r\n    birthday-&gt;tm_mday = day;\r\n    birthday-&gt;tm_year = year-1900;\r\n\r\n    bday = mktime(birthday);\r\n\r\n    printf(\"You were born on %d\/%d\/%d\\n\",month,day,year);\r\n    printf(\"That's Unix Epoch value %ld\\n\",bday);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code begs the user to input a date, such as a birthdate although any specific date works. You could modify the code to input time values as well.<\/p>\n<p>In Line 17, the <em>time()<\/em> function returns the current Unix Epoc value.<\/p>\n<p>Line 18 fills the <code>birthday<\/code> <em>tm<\/em> structure with the current date and time information. To make the new Epoch value, you simply have to alter specific items in the structure. Lines 19, 20, and 21 do so for the month, day, and year.<\/p>\n<p>In Line 19, the month must be reduced by one notch because the <em>tm<\/em> structure&#8217;s <em>tm_mon<\/em> value ranges from 0 through 11.<\/p>\n<p>Also, in Line 21, the year value must be expressed as years since 1900, so that value needs to be subtracted from whatever <code>year<\/code> value was input.<\/p>\n<p>Finally, at Line 23, the <em>mktime()<\/em> function translates the rebuilt <em>tm<\/em> structure <code>birthday<\/code> into a <em>time_t<\/em> value, <code>bday<\/code>.<\/p>\n<p>Be aware that dates before January 1, 1970 appear as negative <em>time_t<\/em> values, so don&#8217;t think that the computer screwed up when you&#8217;re an old fart who was born before that date and you see a negative Epoch value!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The function that returns a Unix Epoch value is <em>mktime()<\/em>. It eats a <em>tm<\/em> structure filled with the relevant date and time values. And therein lies the rub. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=37\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":48,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions\/48"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}