tftelkamp / single_chan_pkt_fwd

Single Channel LoRaWAN Gateway
Other
235 stars 308 forks source link

Unix timestamp generation in receivepacket() function #36

Open BenjiAmi opened 1 week ago

BenjiAmi commented 1 week ago

When I print the gmt time it gives the correct time (in my case: 2024-09-23 12:42:58 GMT) but this unix time tamp generation method gives "3129255889" which clearly isn't correct. The issue is due to the use of a 32-bit unsigned integer (uint32_t) to store the Unix timestamp, which is expressed in microseconds. Given that Unix time represents the number of seconds since January 1, 1970, combining it with microseconds and storing it in a 32-bit integer is causing an overflow, as 32 bits can't handle such large values when combining both seconds and microseconds. To resolve this issue, a 64-bit integer (uint64_t) for the timestamp must be used to avoid overflow and store the complete value correctly. unit64_t tmst = (uint64_t)(now.tv_sec) * 1000000 + (uint64_t)(now.tv_usec); j = snprintf((char *)(buff_up + buff_index), TX_BUFF_SIZE - buff_index, "\"tmst\":%llu", tmst); // %llu for uint64_t

BenjiAmi commented 6 days ago

Alternatively, If you could simply go with this: uint32_t tmst= (uint32_t)(now.tv_sec);