Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Comparing two ble_date_time_t 's

I need to compare two ble_date_time_t structs to figure out if one is later than the other. So basically I need to check if rtc_datetime > expiration_datetime.
Both rtc_datetime and expiration_datetime are of type ble_date_time_t. I searched through the SDK documentation for a function for comparing those two. I could not find one and now I am looking for the best way to compare. Is just looping through elements and comparing them one by one the best option?
Thanks a lot!

Parents
  • Hi 

    There is no easy utility function for comparing two ble_date_time_t structs unfortunately. 

    One way to compare them is to make a macro or function for converting a ble_date_time_t struct into a second value, similar to how the time.h library allows you to handle time either as a struct with years, months, days etc, or as a 32-bit value counting the seconds passed since Jan 1 1970.

    Once you convert time into a second counter you can compare two time stamps simply by checking which number is larger, and you don't have to manually check every field in the struct. 

    Best regards
    Torbjørn 

  • Hi Ovrebekk,

    Thanks for your reply, this indeed sounds like a better solution. I'll just need to figure out how to do this based on the time.h library. The question that remains: Why is there a ble_data_time_t at all? I used it everywhere in my code because I assumed it would be easier to use the date time struct from the SDK. Why doesn't the SDK just use time_t? Seems like it would save everyone from some converting :)

  • Hi 

    I expect the ble_date_time_t struct was designed to be compliant with the way that the Bluetooth specification defines datetime, in order to make it easy to pass the datetime information to the date time characteristic. 

    The way that time.h defines time is not byte for byte compatible with the way the Bluetooth specification defines it, so at some point you need a conversion if you want to use time.h. 

    You could make a utility function that populates the tm struct from time.h based on the fields in the ble_date_time_t struct, which is probably the quickest way to convert between the two. 

    Best regards
    Torbjørn

Reply
  • Hi 

    I expect the ble_date_time_t struct was designed to be compliant with the way that the Bluetooth specification defines datetime, in order to make it easy to pass the datetime information to the date time characteristic. 

    The way that time.h defines time is not byte for byte compatible with the way the Bluetooth specification defines it, so at some point you need a conversion if you want to use time.h. 

    You could make a utility function that populates the tm struct from time.h based on the fields in the ble_date_time_t struct, which is probably the quickest way to convert between the two. 

    Best regards
    Torbjørn

Children
No Data
Related