<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>gettimeofday returns incorrect timestamp after ~7 days</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/83794/gettimeofday-returns-incorrect-timestamp-after-7-days</link><description>I am working on UTC time synchronization using SNTP. The timestamp is correctly obtained and set using clock_settime(). I wanted to determine at when to resync with the NTP server. I wrote the code below that is called every time an message is send containing</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 28 Jan 2022 11:17:15 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/83794/gettimeofday-returns-incorrect-timestamp-after-7-days" /><item><title>RE: gettimeofday returns incorrect timestamp after ~7 days</title><link>https://devzone.nordicsemi.com/thread/350179?ContentTypeID=1</link><pubDate>Fri, 28 Jan 2022 11:17:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5bab5b75-8045-4453-8757-73cd3e33e82b</guid><dc:creator>esscriber</dc:creator><description>&lt;p&gt;The problem that I was running into was caused by k_ticks_to_ns_floor64, but the overflow in z_imp_clock_gettime that you pointed out would have been an issue at an later stage.&lt;/p&gt;
&lt;p&gt;After searching through code on Zephyr&amp;#39;s github, I found that this issue was reported on github (&lt;a href="https://github.com/zephyrproject-rtos/zephyr/issues/41111"&gt;https://github.com/zephyrproject-rtos/zephyr/issues/41111&lt;/a&gt;). A solution was pushed to github on the day that I created this Q&amp;amp;A (&lt;a href="https://github.com/zephyrproject-rtos/zephyr/pull/41602"&gt;https://github.com/zephyrproject-rtos/zephyr/pull/41602&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The overflow that you pointed out has also been fixed (&lt;a href="https://github.com/zephyrproject-rtos/zephyr/pull/41814"&gt;https://github.com/zephyrproject-rtos/zephyr/pull/41814&lt;/a&gt;).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: gettimeofday returns incorrect timestamp after ~7 days</title><link>https://devzone.nordicsemi.com/thread/348992?ContentTypeID=1</link><pubDate>Fri, 21 Jan 2022 15:07:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0011c464-959e-4a72-bf66-684664580f96</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;Right, most Zephyr applications don&amp;#39;t keep track of real time, only the local system ticks, and these posix libraries are there mostly to make it easier porting linux applications as far as I&amp;#39;ve understood, which is probably why this isn&amp;#39;t a more wide spread issue.&lt;/p&gt;
&lt;p&gt;I do agree it seems like it is this part of the gettime code that is causing the unfortunate quick overflow:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;elapsed_nsecs = k_ticks_to_ns_floor64(k_uptime_ticks());
ts-&amp;gt;tv_sec = (int32_t) (elapsed_nsecs / NSEC_PER_SEC);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;But I believe it might be the part where nsecs is divided by a billion and typecasted from uint64 to int32, and possibly not the k_ticks_to_ns_floor64 function itself.&lt;/p&gt;
&lt;p&gt;You might want to look into whether it could suit your application to use one of Zephyr&amp;#39;s APIs, for example Counter which can give you an interrupt when it overflows.&lt;/p&gt;
&lt;p&gt;You could also report this as an issue on Zephyr&amp;#39;s github if you believe it is not the intended behavior.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: gettimeofday returns incorrect timestamp after ~7 days</title><link>https://devzone.nordicsemi.com/thread/348650?ContentTypeID=1</link><pubDate>Thu, 20 Jan 2022 09:32:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e1d49a4c-8641-47d3-b4d4-e6fb3e11a199</guid><dc:creator>esscriber</dc:creator><description>&lt;p&gt;It is indeed the &lt;span&gt;k_ticks_to_ns_floor64 function that overflows every ~6.51 days. Looking at the code in zephyr/lib/posix/clock.c, the only way to counteract the overflow is to resync with the NTP server after a rollover, with the possibility of having gaps where the time is in the past. Syncing before the rollover will have little to no effect.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;This will happen to every board where Z_HZ_ticks=32768. So I would assume that this would be a more wide spread issue, but I couldn&amp;#39;t find anything related to this.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: gettimeofday returns incorrect timestamp after ~7 days</title><link>https://devzone.nordicsemi.com/thread/348352?ContentTypeID=1</link><pubDate>Tue, 18 Jan 2022 15:54:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a9dcd438-d8a5-4e35-b3e1-5b34b2250989</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;You might be right, it could just be the&amp;nbsp;&lt;span&gt;k_ticks_to_ns_floor64&amp;nbsp;function overflowing.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;One way to interface with the RTC is through Zephyr&amp;#39;s Counter API:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/peripherals/counter.html#counter-api"&gt;http://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/peripherals/counter.html#counter-api&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;-Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: gettimeofday returns incorrect timestamp after ~7 days</title><link>https://devzone.nordicsemi.com/thread/348349?ContentTypeID=1</link><pubDate>Tue, 18 Jan 2022 15:42:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:10b5e6ef-6bf5-4283-95ad-2d9f427b9879</guid><dc:creator>esscriber</dc:creator><description>&lt;p&gt;Hi Einarh,&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t know what the prescaler setting is. Do you know where I can find it in the sdk?&lt;br /&gt;If it is the RTC that overflows, shouldn&amp;#39;t the uptime also be affected (decrease)?&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;esscriber&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: gettimeofday returns incorrect timestamp after ~7 days</title><link>https://devzone.nordicsemi.com/thread/348348?ContentTypeID=1</link><pubDate>Tue, 18 Jan 2022 15:37:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4caba5d8-7881-4c8d-a158-d7ae4f31ae04</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;Hello!&lt;/p&gt;
&lt;p&gt;Sounds like this could be the RTC overflowing, which happens regularly at an interval determined by the RTC prescaler:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://infocenter.nordicsemi.com/topic/ps_nrf9160/rtc.html"&gt;https://infocenter.nordicsemi.com/topic/ps_nrf9160/rtc.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Do you know what the prescaler is set to in your application? Does it match the interval you&amp;#39;ve observed?&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: gettimeofday returns incorrect timestamp after ~7 days</title><link>https://devzone.nordicsemi.com/thread/348347?ContentTypeID=1</link><pubDate>Tue, 18 Jan 2022 15:37:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3317c66f-5000-46d8-81b4-5a63a4d3781c</guid><dc:creator>esscriber</dc:creator><description>&lt;p&gt;I have been looking into the differences between the gettimeofday and k_uptime_get_32 and I have found the following call sequences:&lt;/p&gt;
&lt;p&gt;gettimeofday() =&amp;gt; clock_gettime () =&amp;gt; k_ticks_to_ns_floor64(k_uptime_ticks())&lt;/p&gt;
&lt;p&gt;k_uptime_get_32 () =&amp;gt; (uint32_t)k_uptime_get() =&amp;gt; k_ticks_to_ms_floor64(k_uptime_ticks())&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So it could be an overflow in the k_ticks_to_ns_floor64 call (from clock_gettime)?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>