Module Name:    src
Committed By:   martin
Date:           Sat Jun  3 15:27:13 UTC 2023

Modified Files:
        src/bin/date [netbsd-9]: Makefile date.1 date.c

Log Message:
Pull up the following revisions, requested by kim in ticket #1640:

        bin/date/Makefile                               up to 1.16
        bin/date/date.1                                 up to 1.54
        bin/date/date.c                                 up to 1.65

Add -R option for displaying time in RFC 5322 format, similar to GNU date.
Add -f option to set the time. From FreeBSD.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.15.46.1 src/bin/date/Makefile
cvs rdiff -u -r1.47 -r1.47.6.1 src/bin/date/date.1
cvs rdiff -u -r1.61 -r1.61.18.1 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/Makefile
diff -u src/bin/date/Makefile:1.15 src/bin/date/Makefile:1.15.46.1
--- src/bin/date/Makefile:1.15	Sun Aug 14 10:53:16 2011
+++ src/bin/date/Makefile	Sat Jun  3 15:27:13 2023
@@ -1,10 +1,15 @@
-#	$NetBSD: Makefile,v 1.15 2011/08/14 10:53:16 christos Exp $
+#	$NetBSD: Makefile,v 1.15.46.1 2023/06/03 15:27:13 martin Exp $
 #	@(#)Makefile	8.1 (Berkeley) 5/31/93
 
+.include <bsd.init.mk>
+
 PROG=	date
-SRCS=	date.c netdate.c
+SRCS=	date.c
+.if !defined(HOSTPROG)
+SRCS+=	netdate.c
 DPADD+=	${LIBUTIL}
 LDADD+=	-lutil
+.endif
 CPPFLAGS+=-I${.CURDIR}
 
 COPTS.date.c = -Wno-format-nonliteral

Index: src/bin/date/date.1
diff -u src/bin/date/date.1:1.47 src/bin/date/date.1:1.47.6.1
--- src/bin/date/date.1:1.47	Sat Jan 27 18:59:38 2018
+++ src/bin/date/date.1	Sat Jun  3 15:27:13 2023
@@ -1,4 +1,4 @@
-.\"	$NetBSD: date.1,v 1.47 2018/01/27 18:59:38 wiz Exp $
+.\"	$NetBSD: date.1,v 1.47.6.1 2023/06/03 15:27:13 martin Exp $
 .\"
 .\" Copyright (c) 1980, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"     @(#)date.1	8.3 (Berkeley) 4/28/95
 .\"
-.Dd January 25, 2018
+.Dd May 31, 2023
 .Dt DATE 1
 .Os
 .Sh NAME
@@ -40,19 +40,27 @@
 .Nd display or set date and time
 .Sh SYNOPSIS
 .Nm
-.Op Fl ajnu
+.Op Fl ajnRu
 .Op Fl d Ar date
 .Op Fl r Ar seconds
 .Op Cm + Ns Ar format
 .Sm off
-.Oo Oo Oo Oo Oo Oo
+.Oo
+.Oo Oo Oo Oo Oo
 .Ar CC Oc
 .Ar yy Oc
 .Ar mm Oc
 .Ar dd Oc
-.Ar HH Oc Ar MM Oo
-.Li \&. Ar SS Oc Oc
+.Ar HH Oc
+.Ar MM
+.Op Cm \&. Ar SS
+.Oc
 .Sm on
+.Nm
+.Op Fl ajnRu
+.Fl f Ar input_format
+.Ar new_date
+.Op Cm + Ns Ar format
 .Sh DESCRIPTION
 .Nm
 displays the current date and time when invoked without arguments.
@@ -61,7 +69,7 @@ way or set the date.
 Only the superuser may set the date.
 .Pp
 The options are as follows:
-.Bl -tag -width Ds
+.Bl -tag -width Fl
 .It Fl a
 Use
 .Xr adjtime 2
@@ -76,6 +84,25 @@ actually changing the system clock.
 (See
 .Xr parsedate 3
 for examples.)
+.It Fl f Ar input_fmt
+Use
+.Ar input_fmt
+as the format string to parse the
+.Ar new_date
+provided rather than using the default
+.Sm off
+.Oo Oo Oo Oo Oo
+.Ar CC Oc
+.Ar yy Oc
+.Ar mm Oc
+.Ar dd Oc
+.Ar HH Oc
+.Ar MM
+.Op Cm \&. Ar SS
+.Sm on
+format.
+Parsing is done using
+.Xr strptime 3 .
 .It Fl j
 Parse the provided canonical representation of date and time (described below)
 and display the result without actually changing the system clock.
@@ -93,6 +120,9 @@ The
 option stops
 .Nm
 from setting the time for other than the current machine.
+.It Fl R
+Use a default display format that conforms to the date and time
+specification in RFC 5322 (Internet Message Format).
 .It Fl r Ar seconds
 Print out the date and time that is
 .Ar seconds
@@ -109,12 +139,13 @@ The format string may contain any of the
 in the
 .Xr strftime 3
 manual page, as well as any arbitrary text.
-A <newline> character is always output after the characters
+A
+.Aq newline
+character is always output after the characters
 specified by the format string.
 The format string for the default display is:
-.Bd -literal -offset indent
-%a %b %e %H:%M:%S %Z %Y
-.Ed
+.Pp
+.Dl %a %b %e %H:%M:%S %Z %Y
 .Pp
 If an operand does not have a leading plus sign, it is interpreted as
 a value for setting the system's notion of the current date and time.
@@ -156,7 +187,7 @@ and years are handled automatically.
 .Sh ENVIRONMENT
 The following environment variables affect the execution of
 .Nm :
-.Bl -tag -width iTZ
+.Bl -tag -width Ev
 .It Ev TZ
 The timezone to use when displaying dates.
 See
@@ -164,13 +195,14 @@ See
 for more information.
 .El
 .Sh FILES
-.Bl -tag -width /usr/share/zoneinfo/posixrules -compact
+.Bl -tag -width Pa -compact
 .It Pa /etc/localtime
 Symlink pointing to system's default timezone information file in
 .Pa /usr/share/zoneinfo
 directory.
-.It Pa /usr/lib/locale/<L>/LC_TIME
-Description of time locale <L>.
+.It Pa /usr/lib/locale/ Ns Ao Ar L Ac Ns Pa /LC_TIME
+Description of time locale
+.Aq Ar L .
 .It Pa /usr/share/zoneinfo
 Time zone information directory.
 .It Pa /usr/share/zoneinfo/posixrules
@@ -189,9 +221,8 @@ is absent, UTC leap seconds are loaded f
 .Pa /usr/share/zoneinfo/posixrules .
 .Sh EXAMPLES
 The command:
-.Bd -literal -offset indent
-date '+DATE: %m/%d/%y%nTIME: %H:%M:%S'
-.Ed
+.Pp
+.Dl date '+DATE: %m/%d/%y%nTIME: %H:%M:%S'
 .Pp
 will display:
 .Bd -literal -offset indent
@@ -200,21 +231,25 @@ TIME: 13:36:16
 .Ed
 .Pp
 The command:
-.Bd -literal -offset indent
-date 8506131627
-.Ed
+.Pp
+.Dl date 8506131627
 .Pp
 sets the date to
 .Dq Li "June 13, 1985, 4:27 PM" .
 .Pp
 The command:
-.Bd -literal -offset indent
-date 1432
-.Ed
+.Pp
+.Dl date 1432
 .Pp
 sets the time to
 .Li "2:32 PM" ,
 without modifying the date.
+.Pp
+The command:
+.Pp
+.Dl date +%s
+.Pp
+prints the current time as seconds since the 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.

Index: src/bin/date/date.c
diff -u src/bin/date/date.c:1.61 src/bin/date/date.c:1.61.18.1
--- src/bin/date/date.c:1.61	Mon Sep  1 21:42:21 2014
+++ src/bin/date/date.c	Sat Jun  3 15:27:13 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: date.c,v 1.61 2014/09/01 21:42:21 dholland Exp $ */
+/* $NetBSD: date.c,v 1.61.18.1 2023/06/03 15:27:13 martin Exp $ */
 
 /*
  * Copyright (c) 1985, 1987, 1988, 1993
@@ -29,6 +29,10 @@
  * SUCH DAMAGE.
  */
 
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
 #include <sys/cdefs.h>
 #ifndef lint
 __COPYRIGHT(
@@ -40,7 +44,7 @@ __COPYRIGHT(
 #if 0
 static char sccsid[] = "@(#)date.c	8.2 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: date.c,v 1.61 2014/09/01 21:42:21 dholland Exp $");
+__RCSID("$NetBSD: date.c,v 1.61.18.1 2023/06/03 15:27:13 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -60,11 +64,15 @@ __RCSID("$NetBSD: date.c,v 1.61 2014/09/
 #include <tzfile.h>
 #include <unistd.h>
 #include <util.h>
+#if !HAVE_NBTOOL_CONFIG_H
+#include <utmpx.h>
+#endif
 
 #include "extern.h"
 
 static time_t tval;
-static int aflag, jflag, rflag, nflag;
+static int Rflag, aflag, jflag, rflag, nflag;
+static char *fmt;
 
 __dead static void badcanotime(const char *, const char *, size_t);
 static void setthetime(const char *);
@@ -83,13 +91,14 @@ main(int argc, char *argv[])
 	setprogname(argv[0]);
 	(void)setlocale(LC_ALL, "");
 
-	while ((ch = getopt(argc, argv, "ad:jnr:u")) != -1) {
+	while ((ch = getopt(argc, argv, "ad:f:jnRr:u")) != -1) {
 		switch (ch) {
 		case 'a':		/* adjust time slowly */
 			aflag = 1;
 			nflag = 1;
 			break;
 		case 'd':
+#ifndef HAVE_NBTOOL_CONFIG_H
 			rflag = 1;
 			tval = parsedate(optarg, NULL, NULL);
 			if (tval == -1) {
@@ -97,12 +106,22 @@ main(int argc, char *argv[])
 				    "%s: Unrecognized date format", optarg);
 			}
 			break;
+#else
+			errx(EXIT_FAILURE,
+			    "-d not supported in the tool version");
+#endif
+		case 'f':
+			fmt = optarg;
+			break;
 		case 'j':		/* don't set time */
 			jflag = 1;
 			break;
 		case 'n':		/* don't set network */
 			nflag = 1;
 			break;
+		case 'R':		/* RFC-5322 email format */
+			Rflag = 1;
+			break;
 		case 'r':		/* user specified seconds */
 			if (optarg[0] == '\0') {
 				errx(EXIT_FAILURE, "<empty>: Invalid number");
@@ -137,13 +156,17 @@ main(int argc, char *argv[])
 	if (*argv && **argv == '+') {
 		format = *argv;
 		++argv;
+	} else if (Rflag) {
+		(void)setlocale(LC_TIME, "C");
+		format = "+%a, %-e %b %Y %H:%M:%S %z";
 	} else
 		format = "+%a %b %e %H:%M:%S %Z %Y";
 
 	if (*argv) {
 		setthetime(*argv);
 		++argv;
-	}
+	} else if (fmt)
+		usage();
 
 	if (*argv && **argv == '+')
 		format = *argv;
@@ -186,6 +209,22 @@ setthetime(const char *p)
 	size_t len;
 	int yearset;
 
+	if ((lt = localtime(&tval)) == NULL)
+		err(EXIT_FAILURE, "%lld: localtime", (long long)tval);
+
+	lt->tm_isdst = -1;			/* Divine correct DST */
+
+	if (fmt) {
+		t = strptime(p, fmt, lt);
+		if (t == NULL) {
+			warnx("Failed conversion of ``%s''"
+			    " using format ``%s''\n", p, fmt);
+		} else if (*t != '\0')
+			warnx("Ignoring %zu extraneous"
+				" characters in date string (%s)",
+				strlen(t), t);
+		goto setit;
+	}
 	for (t = p, dot = NULL; *t; ++t) {
 		if (*t == '.') {
 			if (dot == NULL) {
@@ -198,10 +237,6 @@ setthetime(const char *p)
 		}
 	}
 
-	if ((lt = localtime(&tval)) == NULL)
-		err(EXIT_FAILURE, "%lld: localtime", (long long)tval);
-
-	lt->tm_isdst = -1;			/* Divine correct DST */
 
 	if (dot != NULL) {			/* .ss */
 		len = strlen(dot);
@@ -316,11 +351,11 @@ setthetime(const char *p)
 		    badcanotime("Not enough digits", p, strlen(p) - len);
 	    }
 	}
-
+setit:
 	/* convert broken-down time to UTC clock time */
 	if ((new_time = mktime(lt)) == -1) {
 		/* Can this actually happen? */
-		err(EXIT_FAILURE, "%s: mktime", op);
+		err(EXIT_FAILURE, "mktime");
 	}
 
 	/* if jflag is set, don't actually change the time, just return */
@@ -330,6 +365,13 @@ setthetime(const char *p)
 	}
 
 	/* set the time */
+#ifndef HAVE_NBTOOL_CONFIG_H
+	struct utmpx utx;
+	memset(&utx, 0, sizeof(utx));
+	utx.ut_type = OLD_TIME;
+	(void)gettimeofday(&utx.ut_tv, NULL);
+	pututxline(&utx);
+
 	if (nflag || netsettime(new_time)) {
 		logwtmp("|", "date", "");
 		if (aflag) {
@@ -346,19 +388,28 @@ setthetime(const char *p)
 		}
 		logwtmp("{", "date", "");
 	}
+	utx.ut_type = NEW_TIME;
+	(void)gettimeofday(&utx.ut_tv, NULL);
+	pututxline(&utx);
 
 	if ((p = getlogin()) == NULL)
 		p = "???";
 	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
+#else
+	errx(EXIT_FAILURE, "Can't set the time in the tools version");
+#endif
 }
 
 static void
 usage(void)
 {
 	(void)fprintf(stderr,
-	    "Usage: %s [-ajnu] [-d date] [-r seconds] [+format]",
+	    "Usage: %s [-ajnRu] [-d date] [-r seconds] [+format]",
 	    getprogname());
 	(void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");
+	(void)fprintf(stderr,
+	    "       %s [-ajnRu] -f input_format new_date [+format]\n",
+	    getprogname());
 	exit(EXIT_FAILURE);
 	/* NOTREACHED */
 }

Reply via email to