diff --git a/include/utils/utils.h b/include/utils/utils.h index a97e525..354147f 100644 --- a/include/utils/utils.h +++ b/include/utils/utils.h @@ -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(). diff --git a/src/utils.c b/src/utils.c index c8f3a49..01a4ac6 100644 --- a/src/utils.c +++ b/src/utils.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -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;