How to update Timestamp using the time.h library in NRF52840

Hello guys,

I'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't get updated. it keeps printing the same time.

Now I tried using a static incrementing counter to update the ".tm_secs" struct member, Now the time updates over as the counter increment. but the minute and hour field soesn't get updated when ".tm_secs" gets to 59.

I don't know if this is the best approach, but I just want to produce a time epoch, I can always set the epoch with the current time over my GSM modem.

Parents
  • Hi 

    I recently did something similar in a hobby project of mine, where I needed to keep track of time. 

    I set the time using the clock_settime(..) function, like this:

    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(&start_time);
    
    ts.tv_sec = t;
    ts.tv_nsec = 0;
    
    clock_settime(CLOCK_REALTIME, &ts);
     

    Then I can simply read out the time like this:

    // Check current time 
    t = time(NULL);
    ptr = localtime(&t);

    You can find the source here.

    Best regards
    Torbjørn 

  • While I have solved the underlying issue, I will try this out and compare what I have with yours. Thank you, sir!

    Edit: I have tried replicating this code but I ran into this error:

    main.c:67: undefined reference to `clock_settime'
    collect2.exe: error: ld returned 1 exit status
    ninja: build stopped: subcommand failed.
    FATAL ERROR: command exited with status 1: 'C:\ncs\toolchains\31f4403e35\opt\bin\cmake.EXE' --build 'c:\nordic\myapps\firmware\app\build' 

    I have included these libraries:

    #include <time.h>
    /* clock_gettime() prototype */
    #include <zephyr/posix/time.h>
    but the error keeps occurring.
Reply
  • While I have solved the underlying issue, I will try this out and compare what I have with yours. Thank you, sir!

    Edit: I have tried replicating this code but I ran into this error:

    main.c:67: undefined reference to `clock_settime'
    collect2.exe: error: ld returned 1 exit status
    ninja: build stopped: subcommand failed.
    FATAL ERROR: command exited with status 1: 'C:\ncs\toolchains\31f4403e35\opt\bin\cmake.EXE' --build 'c:\nordic\myapps\firmware\app\build' 

    I have included these libraries:

    #include <time.h>
    /* clock_gettime() prototype */
    #include <zephyr/posix/time.h>
    but the error keeps occurring.
Children
Related