Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unix timestamp generation in receivepacket() function #36

Open
BenjiAmi opened this issue Sep 23, 2024 · 1 comment
Open

Unix timestamp generation in receivepacket() function #36

BenjiAmi opened this issue Sep 23, 2024 · 1 comment

Comments

@BenjiAmi
Copy link

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant