Add a '-a' option to set the time using adjtime(2) instead of settimeofday(2).

Fix usage so that the options which apply to dat/time setting are on the
correct line.
This commit is contained in:
dsl 2005-04-18 06:53:35 +00:00
parent 6b4d05a703
commit a7151c0abc
2 changed files with 48 additions and 19 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: date.1,v 1.33 2003/08/07 09:05:09 agc Exp $
.\" $NetBSD: date.1,v 1.34 2005/04/18 06:53:35 dsl 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 March 8, 2001
.Dd April 18, 2005
.Dt DATE 1
.Os
.Sh NAME
@ -40,10 +40,11 @@
.Nd display or set date and time
.Sh SYNOPSIS
.Nm
.Op Fl nu
.Op Fl u
.Op Fl r Ar seconds
.Op Cm + Ns Ar format
.Nm
.Op Fl anu
.Sm off
.Oo Oo Oo Oo Oo
.Ar cc Oc
@ -62,6 +63,14 @@ Only the superuser may set the date.
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl a
Use
.Xr adjtime 2
to change the local system time slowly,
maintaining it as a monotonically increasing function.
.Fl a
implies
.Fl n .
.It Fl n
The utility
.Xr timed 8
@ -208,6 +217,7 @@ and
timed
fails.
.Sh SEE ALSO
.Xr adjtime 2 ,
.Xr gettimeofday 2 ,
.Xr settimeofday 2 ,
.Xr strftime 3 ,

View File

@ -1,4 +1,4 @@
/* $NetBSD: date.c,v 1.37 2003/08/07 09:05:09 agc Exp $ */
/* $NetBSD: date.c,v 1.38 2005/04/18 06:53:35 dsl 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.37 2003/08/07 09:05:09 agc Exp $");
__RCSID("$NetBSD: date.c,v 1.38 2005/04/18 06:53:35 dsl Exp $");
#endif
#endif /* not lint */
@ -62,8 +62,9 @@ __RCSID("$NetBSD: date.c,v 1.37 2003/08/07 09:05:09 agc Exp $");
#include "extern.h"
time_t tval;
int retval, nflag;
static time_t tval;
static int aflag, rflag, nflag;
int retval;
int main(int, char *[]);
static void badformat(void);
@ -75,14 +76,17 @@ int
main(int argc, char *argv[])
{
char buf[1024], *format;
int ch, rflag;
int ch;
setprogname(argv[0]);
(void)setlocale(LC_ALL, "");
rflag = 0;
while ((ch = getopt(argc, argv, "nr:u")) != -1)
while ((ch = getopt(argc, argv, "anr:u")) != -1) {
switch((char)ch) {
case 'a': /* adjust time slowly */
aflag = 1;
nflag = 1;
break;
case 'n': /* don't set network */
nflag = 1;
break;
@ -96,6 +100,7 @@ main(int argc, char *argv[])
default:
usage();
}
}
argc -= optind;
argv += optind;
@ -144,6 +149,7 @@ static void
setthetime(const char *p)
{
struct timeval tv;
time_t new_time;
struct tm *lt;
const char *dot, *t;
int len, yearset;
@ -203,22 +209,35 @@ setthetime(const char *p)
case 2: /* mm */
lt->tm_min = ATOI2(p);
break;
case 0: /* was just .sss */
if (len != 0)
break;
/* FALLTHROUGH */
default:
badformat();
}
/* convert broken-down time to UTC clock time */
if ((tval = mktime(lt)) == -1)
if ((new_time = mktime(lt)) == -1)
badtime();
/* set the time */
if (nflag || netsettime(tval)) {
if (nflag || netsettime(new_time)) {
logwtmp("|", "date", "");
tv.tv_sec = tval;
tv.tv_usec = 0;
if (settimeofday(&tv, NULL)) {
perror("date: settimeofday");
exit(1);
if (aflag) {
tv.tv_sec = new_time - tval;
tv.tv_usec = 0;
if (adjtime(&tv, NULL)) {
perror("date: adjtime");
exit(1);
}
} else {
tv.tv_sec = tval;
tv.tv_usec = 0;
if (settimeofday(&tv, NULL)) {
perror("date: settimeofday");
exit(1);
}
}
logwtmp("{", "date", "");
}
@ -232,8 +251,8 @@ static void
usage(void)
{
(void)fprintf(stderr,
"usage: %s [-nu] [-r seconds] [+format]\n", getprogname());
(void)fprintf(stderr, " %s [[[[[cc]yy]mm]dd]hh]mm[.ss]\n", getprogname());
"usage: %s [-u] [-r seconds] [+format]\n", getprogname());
(void)fprintf(stderr, " %s [-anu] [[[[[cc]yy]mm]dd]hh]mm[.ss]\n", getprogname());
exit(1);
/* NOTREACHED */
}