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