{"id":4236,"date":"2020-07-11T00:01:17","date_gmt":"2020-07-11T07:01:17","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4236"},"modified":"2020-07-11T08:15:17","modified_gmt":"2020-07-11T15:15:17","slug":"the-gettimeofday-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4236","title":{"rendered":"The <em>gettimeofday()<\/em> Function"},"content":{"rendered":"<p>Every so often I scour C library references, looking for fun or unusual functions. When I find one I&#8217;m unfamiliar with or something I&#8217;ve seldom used, I write about. After all, the functions do practical things that might be worthy of exploration. A recent example is <em>gettimeofday()<\/em>.<br \/>\n<!--more--><br \/>\nThe <em>gettimeofday()<\/em> is defined in the <code>sys\/time.h<\/code> header. Like many <em>get*<\/em> functions, it has a companion, <em>settimeofday()<\/em>. Yep, these are system functions.<\/p>\n<p>The <em>gettimeofday()<\/em> function fills two structures with details about (you guessed it) the current time of day:<\/p>\n<p><code>int gettimeofday( struct timeval *, struct tzp *);<\/code><\/p>\n<p>The <em>timeval<\/em> structure contains two members, <em>time_t<\/em> variable <code>tv_sec<\/code> and <em>suseconds_t<\/em> variable <code>tv_usec<\/code>. The first, <code>tv_sec<\/code>, is a <em>time_t<\/em> value, the number of seconds elapsed since January 1, 1970. The second is a microsecond value, which the computer knows but isn&#8217;t included with the <em>time_t<\/em> value.<\/p>\n<p>The <em>tzp<\/em> structure contains two members relevant to time zone information: <em>int<\/em> <code>tz_minuteswest<\/code> which is the number of minutes west of GMT for the system&#8217;s current time zone. Some member <code>tz_dsttime<\/code> is an integer that indicates the number of hours to adjust for daylight saving time. These are time details not readily available in the other time functions.<\/p>\n<p>The companion <em>settimteofday()<\/em> function uses the same arguments, but with the structures passed setting various time tidbits. This function can be called only by the superuser, so my focus in this post is on <em>gettimeofday()<\/em>.<\/p>\n<p>The <em>gettimeofday()<\/em> function returns 0 upon success and -1 on failure. The <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1735\">errno<\/a> variable is set when the function fails.<\/p>\n<p>Here&#8217;s my <em>gettimeofday()<\/em> test program:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_07_11-Lesson-a.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_07_11-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;sys\/time.h&gt;\r\n\r\nint main()\r\n{\r\n    struct timeval tv;\r\n    struct timezone tz;\r\n\r\n    gettimeofday(&amp;tv,&amp;tz);\r\n\r\n    printf(\"Seconds since 1\/1\/1970: %lu\\n\",tv.tv_sec);\r\n    printf(\"Microseconds: %d\\n\",tv.tv_usec);\r\n    printf(\"Minutes west of Greenwich: %d\\n\",tz.tz_minuteswest);\r\n    printf(\"Daylight Saving Time adjustment: %d\\n\",tz.tz_dsttime);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>gettimeofday()<\/em> function&#8217;s structure arguments are declared at Lines 6 and 7. If you declare them as pointers, ensure that you assign them to an address. Pointers passed to the function can be set to <code>NULL<\/code>, though if both are set to <code>NULL<\/code> the function won&#8217;t fill them.<\/p>\n<p>Here is the program&#8217;s output:<\/p>\n<p><code>Seconds since 1\/1\/1970: 1593275915<br \/>\nMicroseconds: 613564<br \/>\nMinutes west of Greenwich: 480<br \/>\nDaylight Saving Time adjustment: 1<\/code><\/p>\n<p>Obviously this data isn&#8217;t quite useful, thought I&#8217;m certain some nerd somewhere can read a raw <em>time_t<\/em> value. Here is an improvement to the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_07_11-Lesson-b.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_07_11-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;sys\/time.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    struct timeval tv;\r\n    struct timezone tz;\r\n    struct tm *today;\r\n    int zone;\r\n\r\n    gettimeofday(&amp;tv,&amp;tz);\r\n\r\n    <span class=\"comments\">\/* get time details *\/<\/span>\r\n    today = localtime(&amp;tv.tv_sec);\r\n    printf(\"It's %d:%0d:%0d.%d\\n\",\r\n            today-&gt;tm_hour,\r\n            today-&gt;tm_min,\r\n            today-&gt;tm_sec,\r\n            tv.tv_usec\r\n          );\r\n    <span class=\"comments\">\/* set time zone value to hours, not minutes *\/<\/span>\r\n    zone = tz.tz_minuteswest\/60;\r\n    <span class=\"comments\">\/* calculate for zones east of GMT *\/<\/span>\r\n    if( zone &gt; 12 )\r\n        zone -= 24;\r\n    printf(\"Time zone: GMT %+d\\n\",zone);\r\n    printf(\"Daylight Saving Time adjustment: %d\\n\",tz.tz_dsttime);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>I added the <em>localtime()<\/em> function to deal with the <em>time_t<\/em> value from <code>tv.tv_sec<\/code>. This value is translated into a clock value HH:MM:SS plus the microsecond value, output at Lines 16 through 21.<\/p>\n<p>The time zone value from <code>tz.tz_minuteswest<\/code> is saved in the <code>zone<\/code> variable. Lines 25 and 26 adjust this value to account for time zones east of GMT. This result is output at Line 27. The <code>%+d<\/code> placeholder ensures that a sign, + or &#8211; prefixes the value.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<p><code>It's 10:15:23.952229<br \/>\nTime zone: GMT +8<br \/>\nDaylight Saving Time adjustment: 1<\/code><\/p>\n<p>I&#8217;m uncertain as to how I could use <em>gettimeofday()<\/em> function as the other time function have suited my needs. Still, this function is available and I&#8217;m glad I found it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>And I thought I knew all the C language time functions. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4236\">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-4236","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\/4236","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=4236"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4236\/revisions"}],"predecessor-version":[{"id":4254,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4236\/revisions\/4254"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}