{"id":4628,"date":"2021-02-27T00:01:26","date_gmt":"2021-02-27T08:01:26","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4628"},"modified":"2021-03-06T09:43:01","modified_gmt":"2021-03-06T17:43:01","slug":"the-time_t-value-and-16-hours","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4628","title":{"rendered":"The <em>time_t<\/em> Value . . . and 16 Hours?"},"content":{"rendered":"<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4616\">last week&#8217;s Lesson<\/a>, I covered the mechanics behind manually calculating a <em>time_t<\/em> value. My code was successful, but in a curious way.<br \/>\n<!--more--><br \/>\nHere is the full code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_02_27-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2021_02_27-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n<span class=\"comments\">\/* calculate days in February\r\n   from blog post https:\/\/c-for-dummies.com\/blog\/?p=3134\r\n   this update returns the days of the month, not T\/F\r\n*\/<\/span>\r\nint february(int year)\r\n{\r\n    <span class=\"comments\">\/* if the year is divisible by both 100 and\r\n       400, it's a leap year *\/<\/span>\r\n    if( (year%400)==0 )\r\n        return(29);\r\n    <span class=\"comments\">\/* if the year is divisble by 100, it's not\r\n       a leap year *\/<\/span>\r\n    if( (year%100)==0 )\r\n        return(28);\r\n    <span class=\"comments\">\/* check for 4 year interval, which is redundant\r\n       here, but I'll do it anyway *\/<\/span>\r\n    if( (year%4) != 0 )\r\n        return(28);\r\n    <span class=\"comments\">\/* otherwise, it's a leap year *\/<\/span>\r\n    return(29);\r\n}\r\n\r\n<span class=\"comments\">\/* Unix Epoch math *\/<\/span>\r\ntime_t epoch(int year, int mon, int day)\r\n{\r\n    int days,y,m;\r\n    int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };\r\n\r\n    <span class=\"comments\">\/* calculate the number of days *\/<\/span>\r\n    days = 0;\r\n    for( y=1970; y&lt;year; y++ )\r\n    {\r\n        for( m=0; m&lt;12; m++ )\r\n        {\r\n            if( m==1 )        <span class=\"comments\">\/* february *\/<\/span>\r\n                days += february(y);\r\n            else\r\n                days += month[m];\r\n        }\r\n    }\r\n    <span class=\"comments\">\/* calculate final year, months only *\/<\/span>\r\n    for( m=0; m&lt;mon-1; m++ )\r\n    {\r\n        if( m==1 )        <span class=\"comments\">\/* february *\/<\/span>\r\n            days += february(y);        <span class=\"comments\">\/* y value is proper *\/<\/span>\r\n        else\r\n               days+=month[m];\r\n    }\r\n    <span class=\"comments\">\/* add the day *\/<\/span>\r\n    days += day;\r\n    <span class=\"comments\">\/* tally up the seconds *\/<\/span>\r\n    return (time_t)(days * 24 * 60 * 60);\r\n}\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    int year, month, day;\r\n    time_t now,my_now;\r\n    struct tm *today;\r\n    \r\n    <span class=\"comments\">\/* get current time *\/<\/span>\r\n    now = time(NULL);\r\n\r\n    <span class=\"comments\">\/* obtain invidiual values *\/<\/span>\r\n    today = localtime(&amp;now);\r\n    year = today-&gt;tm_year+1900;\r\n    month = today-&gt;tm_mon+1;\r\n    day = today-&gt;tm_mday;\r\n\r\n    <span class=\"comments\">\/* get calculated time_t value *\/<\/span>\r\n    my_now = epoch(year,month,day);\r\n\r\n    <span class=\"comments\">\/* output results *\/<\/span>\r\n    printf(\"For %4d %2d %2d:\\n\",year,month,day);\r\n    printf(\"time_t value is:    %ld, %s\",now,ctime(&amp;now));\r\n    printf(\"My time_t value is: %ld, %s\",my_now,ctime(&amp;my_now));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>main()<\/em> function fills <em>tm<\/em> structure <code>today<\/code> with the current day&#8217;s time values. This part of the code is done for comparison purposes, but also to obtain the current year, month, and date. These values are passed to the <em>epoch()<\/em> function.<\/p>\n<p>In the <em>epoch()<\/em> function, the code tallies the number of days for each month and cumulatively for each year until the current day. The <em>february()<\/em> function is called to ensure that days in leap years are properly counted.<\/p>\n<p>The program&#8217;s output shows the <em>time_t<\/em> value for the current time as well as my manually calculated <em>time_t<\/em> value, which is off by 16 hours:<\/p>\n<p><code>For 2021&nbsp;&nbsp;2 13:<br \/>\ntime_t value is:&nbsp;&nbsp;&nbsp;&nbsp;1613238794, Sat Feb 13 09:53:14 2021<br \/>\nMy time_t value is: 1613260800, Sat Feb 13 16:00:00 2021<\/code><\/p>\n<p>In the output, the <em>ctime()<\/em> function shows the manually calculated <em>time_t<\/em> value as 16:00 and not midnight. I have no idea why. Tests run on all major platforms &mdash; Windows, Mac, and Linux &mdash; yielded the same result.<\/p>\n<p>I believe the difference may have to do with my time zone, though I&#8217;m 8 hours <em>behind<\/em> UTF, not 16 hours ahead.<\/p>\n<p>To test further, I wrote the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_02_27-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2021_02_27-Lesson-b.c<\/a><\/h3>\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    time_t epoch = 0;\r\n\r\n    printf(\"Time: %s\\n\",ctime(&amp;epoch));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code quickly outputs the Unix Epoch value, which should be midnight January 1, 1970. But here&#8217;s the output:<\/p>\n<p><code>Time: Wed Dec 31 16:00:00 1969<\/code><\/p>\n<p>Uh.<\/p>\n<p>Then it dawned on me. December 31, 1969 is indeed 8 hours behind UTF. My manual calculation is correct! But because I include the current day in my calculation, it&#8217;s one day ahead <em>minus<\/em> 8 hours for my locale&#8217;s time zone. Problem solved! Yet, the code must be updated with the locale calculation given the current time zone. I plumb this depth in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4642\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For some puzzling reason, my computer generates a <em>time_t<\/em> value that appears to be off by 16 hours. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4628\">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-4628","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\/4628","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=4628"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4628\/revisions"}],"predecessor-version":[{"id":4660,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4628\/revisions\/4660"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}