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 ;) !

  • Hello,

    ActiTAG said:
    Yes, I have checked that. I ran the at_client sample and it works if I set the clock with AT+CCLCK="xxxx".

    Great, I am glad to hear that this works, at least. 

    ActiTAG said:
    I'm using a prepaid SIM card 1NCE, I'm connecting to de DeutschTelekom network but I don't find any information to know if they provide network time.

    To officially confirm whether they do provide this or not you would unfortunately have to contact and inquire them directly, but since the at_client is able to get the network time it is safe to say that they do, so this should not be the issue here.

    ActiTAG said:
    I've never had a problem before but I noticed it when I added this line
    ActiTAG said:
    But I have no idea how to fix the problem. Maybe you have already encountered this problem?

    Is your intention for this modification to have the modem query the network time every second?
    The default value for this is 3600, so setting it to 1 might be too low - i.e you might be inquiring about this too often, and thus fail.
    Could you try to set this a little higher, and see if you still see this issue?
    What accuracy do you need for your time synchronization? The onboard timers should provide enough accuracy so that you do not have to query the network so often, to know the network time.

    ActiTAG said:
    Thank you for your help and suggestions on how to get what I am looking for. 

    No problem, I am happy to help!

    Best regards,
    Karl

  • Hello,

    I hope you're fine.

    ActiTAG said:
    Yes, I have checked that. I ran the at_client sample and it works if I set the clock with AT+CCLCK="xxxx".

    Great, I am glad to hear that this works, at least. 

    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. 

    I'll have a look with datetime lib by increasing it, I'll keep you informed. :)

  • 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

Reply
  • 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

Children
Related