diff options
Diffstat (limited to 'bin/date/date.c')
-rw-r--r-- | bin/date/date.c | 167 |
1 files changed, 120 insertions, 47 deletions
diff --git a/bin/date/date.c b/bin/date/date.c index 4dc5df0dccfc..01797084c0d6 100644 --- a/bin/date/date.c +++ b/bin/date/date.c @@ -29,27 +29,13 @@ * SUCH DAMAGE. */ -#ifndef lint -static char const copyright[] = -"@(#) Copyright (c) 1985, 1987, 1988, 1993\n\ - The Regents of the University of California. All rights reserved.\n"; -#endif /* not lint */ - -#if 0 -#ifndef lint -static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95"; -#endif /* not lint */ -#endif - -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - #include <sys/param.h> #include <sys/time.h> #include <sys/stat.h> #include <ctype.h> #include <err.h> +#include <errno.h> #include <locale.h> #include <stdbool.h> #include <stdio.h> @@ -65,15 +51,15 @@ __FBSDID("$FreeBSD$"); #define TM_YEAR_BASE 1900 #endif -static time_t tval; - static void badformat(void); -static void iso8601_usage(const char *); +static void iso8601_usage(const char *) __dead2; static void multipleformats(void); static void printdate(const char *); -static void printisodate(struct tm *); -static void setthetime(const char *, const char *, int); -static void usage(void); +static void printisodate(struct tm *, long); +static void setthetime(const char *, const char *, int, struct timespec *); +static size_t strftime_ns(char * __restrict, size_t, const char * __restrict, + const struct tm * __restrict, long); +static void usage(void) __dead2; static const struct iso8601_fmt { const char *refname; @@ -83,6 +69,7 @@ static const struct iso8601_fmt { { "hours", "T%H" }, { "minutes", ":%M" }, { "seconds", ":%S" }, + { "ns", ",%N" }, }; static const struct iso8601_fmt *iso8601_selected; @@ -91,11 +78,12 @@ static const char *rfc2822_format = "%a, %d %b %Y %T %z"; int main(int argc, char *argv[]) { + struct timespec ts; int ch, rflag; bool Iflag, jflag, Rflag; const char *format; char buf[1024]; - char *fmt; + char *fmt, *outzone = NULL; char *tmp; struct vary *v; const struct vary *badv; @@ -108,7 +96,9 @@ main(int argc, char *argv[]) (void) setlocale(LC_TIME, ""); rflag = 0; Iflag = jflag = Rflag = 0; - while ((ch = getopt(argc, argv, "f:I::jnRr:uv:")) != -1) + ts.tv_sec = 0; + ts.tv_nsec = 0; + while ((ch = getopt(argc, argv, "f:I::jnRr:uv:z:")) != -1) switch((char)ch) { case 'f': fmt = optarg; @@ -141,17 +131,21 @@ main(int argc, char *argv[]) break; case 'r': /* user specified seconds */ rflag = 1; - tval = strtoq(optarg, &tmp, 0); + ts.tv_sec = strtoq(optarg, &tmp, 0); if (*tmp != 0) { - if (stat(optarg, &sb) == 0) - tval = sb.st_mtim.tv_sec; - else + if (stat(optarg, &sb) == 0) { + ts.tv_sec = sb.st_mtim.tv_sec; + ts.tv_nsec = sb.st_mtim.tv_nsec; + } else usage(); } break; case 'u': /* do everything in UTC */ (void)setenv("TZ", "UTC0", 1); break; + case 'z': + outzone = optarg; + break; case 'v': v = vary_append(v, optarg); break; @@ -161,8 +155,8 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - if (!rflag && time(&tval) == -1) - err(1, "time"); + if (!rflag && clock_gettime(CLOCK_REALTIME, &ts) == -1) + err(1, "clock_gettime"); format = "%+"; @@ -178,7 +172,7 @@ main(int argc, char *argv[]) } if (*argv) { - setthetime(fmt, *argv, jflag); + setthetime(fmt, *argv, jflag, &ts); ++argv; } else if (fmt != NULL) usage(); @@ -189,7 +183,9 @@ main(int argc, char *argv[]) format = *argv + 1; } - lt = localtime(&tval); + if (outzone != NULL && setenv("TZ", outzone, 1) != 0) + err(1, "setenv(TZ)"); + lt = localtime(&ts.tv_sec); if (lt == NULL) errx(1, "invalid time"); badv = vary_apply(v, lt); @@ -202,7 +198,7 @@ main(int argc, char *argv[]) vary_destroy(v); if (Iflag) - printisodate(lt); + printisodate(lt, ts.tv_nsec); if (format == rfc2822_format) /* @@ -211,7 +207,8 @@ main(int argc, char *argv[]) */ setlocale(LC_TIME, "C"); - (void)strftime(buf, sizeof(buf), format, lt); + + (void)strftime_ns(buf, sizeof(buf), format, lt, ts.tv_nsec); printdate(buf); } @@ -225,19 +222,19 @@ printdate(const char *buf) } static void -printisodate(struct tm *lt) +printisodate(struct tm *lt, long nsec) { const struct iso8601_fmt *it; - char fmtbuf[32], buf[32], tzbuf[8]; + char fmtbuf[64], buf[64], tzbuf[8]; fmtbuf[0] = 0; for (it = iso8601_fmts; it <= iso8601_selected; it++) strlcat(fmtbuf, it->format_string, sizeof(fmtbuf)); - (void)strftime(buf, sizeof(buf), fmtbuf, lt); + (void)strftime_ns(buf, sizeof(buf), fmtbuf, lt, nsec); if (iso8601_selected > iso8601_fmts) { - (void)strftime(tzbuf, sizeof(tzbuf), "%z", lt); + (void)strftime_ns(tzbuf, sizeof(tzbuf), "%z", lt, nsec); memmove(&tzbuf[4], &tzbuf[3], 3); tzbuf[3] = ':'; strlcat(buf, tzbuf, sizeof(buf)); @@ -249,15 +246,14 @@ printisodate(struct tm *lt) #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) static void -setthetime(const char *fmt, const char *p, int jflag) +setthetime(const char *fmt, const char *p, int jflag, struct timespec *ts) { struct utmpx utx; struct tm *lt; - struct timeval tv; const char *dot, *t; int century; - lt = localtime(&tval); + lt = localtime(&ts->tv_sec); if (lt == NULL) errx(1, "invalid time"); lt->tm_isdst = -1; /* divine correct DST */ @@ -338,18 +334,19 @@ setthetime(const char *fmt, const char *p, int jflag) } /* convert broken-down time to GMT clock time */ - if ((tval = mktime(lt)) == -1) + lt->tm_yday = -1; + ts->tv_sec = mktime(lt); + if (lt->tm_yday == -1) errx(1, "nonexistent time"); + ts->tv_nsec = 0; if (!jflag) { utx.ut_type = OLD_TIME; memset(utx.ut_id, 0, sizeof(utx.ut_id)); (void)gettimeofday(&utx.ut_tv, NULL); pututxline(&utx); - tv.tv_sec = tval; - tv.tv_usec = 0; - if (settimeofday(&tv, NULL) != 0) - err(1, "settimeofday (timeval)"); + if (clock_settime(CLOCK_REALTIME, ts) != 0) + err(1, "clock_settime"); utx.ut_type = NEW_TIME; (void)gettimeofday(&utx.ut_tv, NULL); pututxline(&utx); @@ -360,6 +357,82 @@ setthetime(const char *fmt, const char *p, int jflag) } } +/* + * The strftime_ns function is a wrapper around strftime(3), which adds support + * for features absent from strftime(3). Currently, the only extra feature is + * support for %N, the nanosecond conversion specification. + * + * The functions scans the format string for the non-standard conversion + * specifications and replaces them with the date and time values before + * passing the format string to strftime(3). The handling of the non-standard + * conversion specifications happens before the call to strftime(3) to handle + * cases like "%%N" correctly ("%%N" should yield "%N" instead of nanoseconds). + */ +static size_t +strftime_ns(char * __restrict s, size_t maxsize, const char * __restrict format, + const struct tm * __restrict t, long nsec) +{ + size_t prefixlen; + size_t ret; + char *newformat; + char *oldformat; + const char *prefix; + const char *suffix; + const char *tok; + bool seen_percent; + + seen_percent = false; + if ((newformat = strdup(format)) == NULL) + err(1, "strdup"); + tok = newformat; + for (tok = newformat; *tok != '\0'; tok++) { + switch (*tok) { + case '%': + /* + * If the previous token was a percent sign, + * then there are two percent tokens in a row. + */ + if (seen_percent) + seen_percent = false; + else + seen_percent = true; + break; + case 'N': + if (seen_percent) { + oldformat = newformat; + prefix = oldformat; + prefixlen = tok - oldformat - 1; + suffix = tok + 1; + /* + * Construct a new format string from the + * prefix (i.e., the part of the old format + * from its beginning to the currently handled + * "%N" conversion specification), the + * nanoseconds, and the suffix (i.e., the part + * of the old format from the next token to the + * end). + */ + if (asprintf(&newformat, "%.*s%.9ld%s", + (int)prefixlen, prefix, nsec, + suffix) < 0) { + err(1, "asprintf"); + } + free(oldformat); + tok = newformat + prefixlen + 9; + } + seen_percent = false; + break; + default: + seen_percent = false; + break; + } + } + + ret = strftime(s, maxsize, newformat, t); + free(newformat); + return (ret); +} + static void badformat(void) { @@ -383,9 +456,9 @@ static void usage(void) { (void)fprintf(stderr, "%s\n%s\n%s\n", - "usage: date [-jnRu] [-I[date|hours|minutes|seconds]] [-f input_fmt]", + "usage: date [-jnRu] [-I[date|hours|minutes|seconds|ns]] [-f input_fmt]", " " - "[-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]", + "[ -z output_zone ] [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]", " " "[[[[[[cc]yy]mm]dd]HH]MM[.SS] | new_date] [+output_fmt]" ); |