char-cat char-upper char-lower unicode stkstring \
wildmatch regex \
prime primetable random \
- time-stamp time-timer \
+ time-stamp time-timer time-conf \
bit-ffs bit-fls \
url \
mainloop main-block main-rec \
fb-socket.test trie-test.test string.test sha1.test asort-test.test binheap-test.test \
redblack-test.test fb-file.test fb-grow.test fb-pool.test fb-atomic.test \
fb-limfd.test fb-temp.test fb-mem.test fb-buffer.test fb-mmap.test url.test strtonum-test.test \
- gary.test)
+ gary.test time.test)
$(o)/ucw/regex.test: $(o)/ucw/regex-t
$(o)/ucw/unicode.test: $(o)/ucw/unicode-t
limfd.test temp.test mem.test buffer.test mmap.test): %.test: %-t
$(o)/ucw/url.test: $(o)/ucw/url-t
$(o)/ucw/gary.test: $(o)/ucw/gary-t
+$(o)/ucw/time.test: $(o)/ucw/time-conf-t
ifdef CONFIG_UCW_THREADS
TESTS+=$(addprefix $(o)/ucw/,asio.test)
--- /dev/null
+/*
+ * UCW Library -- A Parser of Time Intervals
+ *
+ * (c) 2012 Martin Mares <mj@ucw.cz>
+ *
+ * This software may be freely distributed and used according to the terms
+ * of the GNU Lesser General Public License.
+ */
+
+#include <ucw/lib.h>
+#include <ucw/conf.h>
+#include <ucw/time.h>
+#include <ucw/fastbuf.h>
+
+static char *
+timestamp_parser(char *c, void *ptr)
+{
+ timestamp_t *ts = ptr;
+ double d;
+
+ char *err = cf_parse_double(c, &d);
+ if (err)
+ return err;
+ if (d > 1e16)
+ return "Time too large";
+
+ *ts = (timestamp_t)(d*1000 + 0.5);
+ if (!*ts && d)
+ return "Non-zero time rounds down to zero milliseconds";
+
+ return NULL;
+}
+
+static void
+timestamp_dumper(struct fastbuf *fb, void *ptr)
+{
+ timestamp_t *ts = ptr;
+ bprintf(fb, "%jd", (intmax_t) *ts);
+}
+
+struct cf_user_type timestamp_type = {
+ .size = sizeof(timestamp_t),
+ .name = "timestamp",
+ .parser = timestamp_parser,
+ .dumper = timestamp_dumper
+};
+
+#ifdef TEST
+
+#include <stdio.h>
+
+int main(void)
+{
+ char line[256];
+
+ while (fgets(line, sizeof(line), stdin))
+ {
+ char *nl = strchr(line, '\n');
+ if (nl)
+ *nl = 0;
+ timestamp_t t = -1;
+ char *err = timestamp_parser(line, &t);
+ if (err)
+ puts(err);
+ else
+ printf("%jd\n", (intmax_t) t);
+ }
+ return 0;
+}
+
+#endif
timestamp_t get_timestamp(void); /** Get current time as a millisecond timestamp. **/
+/* time-conf.c */
+
+/**
+ * A user type for parsing of time intervals in configuration files.
+ * It is specified as fractional seconds and internally converted to
+ * a <<basics:type_timestamp_t,`timestamp_t`>>. When conversion of
+ * a non-zero value yields zero, an error is raised.
+ **/
+extern struct cf_user_type timestamp_type;
+
/***
* === Timers
*
--- /dev/null
+# Tests for time-conf
+
+Name: Parser normal
+Run: ../obj/ucw/time-conf-t 'ftp://example.com/other'
+In: 123
+ 0.23
+ 1e2
+ 3k
+ 1d
+Out: 123000
+ 230
+ 100000
+ 3000000
+ 86400000
+
+Name: Parser underflow
+Run: ../obj/ucw/time-conf-t 'ftp://example.com/other'
+In: 0.0001
+Out: Non-zero time rounds down to zero milliseconds
+
+Name: Parser overflow
+Run: ../obj/ucw/time-conf-t 'ftp://example.com/other'
+In: 1e30
+Out: Time too large