xref: /original-bsd/sbin/dump/unctime.c (revision 4a884f8b)
1 /*-
2  * Copyright (c) 1980 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)unctime.c	5.6 (Berkeley) 01/25/93";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 
14 #include <stdio.h>
15 #include <time.h>
16 #ifdef __STDC__
17 #include <stdlib.h>
18 #include <string.h>
19 #endif
20 
21 #ifndef __P
22 #include <sys/cdefs.h>
23 #endif
24 
25 /*
26  * Convert a ctime(3) format string into a system format date.
27  * Return the date thus calculated.
28  *
29  * Return -1 if the string is not in ctime format.
30  */
31 
32 /*
33  * Offsets into the ctime string to various parts.
34  */
35 
36 #define	E_MONTH		4
37 #define	E_DAY		8
38 #define	E_HOUR		11
39 #define	E_MINUTE	14
40 #define	E_SECOND	17
41 #define	E_YEAR		20
42 
43 static	int dcmp __P((struct tm *, struct tm *));
44 static	time_t emitl __P((struct tm *));
45 static	int lookup __P((char *));
46 
47 
48 time_t
49 unctime(str)
50 	char *str;
51 {
52 	struct tm then;
53 	char dbuf[30];
54 
55 	if (strlen(str) != 25)
56 		str[25] = '\0';
57 	(void) strcpy(dbuf, str);
58 	dbuf[E_MONTH+3] = '\0';
59 	if ((then.tm_mon = lookup(&dbuf[E_MONTH])) < 0)
60 		return (-1);
61 	then.tm_mday = atoi(&dbuf[E_DAY]);
62 	then.tm_hour = atoi(&dbuf[E_HOUR]);
63 	then.tm_min = atoi(&dbuf[E_MINUTE]);
64 	then.tm_sec = atoi(&dbuf[E_SECOND]);
65 	then.tm_year = atoi(&dbuf[E_YEAR]) - 1900;
66 	return(emitl(&then));
67 }
68 
69 static char months[] =
70 	"JanFebMarAprMayJunJulAugSepOctNovDec";
71 
72 static int
73 lookup(str)
74 	char *str;
75 {
76 	register char *cp, *cp2;
77 
78 	for (cp = months, cp2 = str; *cp != '\0'; cp += 3)
79 		if (strncmp(cp, cp2, 3) == 0)
80 			return((cp-months) / 3);
81 	return(-1);
82 }
83 /*
84  * Routine to convert a localtime(3) format date back into
85  * a system format date.
86  *
87  *	Use a binary search.
88  */
89 
90 static time_t
91 emitl(dp)
92 	struct tm *dp;
93 {
94 	time_t conv;
95 	register int i, bit;
96 	struct tm dcopy;
97 
98 	dcopy = *dp;
99 	dp = &dcopy;
100 	conv = 0;
101 	for (i = 30; i >= 0; i--) {
102 		bit = 1 << i;
103 		conv |= bit;
104 		if (dcmp(localtime(&conv), dp) > 0)
105 			conv &= ~bit;
106 	}
107 	return(conv);
108 }
109 
110 /*
111  * Compare two localtime dates, return result.
112  */
113 
114 #define DECIDE(a) \
115 	if (dp->a > dp2->a) \
116 		return(1); \
117 	if (dp->a < dp2->a) \
118 		return(-1)
119 
120 static int
121 dcmp(dp, dp2)
122 	register struct tm *dp, *dp2;
123 {
124 
125 	DECIDE(tm_year);
126 	DECIDE(tm_mon);
127 	DECIDE(tm_mday);
128 	DECIDE(tm_hour);
129 	DECIDE(tm_min);
130 	DECIDE(tm_sec);
131 	return(0);
132 }
133