stime.c revision 1.4
11.4Sxtraeme/*	$NetBSD: stime.c,v 1.4 2005/02/17 16:56:10 xtraeme Exp $	*/
21.1Skleink
31.1Skleink/*
41.1Skleink * Copyright (c) 1993
51.1Skleink *	The Regents of the University of California.  All rights reserved.
61.1Skleink *
71.1Skleink * Redistribution and use in source and binary forms, with or without
81.1Skleink * modification, are permitted provided that the following conditions
91.1Skleink * are met:
101.1Skleink * 1. Redistributions of source code must retain the above copyright
111.1Skleink *    notice, this list of conditions and the following disclaimer.
121.1Skleink * 2. Redistributions in binary form must reproduce the above copyright
131.1Skleink *    notice, this list of conditions and the following disclaimer in the
141.1Skleink *    documentation and/or other materials provided with the distribution.
151.3Sagc * 3. Neither the name of the University nor the names of its contributors
161.1Skleink *    may be used to endorse or promote products derived from this software
171.1Skleink *    without specific prior written permission.
181.1Skleink *
191.1Skleink * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201.1Skleink * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211.1Skleink * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221.1Skleink * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231.1Skleink * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241.1Skleink * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251.1Skleink * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261.1Skleink * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271.1Skleink * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281.1Skleink * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291.1Skleink * SUCH DAMAGE.
301.1Skleink */
311.1Skleink
321.1Skleink#include <sys/cdefs.h>
331.1Skleink#ifndef lint
341.1Skleink__COPYRIGHT("@(#) Copyright (c) 1993\n\
351.1Skleink	The Regents of the University of California.  All rights reserved.\n");
361.1Skleink#endif /* not lint */
371.1Skleink
381.1Skleink#ifndef lint
391.1Skleink#if 0
401.1Skleinkstatic char sccsid[] = "from: @(#)touch.c	8.2 (Berkeley) 4/28/95";
411.1Skleink#endif
421.4Sxtraeme__RCSID("$NetBSD: stime.c,v 1.4 2005/02/17 16:56:10 xtraeme Exp $");
431.1Skleink#endif /* not lint */
441.1Skleink
451.1Skleink
461.1Skleink#include <err.h>
471.1Skleink#include <string.h>
481.1Skleink#include <time.h>
491.1Skleink#include <tzfile.h>
501.1Skleink
511.1Skleink#include "stime.h"
521.1Skleink
531.1Skleink#define	ATOI2(s)	((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
541.1Skleink
551.1Skleinktime_t
561.4Sxtraemestime(char *arg)
571.1Skleink{
581.1Skleink	struct tm *t;
591.1Skleink	time_t tmptime;
601.1Skleink	int yearset;
611.1Skleink	char *p;
621.1Skleink					/* Start with the current time. */
631.1Skleink	if (time(&tmptime) == (time_t)-1)
641.1Skleink		err(1, "time");
651.1Skleink
661.1Skleink	if ((t = localtime(&tmptime)) == NULL)
671.1Skleink		err(1, "localtime");
681.1Skleink					/* [[CC]YY]MMDDhhmm[.SS] */
691.1Skleink	if ((p = strchr(arg, '.')) == NULL)
701.1Skleink		t->tm_sec = 0;		/* Seconds defaults to 0. */
711.1Skleink	else {
721.1Skleink		if (strlen(p + 1) != 2)
731.1Skleink			goto terr;
741.1Skleink		*p++ = '\0';
751.1Skleink		t->tm_sec = ATOI2(p);
761.1Skleink	}
771.1Skleink
781.1Skleink	yearset = 0;
791.1Skleink	switch (strlen(arg)) {
801.1Skleink	case 12:			/* CCYYMMDDhhmm */
811.1Skleink		t->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE;
821.1Skleink		yearset = 1;
831.1Skleink		/* FALLTHOUGH */
841.1Skleink	case 10:			/* YYMMDDhhmm */
851.1Skleink		if (yearset) {
861.1Skleink			t->tm_year += ATOI2(arg);
871.1Skleink		} else {
881.1Skleink			yearset = ATOI2(arg);
891.1Skleink			if (yearset < 69)
901.1Skleink				t->tm_year = yearset + 2000 - TM_YEAR_BASE;
911.1Skleink			else
921.1Skleink				t->tm_year = yearset + 1900 - TM_YEAR_BASE;
931.1Skleink		}
941.1Skleink		/* FALLTHROUGH */
951.1Skleink	case 8:				/* MMDDhhmm */
961.1Skleink		t->tm_mon = ATOI2(arg);
971.1Skleink		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
981.1Skleink		/* FALLTHROUGH */
991.1Skleink	case 6:
1001.1Skleink		t->tm_mday = ATOI2(arg);
1011.1Skleink		/* FALLTHROUGH */
1021.1Skleink	case 4:
1031.1Skleink		t->tm_hour = ATOI2(arg);
1041.1Skleink		/* FALLTHROUGH */
1051.1Skleink	case 2:
1061.1Skleink		t->tm_min = ATOI2(arg);
1071.1Skleink		break;
1081.1Skleink	default:
1091.1Skleink		goto terr;
1101.1Skleink	}
1111.1Skleink
1121.1Skleink	t->tm_isdst = -1;		/* Figure out DST. */
1131.1Skleink	tmptime = mktime(t);
1141.1Skleink	if (tmptime == (time_t)-1)
1151.1Skleinkterr:		errx(1,
1161.1Skleink	"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
1171.1Skleink
1181.1Skleink	return (tmptime);
1191.1Skleink}
120