xref: /csrg-svn/old/dump.4.1/unctime.c (revision 1427)
1*1427Sroot static	char *sccsid = "@(#)unctime.c	1.1 (Berkeley) 10/13/80";
2*1427Sroot #include <sys/types.h>
3*1427Sroot #include <time.h>
4*1427Sroot /*
5*1427Sroot  * Convert a ctime(3) format string into a system format date.
6*1427Sroot  * Return the date thus calculated.
7*1427Sroot  *
8*1427Sroot  * Return -1 if the string is not in ctime format.
9*1427Sroot  */
10*1427Sroot 
11*1427Sroot /*
12*1427Sroot  * Offsets into the ctime string to various parts.
13*1427Sroot  */
14*1427Sroot 
15*1427Sroot #define	E_MONTH		4
16*1427Sroot #define	E_DAY		8
17*1427Sroot #define	E_HOUR		11
18*1427Sroot #define	E_MINUTE	14
19*1427Sroot #define	E_SECOND	17
20*1427Sroot #define	E_YEAR		20
21*1427Sroot 
22*1427Sroot time_t unctime(str)
23*1427Sroot 	char *str;
24*1427Sroot {
25*1427Sroot 	struct tm then;
26*1427Sroot 	char dbuf[30];
27*1427Sroot 	time_t emitl();
28*1427Sroot 
29*1427Sroot 	if (strlen(str) != 25)
30*1427Sroot 		str[25] = 0;
31*1427Sroot 	strcpy(dbuf, str);
32*1427Sroot 	dbuf[E_MONTH+3] = 0;
33*1427Sroot 	if ( (then.tm_mon = lookup(&dbuf[E_MONTH])) < 0)
34*1427Sroot 		return(-1);;
35*1427Sroot 	then.tm_mday = atoi(&dbuf[E_DAY]);
36*1427Sroot 	then.tm_hour = atoi(&dbuf[E_HOUR]);
37*1427Sroot 	then.tm_min = atoi(&dbuf[E_MINUTE]);
38*1427Sroot 	then.tm_sec = atoi(&dbuf[E_SECOND]);
39*1427Sroot 	then.tm_year = atoi(&dbuf[E_YEAR]) - 1900;
40*1427Sroot 	return(emitl(&then));
41*1427Sroot }
42*1427Sroot 
43*1427Sroot static char months[] =
44*1427Sroot 	"JanFebMarAprMayJunJulAugSepOctNovDec";
45*1427Sroot 
46*1427Sroot static
47*1427Sroot lookup(str)
48*1427Sroot 	char *str;
49*1427Sroot {
50*1427Sroot 	register char *cp, *cp2;
51*1427Sroot 
52*1427Sroot 	for (cp = months, cp2 = str; *cp != 0; cp += 3)
53*1427Sroot 		if (strncmp(cp, cp2, 3) == 0)
54*1427Sroot 			return((cp-months) / 3);
55*1427Sroot 	return(-1);
56*1427Sroot }
57*1427Sroot /*
58*1427Sroot  * Routine to convert a localtime(3) format date back into
59*1427Sroot  * a system format date.
60*1427Sroot  *
61*1427Sroot  *	Use a binary search.
62*1427Sroot  */
63*1427Sroot 
64*1427Sroot struct tm *localtime();
65*1427Sroot 
66*1427Sroot time_t emitl(dp)
67*1427Sroot 	struct tm *dp;
68*1427Sroot {
69*1427Sroot 	time_t conv;
70*1427Sroot 	register int i, bit;
71*1427Sroot 	struct tm dcopy;
72*1427Sroot 
73*1427Sroot 	dcopy = *dp;
74*1427Sroot 	dp = &dcopy;
75*1427Sroot 	conv = 0;
76*1427Sroot 	for (i = 31; i >= 0; i--) {
77*1427Sroot 		bit = 1 << i;
78*1427Sroot 		conv |= bit;
79*1427Sroot 		if (dcmp(localtime(&conv), dp) > 0)
80*1427Sroot 			conv &= ~bit;
81*1427Sroot 	}
82*1427Sroot 	return(conv);
83*1427Sroot }
84*1427Sroot 
85*1427Sroot /*
86*1427Sroot  * Compare two localtime dates, return result.
87*1427Sroot  */
88*1427Sroot 
89*1427Sroot #define DECIDE(a) \
90*1427Sroot 	if (dp->a > dp2->a) \
91*1427Sroot 		return(1); \
92*1427Sroot 	if (dp->a < dp2->a) \
93*1427Sroot 		return(-1)
94*1427Sroot 
95*1427Sroot static
96*1427Sroot dcmp(dp, dp2)
97*1427Sroot 	register struct tm *dp, *dp2;
98*1427Sroot {
99*1427Sroot 
100*1427Sroot 	DECIDE(tm_year);
101*1427Sroot 	DECIDE(tm_mon);
102*1427Sroot 	DECIDE(tm_mday);
103*1427Sroot 	DECIDE(tm_hour);
104*1427Sroot 	DECIDE(tm_min);
105*1427Sroot 	DECIDE(tm_sec);
106*1427Sroot 	return(0);
107*1427Sroot }
108