- WARNS?=6 - Statify - Style(9) - Don't cast - Bump FreeBSD CVS IDs - Check for write errors; report and exit with error value. Author: dds, cp.c:1.44 - udp/timed -> timed/udp. Author: charnier, netdate.c:1.43 - Change the behaviour of `-v' so that, e.g., stepping a month back on March 31 won't take you to March 2 or 3 (now the result will be the last day of February.) Author: yar, vary.c:1.15 - Add the new standard EXIT STATUS section where appropriate. Sort standard sections in the (documented) preferred order. Author: ru, date.1:1.71 - Give the most awesome example of how to parse the output of date back into Epoch time. Author: alfred, date.1:1.67 - better SYSNOPIS. Author: green, date.1:1.63 - Minor man page clean ups by ru Index: Makefile =================================================================== RCS file: /home/dcvs/src/bin/date/Makefile,v retrieving revision 1.5 diff -u -r1.5 Makefile --- Makefile 6 Feb 2005 06:16:39 -0000 1.5 +++ Makefile 5 Mar 2005 10:22:08 -0000 @@ -3,6 +3,7 @@ # $DragonFly: src/bin/date/Makefile,v 1.5 2005/02/06 06:16:39 okumoto Exp $ PROG= date +WARNS?= 6 SRCS= date.c netdate.c vary.c DPADD= ${LIBUTIL} Index: date.1 =================================================================== RCS file: /home/dcvs/src/bin/date/date.1,v retrieving revision 1.2 diff -u -r1.2 date.1 --- date.1 17 Jun 2003 04:22:49 -0000 1.2 +++ date.1 5 Mar 2005 10:01:09 -0000 @@ -33,10 +33,10 @@ .\" SUCH DAMAGE. .\" .\" @(#)date.1 8.3 (Berkeley) 4/28/95 -.\" $FreeBSD: src/bin/date/date.1,v 1.34.2.15 2003/02/24 03:01:00 trhodes Exp $ +.\" $FreeBSD: src/bin/date/date.1,v 1.72 2005/02/13 22:25:09 ru Exp $ .\" $DragonFly: src/bin/date/date.1,v 1.2 2003/06/17 04:22:49 dillon Exp $ .\" -.Dd November 17, 1993 +.Dd August 9, 2004 .Dt DATE 1 .Os .Sh NAME @@ -44,10 +44,8 @@ .Nd display or set date and time .Sh SYNOPSIS .Nm -.Op Fl jnu -.Op Fl d Ar dst +.Op Fl ju .Op Fl r Ar seconds -.Op Fl t Ar minutes_west .Oo .Fl v .Sm off @@ -56,15 +54,20 @@ .Sm on .Oc .Ar ... -.Oo -.Fl f -.Ar fmt date | +.Op Cm + Ns Ar output_fmt +.Nm +.Op Fl jnu .Sm off .Op Oo Oo Oo Oo Ar cc Oc Ar yy Oc Ar mm Oc Ar dd Oc Ar HH .Ar MM Op Ar .ss .Sm on -.Oc -.Op Cm + Ns Ar format +.Nm +.Op Fl jnu +.Fl f Ar input_fmt new_date +.Op Cm + Ns Ar output_fmt +.Nm +.Op Fl d Ar dst +.Op Fl t Ar minutes_west .Sh DESCRIPTION When invoked without arguments, the .Nm @@ -95,12 +98,12 @@ to .Xr gettimeofday 2 will return a non-zero for -.Fa tz_dsttime . +.Fa tz_dsttime . .It Fl f Use -.Ar fmt +.Ar input_fmt as the format string to parse the -.Ar date +.Ar new_date provided rather than using the default .Sm off .Oo Oo Oo Oo Oo @@ -211,7 +214,7 @@ .Fl v No +3H will be necessary to reach October 29, 2:30. .Pp -When the date is adjusted to a specific value that doesn't actually exist +When the date is adjusted to a specific value that does not actually exist (for example March 26, 1:30 BST 2000 in the Europe/London timezone), the date will be silently adjusted forwards in units of one hour until it reaches a valid time. @@ -220,6 +223,22 @@ the resulting timezone will be set so that the date matches the earlier of the two times. .Pp +Adjusting the date by months is inherently ambiguous because +a month is a unit of variable length depending on the current date. +This kind of date adjustment is applied in the most intuitive way. +First of all, +.Nm +tries to preserve the day of the month. +If it is impossible because the target month is shorter than the present one, +the last day of the target month will be the result. +For example, using +.Fl v No +1m +on May 31 will adjust the date to June 30, while using the same option +on January 30 will result in the date adjusted to the last day of February. +This approach is also believed to make the most sense for shell scripting. +Nevertheless, be aware that going forth and back by the same number of +months may take you to a different date. +.Pp Refer to the examples below for further details. .El .Pp @@ -249,7 +268,7 @@ prepended to the abbreviated year. .It Ar yy Year in abbreviated form -(e.g. 89 for 1989, 06 for 2006). +(e.g., 89 for 1989, 06 for 2006). .It Ar mm Numeric month, a number from 1 to 12. .It Ar dd @@ -267,6 +286,33 @@ .Pp Time changes for Daylight Saving Time, standard time, leap seconds, and leap years are handled automatically. +.Sh ENVIRONMENT +The following environment variables affect the execution of +.Nm : +.Bl -tag -width Ds +.It Ev TZ +The timezone to use when displaying dates. +The normal format is a pathname relative to +.Pa /usr/share/zoneinfo . +For example, the command +.Dq TZ=America/Los_Angeles date +displays the current time in California. +See +.Xr environ 7 +for more information. +.El +.Sh FILES +.Bl -tag -width /var/log/messages -compact +.It Pa /var/log/wtmp +record of date resets and time changes +.It Pa /var/log/messages +record of the user setting the time +.El +.Sh EXIT STATUS +The +.Nm +utility exits 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. .Sh EXAMPLES The command: .Pp @@ -286,7 +332,8 @@ .Pp .Dl "Sun Jan 4 04:15:24 GMT 1998" .Pp -where it is currently Mon Aug 4 04:15:24 BST 1997. +where it is currently +.Li "Mon Aug 4 04:15:24 BST 1997" . .Pp The command: .Pp @@ -296,6 +343,12 @@ .Pp .Dl "Tue Feb 29 03:18:00 GMT 2000" .Pp +So will do the command: +.Pp +.Dl "date -v30d -v3m -v0y -v-1m" +.Pp +because there is no such date as the 30th of February. +.Pp The command: .Pp .Dl "date -v1d -v+1m -v-1d -v-fri" @@ -304,7 +357,8 @@ .Pp .Dl "Fri Aug 29 04:31:11 BST 1997" .Pp -where it is currently Mon Aug 4 04:31:11 BST 1997. +where it is currently +.Li "Mon Aug 4 04:31:11 BST 1997" . .Pp The command: .Pp @@ -317,6 +371,9 @@ .Pp may be used on one machine to print out the date suitable for setting on another. +.Qq ( Li "+%m%d%H%M%Y.%S" +for use on +.Tn Linux . ) .Pp The command: .Pp @@ -325,45 +382,15 @@ sets the time to .Li "2:32 PM" , without modifying the date. -.Sh ENVIRONMENT -The following environment variables affect the execution of -.Nm : -.Bl -tag -width Ds -.It Ev TZ -The timezone to use when displaying dates. -The normal format is a pathname relative to -.Pa /usr/share/zoneinfo . -For example, the command -.Dq TZ=America/Los_Angeles date -displays the current time in California. -See -.Xr environ 7 -for more information. -.El -.Sh FILES -.Bl -tag -width /var/log/messages -compact -.It Pa /var/log/wtmp -record of date resets and time changes -.It Pa /var/log/messages -record of the user setting the time -.El -.Sh SEE ALSO -.Xr gettimeofday 2 , -.Xr strftime 3 , -.Xr strptime 3 , -.Xr utmp 5 , -.Xr timed 8 -.Rs -.%T "TSP: The Time Synchronization Protocol for UNIX 4.3BSD" -.%A R. Gusella -.%A S. Zatti -.Re -.Sh DIAGNOSTICS -The -.Nm -utility exits 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. .Pp +Finally the command: +.Pp +.Dl "date -j -f ""%a %b %d %T %Z %Y"" ""`date`"" ""+%s""" +.Pp +can be used to parse the output from +.Nm +and express it in Epoch time. +.Sh DIAGNOSTICS Occasionally, when .Xr timed 8 synchronizes the time on many hosts, the setting of a new time value may @@ -380,6 +407,17 @@ and .Xr timed 8 fails. +.Sh SEE ALSO +.Xr gettimeofday 2 , +.Xr strftime 3 , +.Xr strptime 3 , +.Xr utmp 5 , +.Xr timed 8 +.Rs +.%T "TSP: The Time Synchronization Protocol for UNIX 4.3BSD" +.%A R. Gusella +.%A S. Zatti +.Re .Sh STANDARDS The .Nm Index: date.c =================================================================== RCS file: /home/dcvs/src/bin/date/date.c,v retrieving revision 1.8 diff -u -r1.8 date.c --- date.c 7 Nov 2004 20:54:51 -0000 1.8 +++ date.c 19 Mar 2005 12:09:53 -0000 @@ -32,7 +32,7 @@ * * @(#) Copyright (c) 1985, 1987, 1988, 1993 The Regents of the University of California. All rights reserved. * @(#)date.c 8.2 (Berkeley) 4/28/95 - * $FreeBSD: src/bin/date/date.c,v 1.32.2.6 2001/10/31 17:31:51 dillon Exp $ + * $FreeBSD: src/bin/date/date.c,v 1.47 2005/01/10 08:39:21 imp Exp $$ * $DragonFly: src/bin/date/date.c,v 1.8 2004/11/07 20:54:51 eirikn Exp $ */ @@ -56,12 +56,12 @@ #define TM_YEAR_BASE 1900 #endif -time_t tval; +static time_t tval; int retval; -static void setthetime (const char *, const char *, int, int); -static void badformat (void); -static void usage (void); +static void setthetime(const char *, const char *, int, int); +static void badformat(void); +static void usage(void); int main(int argc, char **argv) @@ -86,7 +86,7 @@ jflag = nflag = 0; set_timezone = 0; while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:")) != -1) - switch((char)ch) { + switch(ch) { case 'd': /* daylight savings time */ tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0; if (endptr == optarg || *endptr != '\0') @@ -165,6 +165,8 @@ vary_destroy(v); strftime(buf, sizeof(buf), format, <); printf("%s\n", buf); + if (fflush(stdout)) + err(1, "stdout"); exit(retval); } Index: extern.h =================================================================== RCS file: /home/dcvs/src/bin/date/extern.h,v retrieving revision 1.3 diff -u -r1.3 extern.h --- extern.h 21 Sep 2003 04:19:10 -0000 1.3 +++ extern.h 19 Mar 2005 12:10:31 -0000 @@ -31,8 +31,8 @@ * SUCH DAMAGE. * * @(#)extern.h 8.1 (Berkeley) 5/31/93 - * $FreeBSD: src/bin/date/extern.h,v 1.5 1999/08/27 23:13:59 peter Exp $ + * $FreeBSD: src/bin/date/extern.h,v 1.7 2004/04/06 20:06:45 markm Exp $ * $DragonFly: src/bin/date/extern.h,v 1.3 2003/09/21 04:19:10 drhodus Exp $ */ -int netsettime (time_t); +int netsettime(time_t); Index: netdate.c =================================================================== RCS file: /home/dcvs/src/bin/date/netdate.c,v retrieving revision 1.6 diff -u -r1.6 netdate.c --- netdate.c 7 Nov 2004 20:54:51 -0000 1.6 +++ netdate.c 19 Mar 2005 12:32:18 -0000 @@ -31,7 +31,7 @@ * SUCH DAMAGE. * * @(#)netdate.c 8.1 (Berkeley) 5/31/93 - * $FreeBSD: src/bin/date/netdate.c,v 1.11.2.1 2001/05/12 17:14:22 fenner Exp $ + * $FreeBSD: src/bin/date/netdate.c,v 1.18 2004/04/06 20:06:45 markm Exp $ * $DragonFly: src/bin/date/netdate.c,v 1.6 2004/11/07 20:54:51 eirikn Exp $ */ @@ -77,7 +77,7 @@ char hostname[MAXHOSTNAMELEN]; if ((sp = getservbyname("timed", "udp")) == NULL) { - warnx("udp/timed: unknown service"); + warnx("timed/udp: unknown service"); return (retval = 2); } Index: vary.c =================================================================== RCS file: /home/dcvs/src/bin/date/vary.c,v retrieving revision 1.3 diff -u -r1.3 vary.c --- vary.c 19 Mar 2004 17:30:59 -0000 1.3 +++ vary.c 19 Mar 2005 12:32:51 -0000 @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $FreeBSD: src/bin/date/vary.c,v 1.8.2.2 2000/12/08 11:42:53 brian Exp $ + * $FreeBSD: src/bin/date/vary.c,v 1.16 2004/08/09 13:43:39 yar Exp $ * $DragonFly: src/bin/date/vary.c,v 1.3 2004/03/19 17:30:59 cpressey Exp $ */ @@ -148,6 +148,7 @@ static int adjmon(struct tm *t, char type, int val, int istext, int mk) { + int lmdays; if (val < 0) return 0; @@ -195,6 +196,11 @@ t->tm_mon = --val; } + /* e.g., -v-1m on March, 31 is the last day of February in common sense */ + lmdays = daysinmonth(t); + if (t->tm_mday > lmdays) + t->tm_mday = lmdays; + return !mk || domktime(t, type) != -1; }