mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-18 20:57:56 +00:00
Add strbuf_append_strftime()
This commit is contained in:
parent
65f6e88e67
commit
c6666387e8
@ -21,6 +21,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
#include <poll.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/wait.h>
|
||||
#ifdef HAVE_NETINET_IN_H
|
||||
@ -307,3 +309,28 @@ strbuf strbuf_append_sockaddr(strbuf sb, const struct sockaddr *addr)
|
||||
}
|
||||
return sb;
|
||||
}
|
||||
|
||||
strbuf strbuf_append_strftime(strbuf sb, const char *format, const struct tm *tm)
|
||||
{
|
||||
// First, try calling strftime(3) directly on the buffer in the strbuf, if there is one and it
|
||||
// looks to be long enough.
|
||||
const size_t need = strlen(format); // heuristic, could be better
|
||||
if (strbuf_str(sb)) {
|
||||
size_t avail = strbuf_remaining(sb);
|
||||
if (avail > need) {
|
||||
size_t n = strftime(strbuf_end(sb), avail + 1, format, tm);
|
||||
if (n) {
|
||||
assert(n <= avail);
|
||||
sb->current += n;
|
||||
return sb;
|
||||
}
|
||||
}
|
||||
}
|
||||
// If that didn't work, then call strftime(3) on a temporary buffer and concatenate the result
|
||||
// into the strbuf.
|
||||
const size_t len = 500; // should be enough
|
||||
char *buf = alloca(len + 1);
|
||||
size_t n = strftime(buf, len + 1, format, tm);
|
||||
strbuf_ncat(sb, buf, n);
|
||||
return sb;
|
||||
}
|
||||
|
@ -100,7 +100,12 @@ strbuf strbuf_append_exit_status(strbuf sb, int status);
|
||||
*/
|
||||
struct sockaddr;
|
||||
strbuf strbuf_append_sockaddr(strbuf sb, const struct sockaddr *);
|
||||
|
||||
#define alloca_sockaddr(addr) strbuf_str(strbuf_append_sockaddr(strbuf_alloca(40), (const struct sockaddr *)(addr)))
|
||||
|
||||
/* Append a strftime(3) string.
|
||||
* @author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
struct tm;
|
||||
strbuf strbuf_append_strftime(strbuf sb, const char *format, const struct tm *tm);
|
||||
|
||||
#endif //__STRBUF_HELPERS_H__
|
||||
|
Loading…
Reference in New Issue
Block a user