{"id":5862,"date":"2023-05-06T00:01:09","date_gmt":"2023-05-06T07:01:09","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5862"},"modified":"2023-04-29T10:14:22","modified_gmt":"2023-04-29T17:14:22","slug":"checking-the-time","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5862","title":{"rendered":"Checking the Time"},"content":{"rendered":"<p>I tend to keep the lessons on this blog specific to the standard C library, C99 release. Yet every so often a newer function draws my attention, which may not be standard but can still be useful. Case in point is the <em>clock_gettime()<\/em> function, which subscriber M.Stumpfl mentioned in a <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5797\">previous post<\/a>.<br \/>\n<!--more--><br \/>\nThe <em>clock_gettime()<\/em> function is part of a family of <code>time.h<\/code> functions, including <em>clock_getres()<\/em> and <em>clock_settime()<\/em>. As these are newer functions, they may require linking in the <code>rt<\/code> library for older C compilers.<\/p>\n<p>Like the <em>time()<\/em> function, <em>clock_gettime()<\/em> returns the number of seconds in the Unix Epoch, the time elapsed since midnight January 1, 1970. It also includes the number of nanoseconds elapsed since the last second. This makes the function far more precise than <em>time()<\/em>, which tracks only seconds (a <em>time_t<\/em> value).<\/p>\n<p>Here is the function&#8217;s <em>man<\/em> page format:<\/p>\n<p><code>int clock_gettime(clockid_t clockid, struct timespec *tp);<\/code><\/p>\n<p>The <em>clockid<\/em> value is a constant representing a clock to examine. The constant <code>CLOCK_REALTIME<\/code> serves as the system clock. In my earlier post, M.Stumpfl used the <code>CLOCK_MONOTONIC<\/code> constant, which isn&#8217;t affected by jumps that occur in the system clock. But <code>CLOCK_MONOTONIC<\/code>&#8216;s values represent the number of seconds elapsed since the system was last booted, not the Epoch value.<\/p>\n<p>The second argument is the address of a <em>timespec<\/em> structure. This structure has two members:<\/p>\n<p><code>struct timespec {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;time_t&nbsp;&nbsp;&nbsp;tv_sec;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/* seconds *\/<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tv_nsec;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/* nanoseconds *\/<br \/>\n};<\/code><\/p>\n<p>Member <em>tv_sec<\/em> is the <em>time_t<\/em> value, the number of seconds elapsed since the Epoch. Member <em>tv_nsec<\/em> is the number of nanoseconds (billionths of a second) that have passed since the last clock tick. It&#8217;s this second value that adds such precision to the <em>clock_gettime()<\/em> function.<\/p>\n<p>The following code shows the <em>clock_gettime()<\/em> function in action, outputting the current values retrieved from the system clock:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_05_06-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2023_05_06-Lesson-a.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    struct timespec res;\r\n\r\n    clock_gettime(CLOCK_REALTIME,&amp;res);\r\n    printf(\"Seconds: %lu\\nNanoseconds: %lu\\n\",\r\n            res.tv_sec,\r\n            res.tv_nsec\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The function is called with the <code>CLOCK_REALTIME<\/code> argument, using the system clock. The <em>timespec<\/em> structure <code>res<\/code> holds the values returned, which are output in the <em>printf()<\/em> statement. The <code>%lu<\/code> (<em>long unsigned<\/em>) placeholder is used for both values. Here is the output:<\/p>\n<p><code>Seconds: 1682175617<br \/>\nNanoseconds: 242989728<\/code><\/p>\n<p>The Seconds value is the Unix Epoch, the same value that would be returned from the <em>time()<\/em> function. The value I&#8217;m more interested in is Nanoseconds. Obviously, billionths of seconds are going to fly by more quickly than your reservation time at the Apple Store Genius Bar.<\/p>\n<p>To monitor the nanosecond progress, I updated the code as shown below.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_05_06-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2023_05_06-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    struct timespec res;\r\n    int count;\r\n\r\n    <span class=\"comments\">\/* loop to read nanosecond values *\/<\/span>\r\n    count = 20;\r\n    while( count-- )\r\n    {\r\n        clock_gettime(CLOCK_REALTIME,&amp;res);\r\n        printf(\"%lu\\n\",res.tv_nsec);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>A <em>while<\/em> loop repeats 20 times, each iteration calling the <em>clock_gettime()<\/em> function and outputting only the nanosecond value. Here&#8217;s the output:<\/p>\n<p><code>196619560<br \/>\n196697155<br \/>\n196699640<br \/>\n196700626<br \/>\n196701428<br \/>\n196702211<br \/>\n196702990<br \/>\n196703773<br \/>\n196704726<br \/>\n196705564<br \/>\n196706410<br \/>\n196707235<br \/>\n196708010<br \/>\n196708788<br \/>\n196709578<br \/>\n196710346<br \/>\n196711292<br \/>\n196712156<br \/>\n196713012<br \/>\n196713861<\/code><\/p>\n<p>These values aren&#8217;t consecutive because it takes the computer longer than a nanosecond to output text to the terminal. But if you modify the code to output more values, you see the nanosecond numbers &#8220;flip over&#8221; with each passing second. Doing so generates a lot of output.<\/p>\n<p>In next week&#8217;s Lesson, I continue my fascination with the <em>clock_gettime()<\/em> function and its nanosecond values.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>time()<\/em> function is nice, but the <em>clock_gettime()<\/em> function is more precise. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5862\">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-5862","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\/5862","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=5862"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5862\/revisions"}],"predecessor-version":[{"id":5873,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5862\/revisions\/5873"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}