summaryrefslogtreecommitdiffstats
path: root/src/libnetdata/datetime/rfc7231.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-05 11:19:16 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-07-24 09:53:24 +0000
commitb5f8ee61a7f7e9bd291dd26b0585d03eb686c941 (patch)
treed4d31289c39fc00da064a825df13a0b98ce95b10 /src/libnetdata/datetime/rfc7231.c
parentAdding upstream version 1.44.3. (diff)
downloadnetdata-b5f8ee61a7f7e9bd291dd26b0585d03eb686c941.tar.xz
netdata-b5f8ee61a7f7e9bd291dd26b0585d03eb686c941.zip
Adding upstream version 1.46.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/libnetdata/datetime/rfc7231.c')
-rw-r--r--src/libnetdata/datetime/rfc7231.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/libnetdata/datetime/rfc7231.c b/src/libnetdata/datetime/rfc7231.c
new file mode 100644
index 000000000..4925ed2c9
--- /dev/null
+++ b/src/libnetdata/datetime/rfc7231.c
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#include "../libnetdata.h"
+
+inline size_t rfc7231_datetime(char *buffer, size_t len, time_t now_t) {
+ if (unlikely(!buffer || !len))
+ return 0;
+
+ struct tm *tmp, tmbuf;
+
+ // Use gmtime_r for UTC time conversion.
+ tmp = gmtime_r(&now_t, &tmbuf);
+
+ if (unlikely(!tmp)) {
+ buffer[0] = '\0';
+ return 0;
+ }
+
+ // Format the date and time according to the RFC 7231 format.
+ size_t ret = strftime(buffer, len, "%a, %d %b %Y %H:%M:%S GMT", tmp);
+ if (unlikely(ret == 0))
+ buffer[0] = '\0';
+
+ return ret;
+}
+
+size_t rfc7231_datetime_ut(char *buffer, size_t len, usec_t now_ut) {
+ return rfc7231_datetime(buffer, len, (time_t) (now_ut / USEC_PER_SEC));
+}