{"id":4642,"date":"2021-03-06T00:01:26","date_gmt":"2021-03-06T08:01:26","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4642"},"modified":"2021-02-27T10:05:32","modified_gmt":"2021-02-27T18:05:32","slug":"manual-time_t-values-and-time-zones","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4642","title":{"rendered":"Manual <em>time_t<\/em> Values and Time Zones"},"content":{"rendered":"<p>The 16-hour puzzle presented in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4628\">last week&#8217;s Lesson<\/a> has been resolved: It was actually a 24-hour puzzle, minus 8 hours for my time zone, which equals 16 hours. But what can be done about the time zone difference?<br \/>\n<!--more--><br \/>\nTime zone information is part of the computer system&#8217;s locale details. The <em>locale<\/em> defines aspects about the system based on your location, language, monetary and numeric representations, and other localized details. One of these details is the time zone.<\/p>\n<p>Computer clocks are based on Greenwich Mean Time (GMT), also known as Coordinated Universal Time (UTC). The local time is set relative to GMT based on your time zone. I&#8217;m in the Pacific time zone, which -8 hours from GMT and referred to as GMT-8. Rome is GMT+1, the Central European time zone.<\/p>\n<p>To calculate a proper <em>time_t<\/em> value, it helps to either take into consideration your time zone or adjust the time zone to GMT while the code runs.<\/p>\n<p>In the C language, <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3541\">the <em>locale()<\/em> function<\/a> deals with reporting or changing locale details, such as the currency symbol or date format. Alas, time zone information isn&#8217;t accessible by using the <em>locale()<\/em> function.<\/p>\n<p>In Unix and Linux, time zone details are kept in the <code>\/etc\/localtime<\/code> file, which is often an alias to the actual time zone file. For example, on my computers, the alias is:<\/p>\n<p><code>\/etc\/localtime -> \/var\/db\/timezone\/zoneinfo\/America\/Los_Angeles<\/code><\/p>\n<p>I&#8217;m sure you could devise code that examines the <code>\/etc\/localtime<\/code> file and perform your time zone calculations based on the results. An easier way, however, is to use the <em>tzset()<\/em> function.<\/p>\n<p>The <em>tzset()<\/em> function, defined in the <code>time.h<\/code> header file, takes no arguments and returns no values. Instead, it relies upon the <code>TZ<\/code> environment variable to set the time zone. You can use <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=17\">the <em>putenv()<\/em> function<\/a> to set <code>TZ<\/code>&#8216;s value as the program runs. The format is:<\/p>\n<p><code>TZ=<em>zone<\/em><\/code><\/p>\n<p>Where <em>zone<\/em> is a time zone string such as <code>GMT-8<\/code> or <code>GMT+1<\/code> or just <code>GMT<\/code> for Greenwich Mean time.<\/p>\n<p>To calculate a <em>time_t<\/em> value appropriate to GMT, losing the -8 hour difference from my previous, manual <em>time_t<\/em> calculations, code such as the following is used:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_03_06-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_03_06-Lesson.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\nint main()\r\n{\r\n    time_t epoch = 0;\r\n\r\n    putenv(\"TZ=GMT\");\r\n    tzset();\r\n    printf(\"Time: %s\",ctime(&amp;epoch));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 8 sets the value <code>TZ=GMT<\/code> into the environment. This change holds only as long as the program runs; modifications to the environment aren&#8217;t retained. With the <code>TZ<\/code> variable established, the <em>tzset()<\/em> function at Line 10 recalibrates the current time zone based on the value of <code>TZ<\/code>.<\/p>\n<p>When the <em>ctime()<\/em> function reports the value of <em>time_t<\/em> variable <code>epoch<\/code> at Line 11, the value is relative to GMT, not the local time zone. Here is the output:<\/p>\n<p><code>Time: Thu Jan&nbsp;&nbsp;1 00:00:00 1970<\/code><\/p>\n<p>From last week&#8217;s Lesson, the output from similar code without the time zone adjustment was:<\/p>\n<p><code>Time: Wed Dec 31 16:00:00 1969<\/code><\/p>\n<p>The 8 hour difference, which I misinterpreted as 16 hours in an earlier Lesson, is now accounted for.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you truly want an accurate <em>time_t<\/em> value, you must take into consideration your locale&#8217;s time zone. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4642\">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-4642","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\/4642","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=4642"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4642\/revisions"}],"predecessor-version":[{"id":4654,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4642\/revisions\/4654"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}