Skip to content

Commit

Permalink
utils: Add a method to get ISO 8601 formated string
Browse files Browse the repository at this point in the history
Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Feb 16, 2024
1 parent 409a258 commit 3bd228b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ int64_t millis_now();
*/
void get_time(uint32_t *seconds, uint32_t *micro_seconds);

/**
* @brief Addd an ISO 8601 datetime string to `buf` for upto `size` bytes.
*
* Note: The format is YYYY-MM-DDThh:mm:ssZ, so size must alteast be 21.
*/
int add_iso8601_utc_datetime(char *buf, size_t size);

/**
* @brief Get time elapsed in milli seconds since `last`. Used along with
* millis_now().
Expand Down
10 changes: 10 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stddef.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>

#include <utils/utils.h>
Expand Down Expand Up @@ -106,6 +107,15 @@ void get_time(uint32_t *seconds, uint32_t *micro_seconds)
*micro_seconds = tv.tv_usec;
}

int add_iso8601_utc_datetime(char *buf, size_t size)
{
time_t now;

// Format: YYYY-MM-DDThh:mm:ssZ
time(&now);
return strftime(buf, size, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
}

int64_t usec_since(int64_t last)
{
return usec_now() - last;
Expand Down

0 comments on commit 3bd228b

Please sign in to comment.