xref: /csrg-svn/sbin/dump/unctime.c (revision 67380)
147082Smckusick /*-
261484Sbostic  * Copyright (c) 1980, 1993
361484Sbostic  *	The Regents of the University of California.  All rights reserved.
447082Smckusick  *
547082Smckusick  * %sccs.include.redist.c%
622042Sdist  */
722042Sdist 
814593Ssam #ifndef lint
9*67380Smkm static char sccsid[] = "@(#)unctime.c	8.2 (Berkeley) 06/14/94";
1046587Storek #endif /* not lint */
1114593Ssam 
121427Sroot #include <sys/types.h>
1357725Smckusick 
1457725Smckusick #include <stdio.h>
1546795Sbostic #include <time.h>
1646795Sbostic #ifdef __STDC__
1746587Storek #include <stdlib.h>
1846587Storek #include <string.h>
1946795Sbostic #endif
2046587Storek 
2157725Smckusick #ifndef __P
2257725Smckusick #include <sys/cdefs.h>
2357725Smckusick #endif
2457725Smckusick 
251427Sroot /*
261427Sroot  * Convert a ctime(3) format string into a system format date.
271427Sroot  * Return the date thus calculated.
281427Sroot  *
291427Sroot  * Return -1 if the string is not in ctime format.
301427Sroot  */
311427Sroot 
321427Sroot /*
331427Sroot  * Offsets into the ctime string to various parts.
341427Sroot  */
351427Sroot 
361427Sroot #define	E_MONTH		4
371427Sroot #define	E_DAY		8
381427Sroot #define	E_HOUR		11
391427Sroot #define	E_MINUTE	14
401427Sroot #define	E_SECOND	17
411427Sroot #define	E_YEAR		20
421427Sroot 
4357725Smckusick static	int lookup __P((char *));
4446587Storek 
4557725Smckusick 
4646587Storek time_t
unctime(str)4746587Storek unctime(str)
481427Sroot 	char *str;
491427Sroot {
501427Sroot 	struct tm then;
5159112Smckusick 	char dbuf[26];
521427Sroot 
5359112Smckusick 	(void) strncpy(dbuf, str, sizeof(dbuf) - 1);
5459112Smckusick 	dbuf[sizeof(dbuf) - 1] = '\0';
5557725Smckusick 	dbuf[E_MONTH+3] = '\0';
5646587Storek 	if ((then.tm_mon = lookup(&dbuf[E_MONTH])) < 0)
5746587Storek 		return (-1);
581427Sroot 	then.tm_mday = atoi(&dbuf[E_DAY]);
591427Sroot 	then.tm_hour = atoi(&dbuf[E_HOUR]);
601427Sroot 	then.tm_min = atoi(&dbuf[E_MINUTE]);
611427Sroot 	then.tm_sec = atoi(&dbuf[E_SECOND]);
621427Sroot 	then.tm_year = atoi(&dbuf[E_YEAR]) - 1900;
63*67380Smkm 	then.tm_isdst = -1;
64*67380Smkm 	return(mktime(&then));
651427Sroot }
661427Sroot 
671427Sroot static char months[] =
681427Sroot 	"JanFebMarAprMayJunJulAugSepOctNovDec";
691427Sroot 
7046587Storek static int
lookup(str)711427Sroot lookup(str)
721427Sroot 	char *str;
731427Sroot {
741427Sroot 	register char *cp, *cp2;
751427Sroot 
7657725Smckusick 	for (cp = months, cp2 = str; *cp != '\0'; cp += 3)
771427Sroot 		if (strncmp(cp, cp2, 3) == 0)
781427Sroot 			return((cp-months) / 3);
791427Sroot 	return(-1);
801427Sroot }
81