From ef00272e2ffb8ffcf03e740f74691fecb682060d Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Thu, 15 May 2014 01:18:52 +0100 Subject: add helpers for time_t reading/writing --- utils/utils.c | 63 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 17 deletions(-) (limited to 'utils/utils.c') diff --git a/utils/utils.c b/utils/utils.c index 953cc5cad..6199a8451 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -35,6 +35,7 @@ #include "utils/config.h" #include "utils/messages.h" #include "utils/utf8.h" +#include "utils/time.h" #include "utils/utils.h" /* exported interface documented in utils/utils.h */ @@ -566,29 +567,57 @@ int inet_pton(int af, const char *src, void *dst) #endif -#ifndef HAVE_STRPTIME +/* exported function documented in utils/time.h */ +int nsc_sntimet(char *str, size_t size, time_t *timep) +{ +#ifndef HAVE_STRFTIME + long long val; + val = (long long)*timep; -/** - * naff strptime implementation for risc os and windows. - * - * @warning only supports %s format - */ -char *nsc_time_strptime(const char *s, const char *format, struct tm *tm) + return snprintf(str, size, "%lld", val); +#else + struct tm *ltm; + + ltm = localtime(timep); + if (ltm == NULL) { + return -1; + } + + return strftime(str, size, "%s", ltm); +#endif +} + +nserror nsc_snptimet(char *str, size_t size, time_t *timep) { - time_t esecs; - struct tm *gtm; - char *endptr; + time_t time_out; - if ((format[0] != '%') || (format[1] != 's')) { - return NULL; +#ifndef HAVE_STRPTIME + + if (size < 1) { + return NSERROR_BAD_PARAMETER; } - esecs = (time_t)strtoll(s, &endptr, 10); + time_out = (time_t)strtoll(str, NULL, 10); - gtm = gmtime(esecs); - *tm = *gtm; + if (time_out == 0) { + return NSERROR_BAD_PARAMETER; + } - return endptr; -} +#else + struct tm ltm; + + if (size < 1) { + return NSERROR_BAD_PARAMETER; + } + + if (strptime(str, "%s", <m) == NULL) { + return NSERROR_BAD_PARAMETER; + } + + time_out = mktime(<m); #endif + *timep = time_out; + + return NSERROR_OK; +} -- cgit v1.2.3