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