Add -j flag to not actually change the clock, just parse the date given

(if any) and then display it in the format given.  Matches similar -j
flag functionality in FreeBSD/OpenBSD.

Change requested by George Georgalis on netbsd-users.

While I'm here, disambiguate the letters in the date string, by using
the same option letters as used by strftime(3).
This commit is contained in:
jdarrow 2006-11-15 03:10:01 +00:00
parent 13117244bf
commit 5d27a9d665
2 changed files with 31 additions and 22 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: date.1,v 1.34 2005/04/18 06:53:35 dsl Exp $
.\" $NetBSD: date.1,v 1.35 2006/11/15 03:10:01 jdarrow Exp $
.\"
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
@ -40,19 +40,17 @@
.Nd display or set date and time
.Sh SYNOPSIS
.Nm
.Op Fl u
.Op Fl ajnu
.Op Fl r Ar seconds
.Op Cm + Ns Ar format
.Nm
.Op Fl anu
.Sm off
.Oo Oo Oo Oo Oo
.Ar cc Oc
.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
.Ar HH Oc Ar MM Oo
.Li \&. Ar SS Oc Oc
.Sm on
.Sh DESCRIPTION
.Nm
@ -71,6 +69,9 @@ maintaining it as a monotonically increasing function.
.Fl a
implies
.Fl n .
.It Fl j
Parse the provided date and time and display the result without
actually changing the system clock.
.It Fl n
The utility
.Xr timed 8
@ -115,31 +116,31 @@ a value for setting the system's notion of the current date and time.
The canonical representation for setting the date and time is:
.Pp
.Bl -tag -width Ds -compact -offset indent
.It Ar cc
.It Ar CC
The first two digits of the year (the century).
.It Ar yy
The second two digits of the year.
If
.Ar yy
is specified, but
.Ar cc
.Ar CC
is not, a value for
.Ar yy
between 69 and 99 results in a
.Ar cc
.Ar CC
value of 19.
Otherwise, a
.Ar cc
.Ar CC
value of 20 is used.
.It Ar mm
The month of the year, from 01 to 12.
.It Ar dd
The day of the month, from 01 to 31.
.It Ar hh
.It Ar HH
The hour of the day, from 00 to 23.
.It Ar mm
.It Ar MM
The minute of the hour, from 00 to 59.
.It Ar ss
.It Ar SS
The second of the minute, from 00 to 61.
.El
.Pp

View File

@ -1,4 +1,4 @@
/* $NetBSD: date.c,v 1.45 2006/10/07 09:34:46 elad Exp $ */
/* $NetBSD: date.c,v 1.46 2006/11/15 03:10:01 jdarrow Exp $ */
/*
* Copyright (c) 1985, 1987, 1988, 1993
@ -40,7 +40,7 @@ __COPYRIGHT(
#if 0
static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: date.c,v 1.45 2006/10/07 09:34:46 elad Exp $");
__RCSID("$NetBSD: date.c,v 1.46 2006/11/15 03:10:01 jdarrow Exp $");
#endif
#endif /* not lint */
@ -63,7 +63,7 @@ __RCSID("$NetBSD: date.c,v 1.45 2006/10/07 09:34:46 elad Exp $");
#include "extern.h"
static time_t tval;
static int aflag, rflag, nflag;
static int aflag, jflag, rflag, nflag;
int retval;
static void badformat(void);
@ -83,12 +83,15 @@ main(int argc, char *argv[])
setprogname(argv[0]);
(void)setlocale(LC_ALL, "");
while ((ch = getopt(argc, argv, "anr:u")) != -1) {
while ((ch = getopt(argc, argv, "ajnr:u")) != -1) {
switch (ch) {
case 'a': /* adjust time slowly */
aflag = 1;
nflag = 1;
break;
case 'j': /* don't set time */
jflag = 1;
break;
case 'n': /* don't set network */
nflag = 1;
break;
@ -274,6 +277,12 @@ setthetime(const char *p)
if ((new_time = mktime(lt)) == -1)
badtime();
/* if jflag is set, don't actually change the time, just return */
if (jflag) {
tval = new_time;
return;
}
/* set the time */
if (nflag || netsettime(new_time)) {
logwtmp("|", "date", "");
@ -301,9 +310,8 @@ static void
usage(void)
{
(void)fprintf(stderr,
"usage: %s [-u] [-r seconds] [+format]\n", getprogname());
(void)fprintf(stderr, " %s [-anu] [[[[[cc]yy]mm]dd]hh]mm[.ss]\n",
getprogname());
"usage: %s [-ajnu] [-r seconds] [+format]", getprogname());
(void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");
exit(EXIT_FAILURE);
/* NOTREACHED */
}