From nobody Fri Apr 26 09:27:25 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VQnSG1BPtz5JK0W; Fri, 26 Apr 2024 09:27:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VQnSG0hqzz49W4; Fri, 26 Apr 2024 09:27:26 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714123646; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7nkaYMmHZ9NFCWydKg/Ccz1Rt5JDbgcF0CEgqKK/eNc=; b=h3wsTPXVlmLn+EMxupTm+Ob0NtjMtXzUNK17GOhMyCh9l28Y1adD+0H9Kwll5QcVlx8jb9 LxrekiYBan9zwZ9U5dbQ2NdcAw6flcxqvVziOMrG+wp+IlFFFksS/5XgfJpYzRdj2fuxtJ z4zow02dpG8JSunNMgve0hvOD1pfUDgN/boN9ih3g2jeXyoT2F6ei+rgV6f9+sdFKAMipU nOZWxqBCvpFzWbfqCO7YzLtXRcr4HTchh/fcBVJ7iCN4kK47u6uJZ9+LeGUxqMnKeNYS66 hb0HtygHL3AREpalzNxrY7ONcuMizcPu/PFGnr58MPjF0+VgdrhsngM9ef7ZDg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1714123646; a=rsa-sha256; cv=none; b=ns45VFR6HjkPEf3eh4uonl+n0nZbtaMaDnDrTDn+zfBLh6K2SaYIKcdwcHlzTpKKAUQy2C gwalC3zNMEQ5uZb9yGzO5f5z/tOLEi6QiAngjePbNYpecPiLrRi2LaUtNVdg6GR4nyAw/e mopEn4W8+8naiOi7XxCShpc/cC7/L3vtplQp20IinM97l/J8raz4x0+2QcO+8TxXfe8PfG v2O9uNX65hQW1MUjltRyBitW2miw03VadIFXQLBLdHVeX9JczQVN7r2INQHVqZuC/yCpf8 uRx4l+pnLm0pD6lXFvUOgaIcdrG2QRSPiNvxzT0M+E+G5JO0QFzU6a/YtQbPIA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714123646; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7nkaYMmHZ9NFCWydKg/Ccz1Rt5JDbgcF0CEgqKK/eNc=; b=nV5bxjAiARmWny3rTxQPkIrc5FTjOal7s3jf4JWs2GdAUI8PAd1RR193o4rXC3aR9MzcOW /eXm4K0FqxKIyGAGLLtXBacUnrAlA23606HMTZnEmUWAsffd+Z+DjneWH3nOgY3eOBdx3d +glXZec/R2ZYXo0dsXqsTTec70a1q9753x/PaORWBEqB4GddocUDZHxaxSF9bVvhT963fh hnc/NZe+Re7Ubk5NxA8U2IiqzfdGlFwSKPvPBWzaW3NbcK+nEdP9VczF0Gnl+1/sTHEIez 0Tr6HlL8jWkzvcA3nstA7pNCIEAFMTTtgExmnM3PkXsLcFgAgIS1d9kdQzucrQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VQnSG0BtzzcPk; Fri, 26 Apr 2024 09:27:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43Q9RP9r038492; Fri, 26 Apr 2024 09:27:25 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43Q9RPbT038489; Fri, 26 Apr 2024 09:27:25 GMT (envelope-from git) Date: Fri, 26 Apr 2024 09:27:25 GMT Message-Id: <202404260927.43Q9RPbT038489@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mateusz Piotrowski <0mp@FreeBSD.org> Subject: git: eeb04a736cb9 - main - date: Add support for nanoseconds List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: 0mp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: eeb04a736cb9c07d191af886e25d5f198824658e Auto-Submitted: auto-generated The branch main has been updated by 0mp: URL: https://cgit.FreeBSD.org/src/commit/?id=eeb04a736cb9c07d191af886e25d5f198824658e commit eeb04a736cb9c07d191af886e25d5f198824658e Author: Mateusz Piotrowski <0mp@FreeBSD.org> AuthorDate: 2024-04-21 21:25:32 +0000 Commit: Mateusz Piotrowski <0mp@FreeBSD.org> CommitDate: 2024-04-26 09:26:50 +0000 date: Add support for nanoseconds This patch introduces support for a conversion specification for nanoseconds. The format of %N is meant to be compatible with that of GNU date. The nanoseconds conversion specification is implemented directly in date(1) instead of libc (in strftime(3)) to avoid introducing non-standard functions to libc at this time and modifying struct tm. Apart from introducing the nanoseconds conversion specification, this patch brings the following changes: - The "ns" format for ISO 8061 dates is now unlocked. E.g., date -Ins prints: 2024-04-22T12:20:28,763742224+02:00 - The -r flag when fed a file is now aware of the nanosecond part of the last modification time. - date(1) is now able to set the time with nanosecond precision. It is not possible as of now to do that by specifying nanoseconds directly via the command-line arguments. Instead, the -r flag can be used. - date(1) is now using the clock_gettime(3) family of functions instead of ctime(3) family of functions where possible. Reviewed by: des, markj Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D44905 --- bin/date/date.1 | 28 ++++++-- bin/date/date.c | 132 ++++++++++++++++++++++++++++------- bin/date/tests/format_string_test.sh | 17 ++++- 3 files changed, 142 insertions(+), 35 deletions(-) diff --git a/bin/date/date.1 b/bin/date/date.1 index 286b1f45414a..afa6d555a503 100644 --- a/bin/date/date.1 +++ b/bin/date/date.1 @@ -29,7 +29,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd May 19, 2023 +.Dd April 26, 2024 .Dt DATE 1 .Os .Sh NAME @@ -141,17 +141,19 @@ values are .Cm date , .Cm hours , .Cm minutes , +.Cm seconds , and -.Cm seconds . +.Cm ns No Pq for nanoseconds . The date and time is formatted to the specified precision. When .Ar FMT is .Cm hours -(or the more precise -.Cm minutes +.Po or the more precise +.Cm minutes , +.Cm seconds , or -.Cm seconds ) , +.Cm ns Pc , the .St -iso8601 format includes the timezone. @@ -325,7 +327,9 @@ which specifies the format in which to display the date and time. The format string may contain any of the conversion specifications described in the .Xr strftime 3 -manual page, as well as any arbitrary text. +manual page and +.Ql %N +for nanoseconds, as well as any arbitrary text. A newline .Pq Ql \en character is always output after the characters specified by @@ -551,6 +555,7 @@ prints: and exits with status 1. .Sh SEE ALSO .Xr locale 1 , +.Xr clock_gettime 2 , .Xr gettimeofday 2 , .Xr getutxent 3 , .Xr strftime 3 , @@ -581,6 +586,12 @@ The format selected by the .Fl I flag is compatible with .St -iso8601 . +.Pp +The +.Ql %N +conversion specification for nanoseconds is a non-standard extension. +It is compatible with GNU date's +.Ql %N . .Sh HISTORY A .Nm @@ -599,3 +610,8 @@ The .Fl I flag was added in .Fx 12.0 . +.Pp +The +.Ql %N +conversion specification was added in +.Fx 15.0 . diff --git a/bin/date/date.c b/bin/date/date.c index 0ff3b9f74cbd..ccd72cb98445 100644 --- a/bin/date/date.c +++ b/bin/date/date.c @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -50,14 +51,14 @@ #define TM_YEAR_BASE 1900 #endif -static time_t tval; - static void badformat(void); 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 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 { @@ -68,6 +69,7 @@ static const struct iso8601_fmt { { "hours", "T%H" }, { "minutes", ":%M" }, { "seconds", ":%S" }, + { "ns", ",%N" }, }; static const struct iso8601_fmt *iso8601_selected; @@ -76,6 +78,7 @@ 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; @@ -126,11 +129,12 @@ 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; @@ -149,8 +153,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 = "%+"; @@ -166,7 +170,7 @@ main(int argc, char *argv[]) } if (*argv) { - setthetime(fmt, *argv, jflag); + setthetime(fmt, *argv, jflag, &ts); ++argv; } else if (fmt != NULL) usage(); @@ -179,7 +183,7 @@ main(int argc, char *argv[]) if (outzone != NULL && setenv("TZ", outzone, 1) != 0) err(1, "setenv(TZ)"); - lt = localtime(&tval); + lt = localtime(&ts.tv_sec); if (lt == NULL) errx(1, "invalid time"); badv = vary_apply(v, lt); @@ -192,7 +196,7 @@ main(int argc, char *argv[]) vary_destroy(v); if (Iflag) - printisodate(lt); + printisodate(lt, ts.tv_nsec); if (format == rfc2822_format) /* @@ -202,7 +206,7 @@ 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); } @@ -216,19 +220,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)); @@ -240,15 +244,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 */ @@ -329,18 +332,17 @@ setthetime(const char *fmt, const char *p, int jflag) } /* convert broken-down time to GMT clock time */ - if ((tval = mktime(lt)) == -1) + if ((ts->tv_sec = mktime(lt)) == -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); @@ -351,6 +353,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 (asprintf(&newformat, "%s", format) < 0) + err(1, "asprintf"); + 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 fromat + * 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) { @@ -374,7 +452,7 @@ 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]", " " "[ -z output_zone ] [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]", " " diff --git a/bin/date/tests/format_string_test.sh b/bin/date/tests/format_string_test.sh index a3e2939bfe5c..c2fe2111373f 100755 --- a/bin/date/tests/format_string_test.sh +++ b/bin/date/tests/format_string_test.sh @@ -30,6 +30,17 @@ check() date -r ${TEST2} +%${format_string} } +atf_test_case flag_r_file_test +flag_r_file_test_body() +{ + local file + + file="./testfile" + touch "$file" + atf_check -o "inline:$(stat -f '%9Fm' "$file")\n" \ + date -r "$file" +%s.%N +} + format_string_test() { local desc exp_output_1 exp_output_2 flag @@ -98,6 +109,8 @@ iso8601_${desc}_parity_body() { atf_init_test_cases() { + atf_add_test_case flag_r_file_test + format_string_test A A Saturday Monday format_string_test a a Sat Mon format_string_test B B February November @@ -118,6 +131,7 @@ atf_init_test_cases() format_string_test l l " 7" " 9" format_string_test M M 04 20 format_string_test m m 02 11 + format_string_test N N 000000000 000000000 format_string_test p p AM PM format_string_test R R 07:04 21:20 format_string_test r r "07:04:03 AM" "09:20:00 PM" @@ -143,6 +157,5 @@ atf_init_test_cases() iso8601_string_test hours hours "" "1970-02-07T07+00:00" "2001-11-12T21+00:00" iso8601_string_test minutes minutes "" "1970-02-07T07:04+00:00" "2001-11-12T21:20+00:00" iso8601_string_test seconds seconds "" "1970-02-07T07:04:03+00:00" "2001-11-12T21:20:00+00:00" - # BSD date(1) does not support fractional seconds at this time. - #iso8601_string_test ns ns "" "1970-02-07T07:04:03,000000000+00:00" "2001-11-12T21:20:00,000000000+00:00" + iso8601_string_test ns ns "" "1970-02-07T07:04:03,000000000+00:00" "2001-11-12T21:20:00,000000000+00:00" }