Module Name: src Committed By: kre Date: Tue Sep 17 14:56:48 UTC 2024
Modified Files: src/bin/date: date.1 date.c Log Message: Add -U and -z options to date(1). -U allows the (internal) TZ setting to be returned to what it was when date started running. -z zone sets TZ to be "zone" unless that is an empty string, in which case it causes TZ to be removed from the environment. These can be used together to manipulate TZ around parsing of a -d arg, to allow its parsing to be done in a different zone than the one used for output, and all unrelated to the initial TZ setting (or system default). Note that these new options only appear in the first synopsis form in both the man page and usage(), as -d also appears only there, and these options make little sense without also using -d. This is a very simple change which amounts to no more that a few setenv() and unsetenv() calls. To generate a diff of this commit: cvs rdiff -u -r1.55 -r1.56 src/bin/date/date.1 cvs rdiff -u -r1.68 -r1.69 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.55 src/bin/date/date.1:1.56 --- src/bin/date/date.1:1.55 Tue Sep 17 10:08:38 2024 +++ src/bin/date/date.1 Tue Sep 17 14:56:48 2024 @@ -1,4 +1,4 @@ -.\" $NetBSD: date.1,v 1.55 2024/09/17 10:08:38 kre Exp $ +.\" $NetBSD: date.1,v 1.56 2024/09/17 14:56:48 kre Exp $ .\" .\" Copyright (c) 1980, 1990, 1993 .\" The Regents of the University of California. All rights reserved. @@ -40,9 +40,10 @@ .Nd display or set date and time .Sh SYNOPSIS .Nm -.Op Fl ajnRu +.Op Fl ajnRUu .Op Fl d Ar date .Op Fl r Ar seconds +.Op Fl z Ar zone .Op Cm + Ns Ar format .Sm off .Oo @@ -127,14 +128,35 @@ specification in RFC 5322 (Internet Mess Print out the date and time that is .Ar seconds from the Epoch. +.It Fl U +Reset the timezone used by +.Nm +to that which existed when it was invoked. +This is only useful after an earlier +.Fl u +or +.Fl z +option. .It Fl u Display or set the date in UTC (universal) time. +.It Fl z Ar zone +Set the timezone to be used by +.Nm +to +.Ar zone . +If +.Ar zone +is an empty string, revert to the system's +default timezone (ignoring any setting of +.Ev TZ ) . .El .Pp Note the -.Fl d +.Fl d , +.Fl U , +.Fl u , and -.Fl u +.Fl z options are applied when encountered, hence specifying .Fl u before @@ -270,6 +292,24 @@ The command: .Dl date +%s .Pp prints the current time as seconds since the Epoch. +.Pp +The command: +.Pp +.Dl date -z America/Chicago -d 13:00 -z Asia/Tokyo +%H:%M +.Pp +indicates what the time will be in Tokyo when it is 13:00 +in Chicago. +Any +.Xr strftime 3 +string could be used for the output. +The first +.Fl z +option can be omitted to parse the time as specified by +.Ev TZ +.Pq usually the local timezone , +or the second could be replaced by +.Fl U +to show the parsed time in the local timezone. .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.68 src/bin/date/date.c:1.69 --- src/bin/date/date.c:1.68 Tue Sep 17 11:30:11 2024 +++ src/bin/date/date.c Tue Sep 17 14:56:48 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: date.c,v 1.68 2024/09/17 11:30:11 kre Exp $ */ +/* $NetBSD: date.c,v 1.69 2024/09/17 14:56:48 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.68 2024/09/17 11:30:11 kre Exp $"); +__RCSID("$NetBSD: date.c,v 1.69 2024/09/17 14:56:48 kre Exp $"); #endif #endif /* not lint */ @@ -96,11 +96,14 @@ main(int argc, char *argv[]) int ch; long long val; struct tm *tm; + char *default_tz; setprogname(argv[0]); (void)setlocale(LC_ALL, ""); - while ((ch = getopt(argc, argv, "ad:f:jnRr:u")) != -1) { + default_tz = getenv("TZ"); + + while ((ch = getopt(argc, argv, "ad:f:jnRr:Uuz:")) != -1) { switch (ch) { case 'a': /* adjust time slowly */ aflag = 1; @@ -151,9 +154,21 @@ main(int argc, char *argv[]) rflag = 1; tval = (time_t)val; break; + case 'U': /* reset to default timezone */ + if (default_tz) + (void)setenv("TZ", default_tz, 1); + else + (void)unsetenv("TZ"); + break; case 'u': /* do everything in UTC */ (void)setenv("TZ", "UTC0", 1); break; + case 'z': + if (optarg[0] == '\0') + (void)unsetenv("TZ"); + else + (void)setenv("TZ", optarg, 1); + break; default: usage(); } @@ -538,9 +553,10 @@ static void usage(void) { (void)fprintf(stderr, - "Usage: %s [-ajnRu] [-d date] [-r seconds] [+format]", + "Usage: %s [-ajnRUu] [-d date] [-r seconds] [-z zone] [+format]", getprogname()); - (void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n"); + (void)fprintf(stderr, "\n\t%*s[[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n", + (int)strlen(getprogname()), ""); (void)fprintf(stderr, " %s [-ajnRu] -f input_format new_date [+format]\n", getprogname());