Module Name:    src
Committed By:   kre
Date:           Tue Sep 17 15:25:39 UTC 2024

Modified Files:
        src/bin/date: date.1 date.c

Log Message:
date(1) says:

STANDARDS
     The date utility is expected to be compatible with IEEE Std 1003.2
     (“POSIX.2”).

yet the format used for the date string arg is:

        [[[[[[CC]yy]mm]dd]HH]MM[.SS]]

whereas POSIX demands

        mmddHHMM[[CC]yy]

Why anyone would ever want to use that archaic form is incomprehensible
to me, yet, that is what is required.

Implement support for the POSIX format if POSIXLY_CORRECT is set in
the environment (in the full date(1) build only) in a rather crude
way that relies upon the user getting the format correct.

Tools builds are unaffected - setting the time is not supported there.
(It could be made to work, half setting the time, using -j, is possible
in tools builds, but pointless, and this cheap implementation uses
strptime() which is not necessarily available to a tools build.)

Mention this (very briefly) in the man page.


To generate a diff of this commit:
cvs rdiff -u -r1.56 -r1.57 src/bin/date/date.1
cvs rdiff -u -r1.69 -r1.70 src/bin/date/date.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/date/date.1
diff -u src/bin/date/date.1:1.56 src/bin/date/date.1:1.57
--- src/bin/date/date.1:1.56	Tue Sep 17 14:56:48 2024
+++ src/bin/date/date.1	Tue Sep 17 15:25:39 2024
@@ -1,4 +1,4 @@
-.\"	$NetBSD: date.1,v 1.56 2024/09/17 14:56:48 kre Exp $
+.\"	$NetBSD: date.1,v 1.57 2024/09/17 15:25:39 kre Exp $
 .\"
 .\" Copyright (c) 1980, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -310,6 +310,15 @@ option can be omitted to parse the time 
 or the second could be replaced by
 .Fl U
 to show the parsed time in the local timezone.
+.Pp
+Either of the commands:
+.Pp
+.Dl date -u -d 1970-01-01T00:00:00 -U +%c
+.Dl date -r 0 +%c
+.Pp
+print the local time of the
+.Ux
+epoch.
 .Sh DIAGNOSTICS
 Exit status is 0 on success, 1 if unable to set the date, and 2
 if able to set the local date, but unable to set it globally.
@@ -350,6 +359,13 @@ The
 .Nm
 utility is expected to be compatible with
 .St -p1003.2 .
+However, achieving true compatability requires running
+.Nm
+with the environment variable
+.Ev POSIXLY_CORRECT
+set, in order to parse the time string in the
+archaic format POSIX demands, rather than the
+more rational version described above.
 .Sh HISTORY
 A
 .Nm

Index: src/bin/date/date.c
diff -u src/bin/date/date.c:1.69 src/bin/date/date.c:1.70
--- src/bin/date/date.c:1.69	Tue Sep 17 14:56:48 2024
+++ src/bin/date/date.c	Tue Sep 17 15:25:39 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: date.c,v 1.69 2024/09/17 14:56:48 kre Exp $ */
+/* $NetBSD: date.c,v 1.70 2024/09/17 15:25:39 kre Exp $ */
 
 /*
  * Copyright (c) 1985, 1987, 1988, 1993
@@ -44,7 +44,7 @@ __COPYRIGHT(
 #if 0
 static char sccsid[] = "@(#)date.c	8.2 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: date.c,v 1.69 2024/09/17 14:56:48 kre Exp $");
+__RCSID("$NetBSD: date.c,v 1.70 2024/09/17 15:25:39 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -373,6 +373,23 @@ setthetime(const char *p)
 				strlen(t), t);
 		goto setit;
 	}
+	if (getenv("POSIXLY_CORRECT") != NULL) {
+		int yrdigs;
+		const char * const e = "Bad POSIX format date ``%s''";
+
+		t = strptime(p, "%m%d%H%M", lt);
+		if (t == NULL)
+			errx(EXIT_FAILURE, e, p);
+		if (*t != '\0') {
+			yrdigs = strspn(t, "0123456789");
+			if (yrdigs != 2 && yrdigs != 4)
+				errx(EXIT_FAILURE, e, p);
+			t = strptime(t, yrdigs == 2 ? "%y" : "%Y", lt);
+			if (t == NULL || *t != '\0')
+				errx(EXIT_FAILURE, e, p);
+		}
+		goto setit;
+	}
 #endif
 	for (t = p, dot = NULL; *t; ++t) {
 		if (*t == '.') {

Reply via email to