This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Get the lib time in the thingy 91

Hello,

I'm coming to you with a beginner question, I would like to use the functions localtime, difftime, strftime but I saw that time.h is not included. Is it possible to get it or to do the same thing as these functions? I tried including NEWLIB but there is no time lib.

In my project I get a timestamp and I would like to have the difference of time elapsed between this timestamp and the timestamp of now.

Thanks in advance for your help!

Parents
  • Hello,

    I'm coming to you with a beginner question

    No problem at all, we are happy to help with any question you might have! Slight smile

    I would like to use the functions localtime, difftime, strftime but I saw that time.h is not included. Is it possible to get it or to do the same thing as these functions? I tried including NEWLIB but there is no time lib.

    In my project I get a timestamp and I would like to have the difference of time elapsed between this timestamp and the timestamp of now.

    When you say that you need a timestamp, do you mean a timestamp from the network, or a RTC counting locally on your device? As in, do you need the timestamp to be the actual current time, which is used in the network?
    Or, do you just need to see how long time has elapsed since the previous timestamp, with no regards to what the time actually is in the network?
    In the first case, where you need the network time, please see the answer by my colleague in this ticket.
    In the latter case, where you just need to see how long time has passed since last, you can use the nRF9160's RTC peripheral directly. 

    Please do not hesitate to let me know if any part of my answer is unclear, or if you should have any other issues or questions!

    Best regards,
    Karl

  • Hello !

    Thank you for your answer, I understand better! I would like to recover the timestamp network which must be the gmtime, I use this command:

    char buf_time[MODEM_INFO_JSON_STRING_SIZE];
    modem_info_string_get(MODEM_INFO_DATE_TIME, buf_time, MODEM_INFO_JSON_STRING_SIZE);
    printk("%s\n",buf_time);
    double diff_t = atoi(payload_buf) - atoi(buf_time);

    However, I can't read the result. There would be something that I missed? thank you in advance for your time ;) !

  • ActiTAG said:
    I hope you're fine.

    Thank you, I hope you are as well!

    ActiTAG said:
    Yes but my problem is that I need to set it. In my case, I would like to have the network time to know when the thingy is running and set it automatically, to after using a timer to continue to have the time up to date. 

    Yes, but if I understand you correctly, you shouldnt have to query the network for its time every second (as indicated by the CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS config - but instead query it more seldom, and keep track of it locally using a timer.
    Does this make sense? Then you would just have to query for the network time every so often, to negate the timer's drift over time.

    ActiTAG said:
    I'll have a look with datetime lib by increasing it

    My suggestion was to try increasing the CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS config, if you started to receive your error after setting it to 1, in order to pinpoint that this configuration was indeed the issue.

    ActiTAG said:
    I'll keep you informed. :)

    Alright, sounds good to me! :) 

    Best regards,
    Karl

  • Humm I set the value to 60 and 1800 but the date time get is 00000179fae6436e / 00000179fb00561a (Mon Jul 27 1970 19:05:45 GMT+0000) ...

  • Hello,

    Thank you for your patience. 

    ActiTAG said:
    Humm I set the value to 60 and 1800

    Are you referring to the CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS value?
    It only configures how often the date time is updated, in seconds, so it does not affect the current date time value. 

    ActiTAG said:
    00000179fae6436e / 00000179fb00561a (Mon Jul 27 1970 19:05:45 GMT+0000) ...

    Is this immediately upon starting your device, or is it after having received the network time and date? How did you print out and convert these values to the Jul 27 1970 date?

    Best regards,
    Karl

  • Hello, thank you for your answer! 


    It only configures how often the date time is updated, in seconds, so it does not affect the current date time value. 

    Yes, I know but even if I'm waiting for the update of the date_time (so 60 and 1800s), I received those printed values (00000179fae6436e / 00000179fb00561a). I used a site to convert it to a readable value ( I don't want to convert it in my code, it's not necessary). 

    From what I see, I have never received the network time and date. Maybe I forget to add something? I made the same as this topic :nRF9160 date-time example - Nordic Q&A - Nordic DevZone - Nordic DevZone (nordicsemi.com)

    If you have an idea I would be glad to hear it! 

    Kind Regards,

  • Hello,

    ActiTAG said:
    thank you for your answer! 

    No problem at all, I am happy to help!

    ActiTAG said:

    Yes, I know but even if I'm waiting for the update of the date_time (so 60 and 1800s), I received those printed values (00000179fae6436e / 00000179fb00561a). I used a site to convert it to a readable value ( I don't want to convert it in my code, it's not necessary). 

    From what I see, I have never received the network time and date. Maybe I forget to add something? I made the same as this topic :nRF9160 date-time example - Nordic Q&A - Nordic DevZone - Nordic DevZone (nordicsemi.com)

    Are you checking the return value of date_time_now? Is it successful?
    The function will return an error if it does not have a valid timestamp, or if it fails for another reason.
    Please also make a call to date_time_is_valid before starting your other date-time operations, to check that the library has successfully received a valid initial timestamp.

    Best regards,
    Karl

Reply
  • Hello,

    ActiTAG said:
    thank you for your answer! 

    No problem at all, I am happy to help!

    ActiTAG said:

    Yes, I know but even if I'm waiting for the update of the date_time (so 60 and 1800s), I received those printed values (00000179fae6436e / 00000179fb00561a). I used a site to convert it to a readable value ( I don't want to convert it in my code, it's not necessary). 

    From what I see, I have never received the network time and date. Maybe I forget to add something? I made the same as this topic :nRF9160 date-time example - Nordic Q&A - Nordic DevZone - Nordic DevZone (nordicsemi.com)

    Are you checking the return value of date_time_now? Is it successful?
    The function will return an error if it does not have a valid timestamp, or if it fails for another reason.
    Please also make a call to date_time_is_valid before starting your other date-time operations, to check that the library has successfully received a valid initial timestamp.

    Best regards,
    Karl

Children
Related