diff options
author | Kristaps Dzonsons <kristaps@bsd.lv> | 2009-11-02 06:22:44 +0000 |
---|---|---|
committer | Kristaps Dzonsons <kristaps@bsd.lv> | 2009-11-02 06:22:44 +0000 |
commit | a39b97ef0c1432973b5d815709925a82507c100b (patch) | |
tree | 9ca38e01caeb3a842bd0520375f0e52e83099515 /mandoc.c | |
parent | f50364828978249af6c0a2c5e92ee923b8d86c75 (diff) | |
download | mandoc-a39b97ef0c1432973b5d815709925a82507c100b.tar.gz mandoc-a39b97ef0c1432973b5d815709925a82507c100b.tar.zst mandoc-a39b97ef0c1432973b5d815709925a82507c100b.zip |
Added mandoc_a2time() for proper date conversion.
Fitted TH and Dd handlers to use mandoc_a2time().
Documented date syntax for -man, fixed documentation for -mdoc.
Diffstat (limited to 'mandoc.c')
-rw-r--r-- | mandoc.c | 64 |
1 files changed, 63 insertions, 1 deletions
@@ -1,4 +1,4 @@ -/* $Id: mandoc.c,v 1.6 2009/10/31 06:10:58 kristaps Exp $ */ +/* $Id: mandoc.c,v 1.7 2009/11/02 06:22:45 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -14,6 +14,10 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#if defined(__linux__) || defined(__MINT__) +# define _GNU_SOURCE /* strptime() */ +#endif + #include <sys/types.h> #include <assert.h> @@ -21,9 +25,13 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <time.h> #include "libmandoc.h" +static int a2time(time_t *, const char *, const char *); + + int mandoc_special(const char *p) { @@ -163,3 +171,57 @@ mandoc_strdup(const char *ptr) return(p); } + + +static int +a2time(time_t *t, const char *fmt, const char *p) +{ + struct tm tm; + char *pp; + + memset(&tm, 0, sizeof(struct tm)); + + pp = strptime(p, fmt, &tm); + if (NULL != pp && '\0' == *pp) { + *t = mktime(&tm); + return(1); + } + + return(0); +} + + +/* + * Convert from a manual date string (see mdoc(7) and man(7)) into a + * date according to the stipulated date type. + */ +time_t +mandoc_a2time(int flags, const char *p) +{ + time_t t; + + if (MTIME_MDOCDATE & flags) { + if (0 == strcmp(p, "$" "Mdocdate$")) + return(time(NULL)); + if (a2time(&t, "$" "Mdocdate: %b %d %Y $", p)) + return(t); + } + + if (MTIME_CANONICAL & flags || MTIME_REDUCED & flags) + if (a2time(&t, "%b %d, %Y", p)) + return(t); + + if (MTIME_ISO_8601 & flags) + if (a2time(&t, "%Y-%m-%d", p)) + return(t); + + if (MTIME_REDUCED & flags) { + if (a2time(&t, "%d, %Y", p)) + return(t); + if (a2time(&t, "%Y", p)) + return(t); + } + + return(0); +} + |