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 | d612b79e06155082b6342af68940fa671568998d (patch) | |
tree | c823b0d3420b6f66b76bf2249667aab2ed2ca0c2 /mandoc.c | |
parent | 768ae2f18e90273f3a363fe0374c8dc61ecff26b (diff) | |
download | mandoc-d612b79e06155082b6342af68940fa671568998d.tar.gz |
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 | 62 |
1 files changed, 62 insertions, 0 deletions
@@ -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); +} + |