stime.c revision 1.6
11.6Schristos/*	$NetBSD: stime.c,v 1.6 2008/04/05 16:26:57 christos 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.6Schristos__RCSID("$NetBSD: stime.c,v 1.6 2008/04/05 16:26:57 christos Exp $");
431.1Skleink#endif /* not lint */
441.1Skleink
451.1Skleink#include <err.h>
461.6Schristos#include <stdlib.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.6Schristos		err(EXIT_FAILURE, "time");
651.1Skleink
661.1Skleink	if ((t = localtime(&tmptime)) == NULL)
671.6Schristos		err(EXIT_FAILURE, "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.6Schristos
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.5Stsutsui		/* FALLTHROUGH */
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.6Schristos			t->tm_year = conv_2dig_year(yearset) - TM_YEAR_BASE;
901.1Skleink		}
911.1Skleink		/* FALLTHROUGH */
921.1Skleink	case 8:				/* MMDDhhmm */
931.1Skleink		t->tm_mon = ATOI2(arg);
941.1Skleink		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
951.1Skleink		/* FALLTHROUGH */
961.1Skleink	case 6:
971.1Skleink		t->tm_mday = ATOI2(arg);
981.1Skleink		/* FALLTHROUGH */
991.1Skleink	case 4:
1001.1Skleink		t->tm_hour = ATOI2(arg);
1011.1Skleink		/* FALLTHROUGH */
1021.1Skleink	case 2:
1031.1Skleink		t->tm_min = ATOI2(arg);
1041.1Skleink		break;
1051.1Skleink	default:
1061.1Skleink		goto terr;
1071.1Skleink	}
1081.1Skleink
1091.1Skleink	t->tm_isdst = -1;		/* Figure out DST. */
1101.1Skleink	tmptime = mktime(t);
1111.6Schristos	if (tmptime == (time_t)-1) {
1121.6Schristos terr:		errx(EXIT_FAILURE,
1131.6Schristos     "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
1141.6Schristos	}
1151.6Schristos	return tmptime;
1161.1Skleink}
117