Home | History | Annotate | Line # | Download | only in date
date.c revision 1.61.18.2
      1  1.61.18.2    martin /* $NetBSD: date.c,v 1.61.18.2 2024/10/15 18:34:40 martin Exp $ */
      2        1.9       cgd 
      3        1.1       cgd /*
      4        1.8   mycroft  * Copyright (c) 1985, 1987, 1988, 1993
      5        1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8        1.1       cgd  * modification, are permitted provided that the following conditions
      9        1.1       cgd  * are met:
     10        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.37       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1       cgd  *    may be used to endorse or promote products derived from this software
     17        1.1       cgd  *    without specific prior written permission.
     18        1.1       cgd  *
     19        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1       cgd  * SUCH DAMAGE.
     30        1.1       cgd  */
     31        1.1       cgd 
     32  1.61.18.1    martin #if HAVE_NBTOOL_CONFIG_H
     33  1.61.18.1    martin #include "nbtool_config.h"
     34  1.61.18.1    martin #endif
     35  1.61.18.1    martin 
     36       1.15   thorpej #include <sys/cdefs.h>
     37        1.1       cgd #ifndef lint
     38       1.15   thorpej __COPYRIGHT(
     39       1.52     lukem "@(#) Copyright (c) 1985, 1987, 1988, 1993\
     40       1.52     lukem  The Regents of the University of California.  All rights reserved.");
     41        1.1       cgd #endif /* not lint */
     42        1.1       cgd 
     43        1.1       cgd #ifndef lint
     44       1.10       cgd #if 0
     45       1.11       jtc static char sccsid[] = "@(#)date.c	8.2 (Berkeley) 4/28/95";
     46       1.10       cgd #else
     47  1.61.18.2    martin __RCSID("$NetBSD: date.c,v 1.61.18.2 2024/10/15 18:34:40 martin Exp $");
     48       1.10       cgd #endif
     49        1.1       cgd #endif /* not lint */
     50        1.1       cgd 
     51        1.1       cgd #include <sys/param.h>
     52        1.1       cgd #include <sys/time.h>
     53        1.8   mycroft 
     54        1.8   mycroft #include <ctype.h>
     55        1.8   mycroft #include <err.h>
     56        1.8   mycroft #include <fcntl.h>
     57       1.59  christos #include <errno.h>
     58       1.34       wiz #include <locale.h>
     59        1.1       cgd #include <stdio.h>
     60        1.1       cgd #include <stdlib.h>
     61        1.1       cgd #include <string.h>
     62        1.8   mycroft #include <syslog.h>
     63       1.22    kleink #include <time.h>
     64       1.18   mycroft #include <tzfile.h>
     65        1.8   mycroft #include <unistd.h>
     66       1.14       cgd #include <util.h>
     67  1.61.18.1    martin #if !HAVE_NBTOOL_CONFIG_H
     68  1.61.18.1    martin #include <utmpx.h>
     69  1.61.18.1    martin #endif
     70        1.8   mycroft 
     71        1.8   mycroft #include "extern.h"
     72        1.1       cgd 
     73       1.38       dsl static time_t tval;
     74  1.61.18.1    martin static int Rflag, aflag, jflag, rflag, nflag;
     75        1.1       cgd 
     76       1.61  dholland __dead static void badcanotime(const char *, const char *, size_t);
     77       1.34       wiz static void setthetime(const char *);
     78       1.60     joerg __dead static void usage(void);
     79        1.8   mycroft 
     80  1.61.18.2    martin #if HAVE_NBTOOL_CONFIG_H
     81  1.61.18.2    martin static int parse_iso_datetime(time_t *, const char *);
     82  1.61.18.2    martin #else
     83  1.61.18.2    martin static char *fmt;
     84  1.61.18.2    martin #endif
     85  1.61.18.2    martin 
     86  1.61.18.2    martin #if !defined(isleap)
     87  1.61.18.2    martin # define isleap(y)   (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
     88  1.61.18.2    martin #endif
     89  1.61.18.2    martin 
     90        1.8   mycroft int
     91       1.34       wiz main(int argc, char *argv[])
     92        1.1       cgd {
     93       1.43  christos 	char *buf;
     94       1.43  christos 	size_t bufsiz;
     95       1.40  christos 	const char *format;
     96       1.38       dsl 	int ch;
     97       1.59  christos 	long long val;
     98       1.59  christos 	struct tm *tm;
     99        1.1       cgd 
    100       1.35       wiz 	setprogname(argv[0]);
    101       1.16       cgd 	(void)setlocale(LC_ALL, "");
    102        1.7       jtc 
    103  1.61.18.1    martin 	while ((ch = getopt(argc, argv, "ad:f:jnRr:u")) != -1) {
    104       1.42     peter 		switch (ch) {
    105       1.38       dsl 		case 'a':		/* adjust time slowly */
    106       1.38       dsl 			aflag = 1;
    107       1.38       dsl 			nflag = 1;
    108       1.38       dsl 			break;
    109       1.47  christos 		case 'd':
    110       1.47  christos 			rflag = 1;
    111  1.61.18.2    martin #ifdef HAVE_NBTOOL_CONFIG_H
    112  1.61.18.2    martin 			if (parse_iso_datetime(&tval, optarg))
    113  1.61.18.2    martin 				break;
    114  1.61.18.2    martin 			errx(EXIT_FAILURE,
    115  1.61.18.2    martin 			    "-d only supports ISO format in the tool version");
    116  1.61.18.2    martin 			break;
    117  1.61.18.2    martin #else
    118  1.61.18.2    martin 			errno = 0;
    119       1.49  christos 			tval = parsedate(optarg, NULL, NULL);
    120  1.61.18.2    martin 			if (tval == -1 && errno != 0) {
    121       1.61  dholland 				errx(EXIT_FAILURE,
    122       1.61  dholland 				    "%s: Unrecognized date format", optarg);
    123       1.61  dholland 			}
    124       1.47  christos 			break;
    125  1.61.18.1    martin 		case 'f':
    126  1.61.18.1    martin 			fmt = optarg;
    127  1.61.18.1    martin 			break;
    128  1.61.18.2    martin #endif
    129       1.46   jdarrow 		case 'j':		/* don't set time */
    130       1.46   jdarrow 			jflag = 1;
    131       1.46   jdarrow 			break;
    132        1.1       cgd 		case 'n':		/* don't set network */
    133        1.1       cgd 			nflag = 1;
    134        1.1       cgd 			break;
    135  1.61.18.1    martin 		case 'R':		/* RFC-5322 email format */
    136  1.61.18.1    martin 			Rflag = 1;
    137  1.61.18.1    martin 			break;
    138        1.1       cgd 		case 'r':		/* user specified seconds */
    139       1.61  dholland 			if (optarg[0] == '\0') {
    140       1.61  dholland 				errx(EXIT_FAILURE, "<empty>: Invalid number");
    141       1.61  dholland 			}
    142       1.59  christos 			errno = 0;
    143       1.59  christos 			val = strtoll(optarg, &buf, 0);
    144       1.61  dholland 			if (errno) {
    145       1.61  dholland 				err(EXIT_FAILURE, "%s", optarg);
    146       1.61  dholland 			}
    147       1.61  dholland 			if (optarg[0] == '\0' || *buf != '\0') {
    148       1.61  dholland 				errx(EXIT_FAILURE,
    149       1.61  dholland 				    "%s: Invalid number", optarg);
    150       1.61  dholland 			}
    151        1.1       cgd 			rflag = 1;
    152       1.59  christos 			tval = (time_t)val;
    153        1.1       cgd 			break;
    154       1.27   mycroft 		case 'u':		/* do everything in UTC */
    155       1.55      gson 			(void)setenv("TZ", "UTC0", 1);
    156        1.1       cgd 			break;
    157        1.1       cgd 		default:
    158        1.1       cgd 			usage();
    159        1.1       cgd 		}
    160       1.38       dsl 	}
    161        1.1       cgd 	argc -= optind;
    162        1.1       cgd 	argv += optind;
    163        1.1       cgd 
    164        1.8   mycroft 	if (!rflag && time(&tval) == -1)
    165       1.42     peter 		err(EXIT_FAILURE, "time");
    166        1.1       cgd 
    167        1.1       cgd 
    168        1.1       cgd 	/* allow the operands in any order */
    169        1.1       cgd 	if (*argv && **argv == '+') {
    170       1.53  dholland 		format = *argv;
    171        1.1       cgd 		++argv;
    172  1.61.18.1    martin 	} else if (Rflag) {
    173  1.61.18.1    martin 		(void)setlocale(LC_TIME, "C");
    174  1.61.18.1    martin 		format = "+%a, %-e %b %Y %H:%M:%S %z";
    175       1.59  christos 	} else
    176       1.59  christos 		format = "+%a %b %e %H:%M:%S %Z %Y";
    177        1.1       cgd 
    178        1.1       cgd 	if (*argv) {
    179        1.1       cgd 		setthetime(*argv);
    180        1.1       cgd 		++argv;
    181  1.61.18.2    martin #ifndef HAVE_NBTOOL_CONFIG_H
    182  1.61.18.2    martin 	} else if (fmt) {
    183  1.61.18.1    martin 		usage();
    184  1.61.18.2    martin #endif
    185  1.61.18.2    martin 	}
    186        1.1       cgd 
    187        1.1       cgd 	if (*argv && **argv == '+')
    188       1.53  dholland 		format = *argv;
    189        1.1       cgd 
    190       1.43  christos 	if ((buf = malloc(bufsiz = 1024)) == NULL)
    191       1.43  christos 		goto bad;
    192       1.59  christos 
    193       1.59  christos 	if ((tm = localtime(&tval)) == NULL)
    194       1.61  dholland 		err(EXIT_FAILURE, "%lld: localtime", (long long)tval);
    195       1.59  christos 
    196       1.59  christos 	while (strftime(buf, bufsiz, format, tm) == 0)
    197       1.43  christos 		if ((buf = realloc(buf, bufsiz <<= 1)) == NULL)
    198       1.43  christos 			goto bad;
    199       1.59  christos 
    200       1.59  christos 	(void)printf("%s\n", buf + 1);
    201       1.43  christos 	free(buf);
    202       1.43  christos 	return 0;
    203       1.43  christos bad:
    204       1.59  christos 	err(EXIT_FAILURE, "Cannot allocate format buffer");
    205        1.1       cgd }
    206        1.1       cgd 
    207       1.34       wiz static void
    208       1.61  dholland badcanotime(const char *msg, const char *val, size_t where)
    209       1.34       wiz {
    210       1.61  dholland 	warnx("%s in canonical time", msg);
    211       1.61  dholland 	warnx("%s", val);
    212       1.61  dholland 	warnx("%*s", (int)where + 1, "^");
    213       1.45      elad 	usage();
    214       1.45      elad }
    215       1.45      elad 
    216       1.34       wiz #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
    217       1.21   mycroft 
    218  1.61.18.2    martin #if HAVE_NBTOOL_CONFIG_H
    219  1.61.18.2    martin 
    220  1.61.18.2    martin inline static int
    221  1.61.18.2    martin digitstring(const char *s, int len)
    222  1.61.18.2    martin {
    223  1.61.18.2    martin 	while (--len > 0) {
    224  1.61.18.2    martin 		if (!isdigit(*(unsigned char *)s))
    225  1.61.18.2    martin 			return 0;
    226  1.61.18.2    martin 		s++;
    227  1.61.18.2    martin 	}
    228  1.61.18.2    martin 	return 1;
    229  1.61.18.2    martin }
    230  1.61.18.2    martin 
    231  1.61.18.2    martin static int
    232  1.61.18.2    martin parse_iso_datetime(time_t * res, const char * string)
    233  1.61.18.2    martin {
    234  1.61.18.2    martin 	struct tm tm;
    235  1.61.18.2    martin 	time_t t;
    236  1.61.18.2    martin 
    237  1.61.18.2    martin 	memset(&tm, 0, sizeof tm);
    238  1.61.18.2    martin 
    239  1.61.18.2    martin 	if (!digitstring(string, 4))
    240  1.61.18.2    martin 		return 0;
    241  1.61.18.2    martin 	tm.tm_year = ATOI2(string) * 100;
    242  1.61.18.2    martin 	tm.tm_year += ATOI2(string);
    243  1.61.18.2    martin 	tm.tm_year -= 1900;
    244  1.61.18.2    martin 
    245  1.61.18.2    martin 	if (*string == '-')
    246  1.61.18.2    martin 		string++;
    247  1.61.18.2    martin 
    248  1.61.18.2    martin 	if (!digitstring(string, 2))
    249  1.61.18.2    martin 		return 0;
    250  1.61.18.2    martin 
    251  1.61.18.2    martin 	tm.tm_mon = ATOI2(string);
    252  1.61.18.2    martin 	if (tm.tm_mon < 1 || tm.tm_mon > 12)
    253  1.61.18.2    martin 		return 0;
    254  1.61.18.2    martin 	tm.tm_mon--;
    255  1.61.18.2    martin 
    256  1.61.18.2    martin 	if (*string == '-')
    257  1.61.18.2    martin 		string++;
    258  1.61.18.2    martin 
    259  1.61.18.2    martin 	if (!digitstring(string, 2))
    260  1.61.18.2    martin 		return 0;
    261  1.61.18.2    martin 
    262  1.61.18.2    martin 	tm.tm_mday = ATOI2(string);
    263  1.61.18.2    martin 	if (tm.tm_mday < 1)
    264  1.61.18.2    martin 		return 0;
    265  1.61.18.2    martin 	switch (tm.tm_mon) {
    266  1.61.18.2    martin 	case 0: case 2: case 4: case 6: case 7: case 9: case 11:
    267  1.61.18.2    martin 		if (tm.tm_mday > 31)
    268  1.61.18.2    martin 			return 0;
    269  1.61.18.2    martin 		break;
    270  1.61.18.2    martin 	case 3: case 5: case 8: case 10:
    271  1.61.18.2    martin 		if (tm.tm_mday > 30)
    272  1.61.18.2    martin 			return 0;
    273  1.61.18.2    martin 		break;
    274  1.61.18.2    martin 	case 1:
    275  1.61.18.2    martin 		if (tm.tm_mday > 28 + isleap(tm.tm_year + 1900))
    276  1.61.18.2    martin 			return 0;
    277  1.61.18.2    martin 		break;
    278  1.61.18.2    martin 	default:
    279  1.61.18.2    martin 		abort();
    280  1.61.18.2    martin 	}
    281  1.61.18.2    martin 
    282  1.61.18.2    martin 	do {
    283  1.61.18.2    martin 		if (*string == '\0')
    284  1.61.18.2    martin 			break;
    285  1.61.18.2    martin 		if (*string == 'T' || *string == 't' || *string == ' ' ||
    286  1.61.18.2    martin 		    *string == '-')
    287  1.61.18.2    martin 			string++;
    288  1.61.18.2    martin 
    289  1.61.18.2    martin 		if (!digitstring(string, 2))
    290  1.61.18.2    martin 			return 0;
    291  1.61.18.2    martin 		tm.tm_hour = ATOI2(string);
    292  1.61.18.2    martin 		if (tm.tm_hour > 23)
    293  1.61.18.2    martin 			return 0;
    294  1.61.18.2    martin 
    295  1.61.18.2    martin 		if (*string == '\0')
    296  1.61.18.2    martin 			break;
    297  1.61.18.2    martin 		if (*string == ':')
    298  1.61.18.2    martin 			string++;
    299  1.61.18.2    martin 
    300  1.61.18.2    martin 		if (!digitstring(string, 2))
    301  1.61.18.2    martin 			return 0;
    302  1.61.18.2    martin 		tm.tm_min = ATOI2(string);
    303  1.61.18.2    martin 		if (tm.tm_min >= 60)
    304  1.61.18.2    martin 			return 0;
    305  1.61.18.2    martin 
    306  1.61.18.2    martin 		if (*string == '\0')
    307  1.61.18.2    martin 			break;
    308  1.61.18.2    martin 		if (*string == ':')
    309  1.61.18.2    martin 			string++;
    310  1.61.18.2    martin 
    311  1.61.18.2    martin 		if (!digitstring(string, 2))
    312  1.61.18.2    martin 			return 0;
    313  1.61.18.2    martin 		tm.tm_sec = ATOI2(string);
    314  1.61.18.2    martin 		if (tm.tm_sec >= 60)
    315  1.61.18.2    martin 			return 0;
    316  1.61.18.2    martin 	} while (0);
    317  1.61.18.2    martin 
    318  1.61.18.2    martin 	if (*string != '\0')
    319  1.61.18.2    martin 		return 0;
    320  1.61.18.2    martin 
    321  1.61.18.2    martin 	tm.tm_isdst = -1;
    322  1.61.18.2    martin 	tm.tm_wday = -1;
    323  1.61.18.2    martin 
    324  1.61.18.2    martin 	t = mktime(&tm);
    325  1.61.18.2    martin 	if (tm.tm_wday == -1)
    326  1.61.18.2    martin 		return 0;
    327  1.61.18.2    martin 
    328  1.61.18.2    martin 	*res = t;
    329  1.61.18.2    martin 	return 1;
    330  1.61.18.2    martin }
    331  1.61.18.2    martin 
    332  1.61.18.2    martin #endif	/*NBTOOL*/
    333  1.61.18.2    martin 
    334       1.33  gmcgarry static void
    335       1.34       wiz setthetime(const char *p)
    336        1.1       cgd {
    337       1.34       wiz 	struct timeval tv;
    338       1.38       dsl 	time_t new_time;
    339       1.12       tls 	struct tm *lt;
    340       1.61  dholland 	const char *dot, *t, *op;
    341       1.50    cbiere 	size_t len;
    342       1.50    cbiere 	int yearset;
    343        1.1       cgd 
    344  1.61.18.1    martin 	if ((lt = localtime(&tval)) == NULL)
    345  1.61.18.1    martin 		err(EXIT_FAILURE, "%lld: localtime", (long long)tval);
    346  1.61.18.1    martin 
    347  1.61.18.1    martin 	lt->tm_isdst = -1;			/* Divine correct DST */
    348  1.61.18.1    martin 
    349  1.61.18.2    martin #ifndef HAVE_NBTOOL_CONFIG_H
    350  1.61.18.1    martin 	if (fmt) {
    351  1.61.18.1    martin 		t = strptime(p, fmt, lt);
    352  1.61.18.1    martin 		if (t == NULL) {
    353  1.61.18.1    martin 			warnx("Failed conversion of ``%s''"
    354  1.61.18.1    martin 			    " using format ``%s''\n", p, fmt);
    355  1.61.18.1    martin 		} else if (*t != '\0')
    356  1.61.18.1    martin 			warnx("Ignoring %zu extraneous"
    357  1.61.18.1    martin 				" characters in date string (%s)",
    358  1.61.18.1    martin 				strlen(t), t);
    359  1.61.18.1    martin 		goto setit;
    360  1.61.18.1    martin 	}
    361  1.61.18.2    martin #endif
    362        1.8   mycroft 	for (t = p, dot = NULL; *t; ++t) {
    363       1.61  dholland 		if (*t == '.') {
    364       1.61  dholland 			if (dot == NULL) {
    365       1.61  dholland 				dot = t;
    366       1.61  dholland 			} else {
    367       1.61  dholland 				badcanotime("Unexpected dot", p, t - p);
    368       1.61  dholland 			}
    369       1.61  dholland 		} else if (!isdigit((unsigned char)*t)) {
    370       1.61  dholland 			badcanotime("Expected digit", p, t - p);
    371        1.8   mycroft 		}
    372        1.8   mycroft 	}
    373        1.1       cgd 
    374        1.1       cgd 
    375        1.8   mycroft 	if (dot != NULL) {			/* .ss */
    376       1.23   mycroft 		len = strlen(dot);
    377       1.61  dholland 		if (len > 3) {
    378       1.61  dholland 			badcanotime("Unexpected digit after seconds field",
    379       1.61  dholland 				    p, strlen(p) - 1);
    380       1.61  dholland 		} else if (len < 3) {
    381       1.61  dholland 			badcanotime("Expected digit in seconds field",
    382       1.61  dholland 				    p, strlen(p));
    383       1.61  dholland 		}
    384       1.23   mycroft 		++dot;
    385        1.8   mycroft 		lt->tm_sec = ATOI2(dot);
    386       1.45      elad 		if (lt->tm_sec > 61)
    387       1.61  dholland 			badcanotime("Seconds out of range", p, strlen(p) - 1);
    388       1.23   mycroft 	} else {
    389       1.23   mycroft 		len = 0;
    390        1.1       cgd 		lt->tm_sec = 0;
    391       1.23   mycroft 	}
    392        1.1       cgd 
    393       1.61  dholland 	op = p;
    394       1.18   mycroft 	yearset = 0;
    395       1.23   mycroft 	switch (strlen(p) - len) {
    396       1.21   mycroft 	case 12:				/* cc */
    397       1.21   mycroft 		lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
    398       1.45      elad 		if (lt->tm_year < 0)
    399       1.61  dholland 			badcanotime("Year before 1900", op, p - op + 1);
    400       1.18   mycroft 		yearset = 1;
    401       1.18   mycroft 		/* FALLTHROUGH */
    402        1.1       cgd 	case 10:				/* yy */
    403       1.18   mycroft 		if (yearset) {
    404       1.21   mycroft 			lt->tm_year += ATOI2(p);
    405       1.18   mycroft 		} else {
    406       1.18   mycroft 			yearset = ATOI2(p);
    407       1.21   mycroft 			if (yearset < 69)
    408       1.21   mycroft 				lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
    409       1.18   mycroft 			else
    410       1.21   mycroft 				lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
    411       1.18   mycroft 		}
    412        1.1       cgd 		/* FALLTHROUGH */
    413        1.1       cgd 	case 8:					/* mm */
    414        1.1       cgd 		lt->tm_mon = ATOI2(p);
    415       1.45      elad 		if (lt->tm_mon > 12 || lt->tm_mon == 0)
    416       1.61  dholland 			badcanotime("Month out of range", op, p - op - 1);
    417        1.1       cgd 		--lt->tm_mon;			/* time struct is 0 - 11 */
    418        1.1       cgd 		/* FALLTHROUGH */
    419        1.1       cgd 	case 6:					/* dd */
    420        1.1       cgd 		lt->tm_mday = ATOI2(p);
    421       1.45      elad 		switch (lt->tm_mon) {
    422       1.45      elad 		case 0:
    423       1.45      elad 		case 2:
    424       1.45      elad 		case 4:
    425       1.45      elad 		case 6:
    426       1.45      elad 		case 7:
    427       1.45      elad 		case 9:
    428       1.45      elad 		case 11:
    429       1.45      elad 			if (lt->tm_mday > 31 || lt->tm_mday == 0)
    430       1.61  dholland 				badcanotime("Day out of range (max 31)",
    431       1.61  dholland 					    op, p - op - 1);
    432       1.45      elad 			break;
    433       1.45      elad 		case 3:
    434       1.45      elad 		case 5:
    435       1.45      elad 		case 8:
    436       1.45      elad 		case 10:
    437       1.45      elad 			if (lt->tm_mday > 30 || lt->tm_mday == 0)
    438       1.61  dholland 				badcanotime("Day out of range (max 30)",
    439       1.61  dholland 					    op, p - op - 1);
    440       1.45      elad 			break;
    441       1.45      elad 		case 1:
    442       1.61  dholland 			if (isleap(lt->tm_year + TM_YEAR_BASE)) {
    443       1.61  dholland 				if (lt->tm_mday > 29 || lt->tm_mday == 0) {
    444       1.61  dholland 					badcanotime("Day out of range "
    445       1.61  dholland 						    "(max 29)",
    446       1.61  dholland 						    op, p - op - 1);
    447       1.61  dholland 				}
    448       1.61  dholland 			} else {
    449       1.61  dholland 				if (lt->tm_mday > 28 || lt->tm_mday == 0) {
    450       1.61  dholland 					badcanotime("Day out of range "
    451       1.61  dholland 						    "(max 28)",
    452       1.61  dholland 						    op, p - op - 1);
    453       1.61  dholland 				}
    454       1.61  dholland 			}
    455       1.45      elad 			break;
    456       1.45      elad 		default:
    457       1.61  dholland 			/*
    458       1.61  dholland 			 * If the month was given, it's already been
    459       1.61  dholland 			 * checked.  If a bad value came back from
    460       1.61  dholland 			 * localtime, something's badly broken.
    461       1.61  dholland 			 * (make this an assertion?)
    462       1.61  dholland 			 */
    463       1.61  dholland 			errx(EXIT_FAILURE, "localtime gave invalid month %d",
    464       1.61  dholland 			    lt->tm_mon);
    465       1.45      elad 		}
    466        1.1       cgd 		/* FALLTHROUGH */
    467        1.1       cgd 	case 4:					/* hh */
    468        1.1       cgd 		lt->tm_hour = ATOI2(p);
    469       1.45      elad 		if (lt->tm_hour > 23)
    470       1.61  dholland 			badcanotime("Hour out of range", op, p - op - 1);
    471        1.1       cgd 		/* FALLTHROUGH */
    472        1.1       cgd 	case 2:					/* mm */
    473        1.1       cgd 		lt->tm_min = ATOI2(p);
    474       1.45      elad 		if (lt->tm_min > 59)
    475       1.61  dholland 			badcanotime("Minute out of range", op, p - op - 1);
    476        1.1       cgd 		break;
    477       1.38       dsl 	case 0:					/* was just .sss */
    478       1.38       dsl 		if (len != 0)
    479       1.38       dsl 			break;
    480       1.38       dsl 		/* FALLTHROUGH */
    481        1.1       cgd 	default:
    482       1.61  dholland 	    if (strlen(p) - len > 12) {
    483       1.61  dholland 		    badcanotime("Too many digits", p, 12);
    484       1.61  dholland 	    } else {
    485       1.61  dholland 		    badcanotime("Not enough digits", p, strlen(p) - len);
    486       1.61  dholland 	    }
    487        1.1       cgd 	}
    488  1.61.18.1    martin setit:
    489       1.27   mycroft 	/* convert broken-down time to UTC clock time */
    490       1.61  dholland 	if ((new_time = mktime(lt)) == -1) {
    491       1.61  dholland 		/* Can this actually happen? */
    492  1.61.18.1    martin 		err(EXIT_FAILURE, "mktime");
    493       1.61  dholland 	}
    494        1.1       cgd 
    495       1.46   jdarrow 	/* if jflag is set, don't actually change the time, just return */
    496       1.46   jdarrow 	if (jflag) {
    497       1.46   jdarrow 		tval = new_time;
    498       1.46   jdarrow 		return;
    499       1.46   jdarrow 	}
    500       1.46   jdarrow 
    501        1.1       cgd 	/* set the time */
    502  1.61.18.1    martin #ifndef HAVE_NBTOOL_CONFIG_H
    503  1.61.18.1    martin 	struct utmpx utx;
    504  1.61.18.1    martin 	memset(&utx, 0, sizeof(utx));
    505  1.61.18.1    martin 	utx.ut_type = OLD_TIME;
    506  1.61.18.1    martin 	(void)gettimeofday(&utx.ut_tv, NULL);
    507  1.61.18.1    martin 	pututxline(&utx);
    508  1.61.18.1    martin 
    509       1.38       dsl 	if (nflag || netsettime(new_time)) {
    510        1.1       cgd 		logwtmp("|", "date", "");
    511       1.38       dsl 		if (aflag) {
    512       1.38       dsl 			tv.tv_sec = new_time - tval;
    513       1.38       dsl 			tv.tv_usec = 0;
    514       1.41   hubertf 			if (adjtime(&tv, NULL))
    515       1.44   jnemeth 				err(EXIT_FAILURE, "adjtime");
    516       1.38       dsl 		} else {
    517       1.39       dsl 			tval = new_time;
    518       1.38       dsl 			tv.tv_sec = tval;
    519       1.38       dsl 			tv.tv_usec = 0;
    520       1.41   hubertf 			if (settimeofday(&tv, NULL))
    521       1.44   jnemeth 				err(EXIT_FAILURE, "settimeofday");
    522        1.1       cgd 		}
    523        1.1       cgd 		logwtmp("{", "date", "");
    524        1.1       cgd 	}
    525  1.61.18.1    martin 	utx.ut_type = NEW_TIME;
    526  1.61.18.1    martin 	(void)gettimeofday(&utx.ut_tv, NULL);
    527  1.61.18.1    martin 	pututxline(&utx);
    528        1.8   mycroft 
    529        1.8   mycroft 	if ((p = getlogin()) == NULL)
    530        1.8   mycroft 		p = "???";
    531        1.8   mycroft 	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
    532  1.61.18.1    martin #else
    533  1.61.18.1    martin 	errx(EXIT_FAILURE, "Can't set the time in the tools version");
    534  1.61.18.1    martin #endif
    535        1.1       cgd }
    536        1.1       cgd 
    537        1.8   mycroft static void
    538       1.34       wiz usage(void)
    539        1.1       cgd {
    540        1.1       cgd 	(void)fprintf(stderr,
    541  1.61.18.1    martin 	    "Usage: %s [-ajnRu] [-d date] [-r seconds] [+format]",
    542       1.59  christos 	    getprogname());
    543       1.46   jdarrow 	(void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");
    544  1.61.18.1    martin 	(void)fprintf(stderr,
    545  1.61.18.1    martin 	    "       %s [-ajnRu] -f input_format new_date [+format]\n",
    546  1.61.18.1    martin 	    getprogname());
    547       1.41   hubertf 	exit(EXIT_FAILURE);
    548       1.24   mycroft 	/* NOTREACHED */
    549        1.1       cgd }
    550