summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2014-05-15 01:18:52 +0100
committerVincent Sanders <vince@kyllikki.org>2014-05-15 01:18:52 +0100
commitef00272e2ffb8ffcf03e740f74691fecb682060d (patch)
treed1ba29cf118a7f4c905762813d3985131fbacd34 /utils
parenta4be7f7d1abe5b75d3dacfbf00adf3bd937bc4c1 (diff)
downloadnetsurf-ef00272e2ffb8ffcf03e740f74691fecb682060d.tar.gz
netsurf-ef00272e2ffb8ffcf03e740f74691fecb682060d.tar.bz2
add helpers for time_t reading/writing
Diffstat (limited to 'utils')
-rw-r--r--utils/config.h10
-rw-r--r--utils/time.h53
-rw-r--r--utils/utils.c63
3 files changed, 105 insertions, 21 deletions
diff --git a/utils/config.h b/utils/config.h
index cc4f75191..c0a3c0d1b 100644
--- a/utils/config.h
+++ b/utils/config.h
@@ -38,13 +38,15 @@ char *strndup(const char *s, size_t n);
char *strcasestr(const char *haystack, const char *needle);
#endif
-#if (defined(riscos) || defined(_WIN32))
+/* Although these platforms might have strftime or strptime they
+ * appear not to support the time_t seconds format specifier.
+ */
+#if (defined(_WIN32) || defined(riscos) || defined(__HAIKU__) || defined(__BEOS__) || defined(__amigaos4__) || defined(__AMIGA__) || defined(__MINT__))
#undef HAVE_STRPTIME
-#define strptime nsc_time_strptime
-struct tm;
-char *nsc_time_strptime(const char *s, const char *format, struct tm *tm);
+#undef HAVE_STRFTIME
#else
#define HAVE_STRPTIME
+#define HAVE_STRFTIME
#endif
/* For some reason, UnixLib defines this unconditionally.
diff --git a/utils/time.h b/utils/time.h
new file mode 100644
index 000000000..bde2ff66a
--- /dev/null
+++ b/utils/time.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2014 Vincent Sanders <vince@netsurf-browser.org>
+ *
+ * This file is part of NetSurf, http://www.netsurf-browser.org/
+ *
+ * NetSurf is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * NetSurf is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * \file utils/time.h
+ * \brief Interface to time operations.
+ */
+
+#ifndef _NETSURF_UTILS_TIME_H_
+#define _NETSURF_UTILS_TIME_H_
+
+#include <time.h>
+
+/**
+ * Write the time in seconds since epoch to a buffer.
+ *
+ * This is provided as strftime is not generally portable.
+ *
+ * @param str The destination buffer.
+ * @param size The length of the destination buffer.
+ * @param timep The pointer to the time to write.
+ * @return The length of the string written.
+ */
+int nsc_sntimet(char *str, size_t size, time_t *timep);
+
+/**
+ * Parse time in seconds since epoc.
+ *
+ * This is provided as strptime is not generally portable.
+ *
+ * @param str The source buffer.
+ * @param size The length of the source buffer.
+ * @param timep Pointer to result.
+ * @return NSERROR_OK on success or error code on faliure.
+ */
+nserror nsc_snptimet(char *str, size_t size, time_t *timep);
+
+#endif
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", &ltm) == NULL) {
+ return NSERROR_BAD_PARAMETER;
+ }
+
+ time_out = mktime(&ltm);
#endif
+ *timep = time_out;
+
+ return NSERROR_OK;
+}