<?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>How to update Timestamp using the time.h library in NRF52840</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/105001/how-to-update-timestamp-using-the-time-h-library-in-nrf52840</link><description>Hello guys, 
 
 I&amp;#39;m stuck trying to produce a timestamp for data synchronization in my project. I have gone through some of the issues raised in this regard but none was actually helpful. 
 Currently I am using the time.h library, but the time doesn&amp;#39;t</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 26 Oct 2023 07:41:50 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/105001/how-to-update-timestamp-using-the-time-h-library-in-nrf52840" /><item><title>RE: How to update Timestamp using the time.h library in NRF52840</title><link>https://devzone.nordicsemi.com/thread/452429?ContentTypeID=1</link><pubDate>Thu, 26 Oct 2023 07:41:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:149a01b3-d443-4d6d-8660-a8991c460787</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Glad I could help. The best of luck with your project &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to update Timestamp using the time.h library in NRF52840</title><link>https://devzone.nordicsemi.com/thread/452399?ContentTypeID=1</link><pubDate>Thu, 26 Oct 2023 03:21:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:07292ca2-8602-4bda-9a94-554e05fd460b</guid><dc:creator>Techist</dc:creator><description>&lt;p&gt;I have fixed the error, i included &amp;quot;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;CONFIG_POSIX_CLOCK&lt;/span&gt;&lt;span&gt;=y&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;quot;&lt;/p&gt;
&lt;p&gt;in my prj.conf file.&lt;/p&gt;
&lt;p&gt;Thanks!!!!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to update Timestamp using the time.h library in NRF52840</title><link>https://devzone.nordicsemi.com/thread/452384?ContentTypeID=1</link><pubDate>Thu, 26 Oct 2023 00:14:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ce1ff1f5-97ce-48d6-b800-ea27ff5fe243</guid><dc:creator>Techist</dc:creator><description>&lt;p&gt;While I have solved the underlying issue, I will try this out and compare what I have with yours. Thank you, sir!&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Edit: I have tried replicating this code but I ran into this error:&lt;/p&gt;
&lt;p&gt;main.c:67: undefined reference to `clock_settime&amp;#39;&lt;br /&gt;collect2.exe: error: ld returned 1 exit status&lt;br /&gt;ninja: build stopped: subcommand failed.&lt;br /&gt;FATAL ERROR: command exited with status 1: &amp;#39;C:\ncs\toolchains\31f4403e35\opt\bin\cmake.EXE&amp;#39; --build &amp;#39;c:\nordic\myapps\firmware\app\build&amp;#39;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I have included these libraries:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;#include&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;&amp;lt;time.h&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;/* clock_gettime() prototype */&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;#include&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;&amp;lt;zephyr/posix/time.h&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;but the error keeps occurring.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to update Timestamp using the time.h library in NRF52840</title><link>https://devzone.nordicsemi.com/thread/452383?ContentTypeID=1</link><pubDate>Thu, 26 Oct 2023 00:11:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4e93bf61-9408-4fbe-8f84-174663a45dda</guid><dc:creator>Techist</dc:creator><description>&lt;p&gt;This was very helpful James!! I have been able to implement the timestamp algorithm. The attached links were indeed helpful, Thank you so much.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to update Timestamp using the time.h library in NRF52840</title><link>https://devzone.nordicsemi.com/thread/452205?ContentTypeID=1</link><pubDate>Wed, 25 Oct 2023 09:32:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:20a6e949-c659-4109-9755-c6d7522894ae</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I recently did something similar in a hobby project of mine, where I needed to keep track of time.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I set the time using the clock_settime(..) function, like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;struct tm start_time;
struct timespec ts;
time_t t;

start_time.tm_year = 122;
start_time.tm_mon = 0;
start_time.tm_mday = 24;
start_time.tm_hour = 8;
start_time.tm_min = 12;
start_time.tm_sec = 12;
t = mktime(&amp;amp;start_time);

ts.tv_sec = t;
ts.tv_nsec = 0;

clock_settime(CLOCK_REALTIME, &amp;amp;ts);&lt;/pre&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Then I can simply read out the time like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// Check current time 
t = time(NULL);
ptr = localtime(&amp;amp;t);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;You can find the source &lt;a href="https://github.com/ovrebekk/ncs-nrf5-cam-tl-controller/blob/master/src/main.c#L393-L410"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to update Timestamp using the time.h library in NRF52840</title><link>https://devzone.nordicsemi.com/thread/451996?ContentTypeID=1</link><pubDate>Tue, 24 Oct 2023 13:26:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a7f23b22-4bb8-4d5f-bf55-6a6e0bab508c</guid><dc:creator>JamesDougherty</dc:creator><description>&lt;p&gt;Not sure how helpful this will be, but I just store the epoch as an integer (uint32 to be exact). This will provide a valid timestamp until February 2106 (if my code is still around at that time, then the next person can fix it). Not sure how this relates to what you&amp;#39;re wanting to do with the modem, but I don&amp;#39;t see why it wouldn&amp;#39;t work.&lt;/p&gt;
&lt;p&gt;As an example:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint32_t timestamp = 1672531200U; // 01.01.2023 12:00:00 AM&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Then to add a second you can simply:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;timestamp += 1U;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;To get the date and times you can use &lt;a href="https://howardhinnant.github.io/date_algorithms.html" rel="noopener noreferrer" target="_blank"&gt;these public domain functions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can see how it works easily&amp;nbsp;&lt;a href="https://www.epochconverter.com/" rel="noopener noreferrer" target="_blank"&gt;here&lt;/a&gt;, just put in the timestamp, convert it, add one to it, convert it, etc.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Just as an example of how I use it to convert the date &amp;amp; time (may be helpful to others as well).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Date and Time Structures&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;typedef struct CsDate {
    CsMonth_t month;
    uint8_t day;
    uint16_t year;
} CsDate_t;

typedef struct CsTime {
    uint8_t hours;
    uint8_t minutes;
    uint8_t seconds;
} CsTime_t;

typedef struct CsDateTime {
    CsDate_t date;
    CsTime_t time;
} CsDateTime_t;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Datetime from Timestamp&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You&amp;#39;re probably not worried about DST, but if you are let me know and I can show you that code as well. The other function are in the link above.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;CsDateTime_t datetime_from_timestamp(const uint64_t timestamp, const bool apply_dst)
{
    const uint16_t oneHour = 60U /* seconds */ * 60U /* minutes */;
    uint64_t epoch = timestamp;
    CsDate_t date = civil_from_days(epoch);

    CsDateTime_t datetime = {
        .date = {
            .month = date.month,
            .day = date.day,
            .year = date.year + 1900U
        },
        .time = {
            .hours = 0,
            .minutes = 0,
            .seconds = 0
        }
    };

    if (apply_dst &amp;amp;&amp;amp; dst_active(datetime))
    {
        epoch += oneHour;
        date = civil_from_days(epoch);
    }

    const uint32_t remainderHours = epoch - (epoch / 86400) * 86400;
    const uint32_t hours = remainderHours / 60U / 60U;
    const uint32_t remainderMinutes = remainderHours - hours * oneHour;
    const uint32_t minutes = remainderMinutes / 60U;

    datetime = (CsDateTime_t) {
        .date = {
            .month = date.month,
            .day = date.day,
            .year = date.year + 1900U
        },
        .time = {
            .hours = hours,
            .minutes = minutes,
            .seconds = remainderMinutes - minutes * 60
        }
    };

    return datetime;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Datetime to Timestamp&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is helpful if you want to go from the datetime back to the timestamp&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint32_t datetime_to_timestamp(const CsDateTime_t datetime)
{
    return (uint32_t)datetime_to_timestamp_64(datetime);
}

uint64_t datetime_to_timestamp_64(const CsDateTime_t datetime)
{
    return datetime.time.seconds +
            (60LL * (datetime.time.minutes + 60LL * datetime.time.hours)) +
            (86400LL * days_from_civil(datetime.date));
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>