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

How to convert date and time to timestamp?

Hi, I use IoT sdk with my iPhone to get current date and time, date comes in ble_cts_c_evt_t structure, how can I convert it to timestamp, I know that I have to subscribe Jan 1, 1970 from current date and multiple all by 84600, but maybe is there more trivial way?

  • You could use the mktime() function from the ANSI C library. See this example.

  • If I use mktime, I get incorrect data

  • This devzone case might be helpful. In addition, the current time application in the nrf sdk could be helpful. See the current_time_print() function in the main.c file for more info. The current_time_print() function also takes in a ble_cts_c_evt_t struct & converts the data to hours, minutes, seconds & fractions of a second.

  • My app based on this current time application, and I got the correct date and time, but when I put it in tm structure and called mktime, I got wrong value of timestamp. Usually one month forward, i.e. if today is 19.03.2018 10:56:34, then my timestamp (after converting back in online service) shows 19.04.2018 10:56:34 (or UTC0 7:56:34)

  • Is the time always one month forward? If so, it seems like there is some kind of offset that leads to an incorrect date.

1 2