xref: /original-bsd/sbin/dump/unctime.c (revision f4a18198)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)unctime.c	8.2 (Berkeley) 06/14/94";
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 lookup __P((char *));
44 
45 
46 time_t
47 unctime(str)
48 	char *str;
49 {
50 	struct tm then;
51 	char dbuf[26];
52 
53 	(void) strncpy(dbuf, str, sizeof(dbuf) - 1);
54 	dbuf[sizeof(dbuf) - 1] = '\0';
55 	dbuf[E_MONTH+3] = '\0';
56 	if ((then.tm_mon = lookup(&dbuf[E_MONTH])) < 0)
57 		return (-1);
58 	then.tm_mday = atoi(&dbuf[E_DAY]);
59 	then.tm_hour = atoi(&dbuf[E_HOUR]);
60 	then.tm_min = atoi(&dbuf[E_MINUTE]);
61 	then.tm_sec = atoi(&dbuf[E_SECOND]);
62 	then.tm_year = atoi(&dbuf[E_YEAR]) - 1900;
63 	then.tm_isdst = -1;
64 	return(mktime(&then));
65 }
66 
67 static char months[] =
68 	"JanFebMarAprMayJunJulAugSepOctNovDec";
69 
70 static int
71 lookup(str)
72 	char *str;
73 {
74 	register char *cp, *cp2;
75 
76 	for (cp = months, cp2 = str; *cp != '\0'; cp += 3)
77 		if (strncmp(cp, cp2, 3) == 0)
78 			return((cp-months) / 3);
79 	return(-1);
80 }
81