xref: /onnv-gate/usr/src/cmd/backup/dump/unctime.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 1998,2001 by Sun Microsystems, Inc.
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
11*0Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
12*0Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
13*0Sstevel@tonic-gate  */
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate #include <sys/types.h>
18*0Sstevel@tonic-gate #include <time.h>
19*0Sstevel@tonic-gate #include <string.h>
20*0Sstevel@tonic-gate #include <stdlib.h>
21*0Sstevel@tonic-gate /*
22*0Sstevel@tonic-gate  * Convert a ctime(3) format string into a system format date.
23*0Sstevel@tonic-gate  * Return the date thus calculated.
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * Return -1 if the string is not in ctime format.
26*0Sstevel@tonic-gate  */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate  * Offsets into the ctime string to various parts.
30*0Sstevel@tonic-gate  */
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #define	E_MONTH		4
33*0Sstevel@tonic-gate #define	E_DAY		8
34*0Sstevel@tonic-gate #define	E_HOUR		11
35*0Sstevel@tonic-gate #define	E_MINUTE	14
36*0Sstevel@tonic-gate #define	E_SECOND	17
37*0Sstevel@tonic-gate #define	E_YEAR		20
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #ifdef __STDC__
40*0Sstevel@tonic-gate static int lookup(char *);
41*0Sstevel@tonic-gate static time_t emitl(struct tm *);
42*0Sstevel@tonic-gate #else
43*0Sstevel@tonic-gate static int lookup();
44*0Sstevel@tonic-gate static time_t emitl();
45*0Sstevel@tonic-gate #endif
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate time_t
unctime(str)48*0Sstevel@tonic-gate unctime(str)
49*0Sstevel@tonic-gate 	char *str;
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	struct tm then;
52*0Sstevel@tonic-gate 	char dbuf[30];
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	/* Definition of ctime(3) is 24 characters + newline + NUL */
55*0Sstevel@tonic-gate 	(void) strncpy(dbuf, str, 24);
56*0Sstevel@tonic-gate 	dbuf[24] = '\0';
57*0Sstevel@tonic-gate 	dbuf[E_MONTH+3] = '\0';
58*0Sstevel@tonic-gate 	then.tm_mon = lookup(&dbuf[E_MONTH]);
59*0Sstevel@tonic-gate 	if (then.tm_mon < 0) {
60*0Sstevel@tonic-gate 		return (-1);
61*0Sstevel@tonic-gate 	}
62*0Sstevel@tonic-gate 	then.tm_mday = atoi(&dbuf[E_DAY]);
63*0Sstevel@tonic-gate 	then.tm_hour = atoi(&dbuf[E_HOUR]);
64*0Sstevel@tonic-gate 	then.tm_min = atoi(&dbuf[E_MINUTE]);
65*0Sstevel@tonic-gate 	then.tm_sec = atoi(&dbuf[E_SECOND]);
66*0Sstevel@tonic-gate 	then.tm_year = atoi(&dbuf[E_YEAR]) - 1900;
67*0Sstevel@tonic-gate 	return (emitl(&then));
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate static char months[] =
71*0Sstevel@tonic-gate 	"JanFebMarAprMayJunJulAugSepOctNovDec";
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate static int
lookup(str)74*0Sstevel@tonic-gate lookup(str)
75*0Sstevel@tonic-gate 	char *str;
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate 	char *cp, *cp2;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	for (cp = months, cp2 = str; *cp != 0; cp += 3)
80*0Sstevel@tonic-gate 		if (strncmp(cp, cp2, 3) == 0)
81*0Sstevel@tonic-gate 			/* LINTED ptr arith will give < INT_MAX result */
82*0Sstevel@tonic-gate 			return (((int)(cp-months)) / 3);
83*0Sstevel@tonic-gate 	return (-1);
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate /*
86*0Sstevel@tonic-gate  * Routine to convert a localtime(3) format date back into
87*0Sstevel@tonic-gate  * a system format date.
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate static time_t
emitl(dp)90*0Sstevel@tonic-gate emitl(dp)
91*0Sstevel@tonic-gate 	struct tm *dp;
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	dp->tm_isdst = -1;
94*0Sstevel@tonic-gate 	return (mktime(dp));
95*0Sstevel@tonic-gate }
96