Mercurial > dovecot > original-hg > dovecot-2.1
changeset 13923:22cfba724675
Added t_strflocaltime().
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Tue, 10 Jan 2012 22:51:52 +0200 |
parents | 1362d8bda03c |
children | d4c506d8f534 |
files | src/lib/macros.h src/lib/time-util.c src/lib/time-util.h |
diffstat | 3 files changed, 28 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib/macros.h Tue Jan 10 21:39:13 2012 +0200 +++ b/src/lib/macros.h Tue Jan 10 22:51:52 2012 +0200 @@ -93,6 +93,8 @@ __attribute__((format_arg (arg_idx))) # define ATTR_SCANF(format_idx, arg_idx) \ __attribute__((format (scanf, format_idx, arg_idx))) +# define ATTR_STRFTIME(format_idx) \ + __attribute__((format (strftime, format_idx, 0))) # define ATTR_UNUSED __attribute__((unused)) # define ATTR_NORETURN __attribute__((noreturn)) # define ATTR_CONST __attribute__((const)) @@ -100,7 +102,8 @@ #else # define ATTR_FORMAT(format_idx, arg_idx) # define ATTR_FORMAT_ARG(arg_idx) -# define ATTR_SCANF +# define ATTR_SCANF(format_idx, arg_idx) +# define ATTR_STRFTIME(format_idx) # define ATTR_UNUSED # define ATTR_NORETURN # define ATTR_CONST
--- a/src/lib/time-util.c Tue Jan 10 21:39:13 2012 +0200 +++ b/src/lib/time-util.c Tue Jan 10 22:51:52 2012 +0200 @@ -3,6 +3,10 @@ #include "lib.h" #include "time-util.h" +#include <time.h> + +#define STRFTIME_MAX_BUFSIZE (1024*64) + int timeval_cmp(const struct timeval *tv1, const struct timeval *tv2) { if (tv1->tv_sec < tv2->tv_sec) @@ -35,3 +39,20 @@ } return ((long long)secs * 1000000ULL) + usecs; } + +const char *t_strflocaltime(const char *fmt, time_t t) +{ + const struct tm *tm; + size_t bufsize = strlen(fmt) + 32; + char *buf = t_buffer_get(bufsize); + size_t ret; + + tm = localtime(&t); + while ((ret = strftime(buf, bufsize, fmt, tm)) == 0) { + bufsize *= 2; + i_assert(bufsize <= STRFTIME_MAX_BUFSIZE); + buf = t_buffer_get(bufsize); + } + t_buffer_alloc(ret + 1); + return buf; +}
--- a/src/lib/time-util.h Tue Jan 10 21:39:13 2012 +0200 +++ b/src/lib/time-util.h Tue Jan 10 22:51:52 2012 +0200 @@ -11,4 +11,7 @@ long long timeval_diff_usecs(const struct timeval *tv1, const struct timeval *tv2); +/* Wrapper to strftime() */ +const char *t_strflocaltime(const char *fmt, time_t t) ATTR_STRFTIME(1); + #endif