From ae850b1ac755f471beac4dbfef4654fe3fbaaae9 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 9 Jan 2024 22:04:24 +0100 Subject: First commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libpatch_create_timestamp.c | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 libpatch_create_timestamp.c (limited to 'libpatch_create_timestamp.c') diff --git a/libpatch_create_timestamp.c b/libpatch_create_timestamp.c new file mode 100644 index 0000000..54b6d91 --- /dev/null +++ b/libpatch_create_timestamp.c @@ -0,0 +1,58 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +size_t +libpatch_create_timestamp(char *buf, size_t bufsize, const struct tm *tm, + uintmax_t subseconds, unsigned subseconds_decimals, + unsigned frac, int zone, enum libpatch_style style) +{ + size_t guess, len, r; + const char *fmt; + + frac = MIN(frac, subseconds_decimals); + + if (style == LIBPATCH_STYLE_COPIED) { + fmt = "%a %b %e %T %Y"; + guess = sizeof("aaa bbb 00 00:00:00 0000"); + } else if (style == LIBPATCH_STYLE_UNIFIED) { + fmt = "%Y-%m-%d %H:%M:%S"; + zone = 1; + guess = sizeof("0000-00-00 00:00:00"); + } else { + errno = EINVAL; + return 0; + } + + if (zone) + guess += sizeof(" +0000"); + guess += (size_t)frac + (size_t)(frac > 0); + + if (!bufsize) + return guess; + + while (subseconds_decimals != frac) { + subseconds /= 10; + subseconds_decimals -= 1; + } + + len = strftime(buf, bufsize, fmt, tm); + if (!len) + return bufsize < guess ? guess : bufsize + 16U; + + if (frac) { + if (len < bufsize) + snprintf(&buf[len], bufsize - len, ".%0*ju", (int)frac, subseconds); + len += (size_t)frac + (size_t)(frac > 0); + } + + if (zone) { + if (len < bufsize) + return bufsize < len + 7U ? len + 7U : bufsize + 16U; + len += r = strftime(&buf[len], bufsize - len, " %z", tm); + if (!r) + return bufsize < len + 7U ? len + 7U : bufsize + 16U; + } + + return len + 1; +} -- cgit v1.2.3-70-g09d2