1*22033Sdist /*
2*22033Sdist * Copyright (c) 1980 Regents of the University of California.
3*22033Sdist * All rights reserved. The Berkeley software License Agreement
4*22033Sdist * specifies the terms and conditions for redistribution.
5*22033Sdist */
6*22033Sdist
7*22033Sdist #ifndef lint
8*22033Sdist static char sccsid[] = "@(#)unctime.c 5.1 (Berkeley) 06/05/85";
9*22033Sdist #endif not lint
10*22033Sdist
1112081Smckusick #include "include.4.1/sys/types.h"
1212081Smckusick #include "include.4.1/time.h"
131427Sroot /*
141427Sroot * Convert a ctime(3) format string into a system format date.
151427Sroot * Return the date thus calculated.
161427Sroot *
171427Sroot * Return -1 if the string is not in ctime format.
181427Sroot */
191427Sroot
201427Sroot /*
211427Sroot * Offsets into the ctime string to various parts.
221427Sroot */
231427Sroot
241427Sroot #define E_MONTH 4
251427Sroot #define E_DAY 8
261427Sroot #define E_HOUR 11
271427Sroot #define E_MINUTE 14
281427Sroot #define E_SECOND 17
291427Sroot #define E_YEAR 20
301427Sroot
unctime(str)311427Sroot time_t unctime(str)
321427Sroot char *str;
331427Sroot {
341427Sroot struct tm then;
351427Sroot char dbuf[30];
361427Sroot time_t emitl();
371427Sroot
381427Sroot if (strlen(str) != 25)
391427Sroot str[25] = 0;
401427Sroot strcpy(dbuf, str);
411427Sroot dbuf[E_MONTH+3] = 0;
421427Sroot if ( (then.tm_mon = lookup(&dbuf[E_MONTH])) < 0)
431427Sroot return(-1);;
441427Sroot then.tm_mday = atoi(&dbuf[E_DAY]);
451427Sroot then.tm_hour = atoi(&dbuf[E_HOUR]);
461427Sroot then.tm_min = atoi(&dbuf[E_MINUTE]);
471427Sroot then.tm_sec = atoi(&dbuf[E_SECOND]);
481427Sroot then.tm_year = atoi(&dbuf[E_YEAR]) - 1900;
491427Sroot return(emitl(&then));
501427Sroot }
511427Sroot
521427Sroot static char months[] =
531427Sroot "JanFebMarAprMayJunJulAugSepOctNovDec";
541427Sroot
551427Sroot static
lookup(str)561427Sroot lookup(str)
571427Sroot char *str;
581427Sroot {
591427Sroot register char *cp, *cp2;
601427Sroot
611427Sroot for (cp = months, cp2 = str; *cp != 0; cp += 3)
621427Sroot if (strncmp(cp, cp2, 3) == 0)
631427Sroot return((cp-months) / 3);
641427Sroot return(-1);
651427Sroot }
661427Sroot /*
671427Sroot * Routine to convert a localtime(3) format date back into
681427Sroot * a system format date.
691427Sroot *
701427Sroot * Use a binary search.
711427Sroot */
721427Sroot
731427Sroot struct tm *localtime();
741427Sroot
emitl(dp)751427Sroot time_t emitl(dp)
761427Sroot struct tm *dp;
771427Sroot {
781427Sroot time_t conv;
791427Sroot register int i, bit;
801427Sroot struct tm dcopy;
811427Sroot
821427Sroot dcopy = *dp;
831427Sroot dp = &dcopy;
841427Sroot conv = 0;
851427Sroot for (i = 31; i >= 0; i--) {
861427Sroot bit = 1 << i;
871427Sroot conv |= bit;
881427Sroot if (dcmp(localtime(&conv), dp) > 0)
891427Sroot conv &= ~bit;
901427Sroot }
911427Sroot return(conv);
921427Sroot }
931427Sroot
941427Sroot /*
951427Sroot * Compare two localtime dates, return result.
961427Sroot */
971427Sroot
981427Sroot #define DECIDE(a) \
991427Sroot if (dp->a > dp2->a) \
1001427Sroot return(1); \
1011427Sroot if (dp->a < dp2->a) \
1021427Sroot return(-1)
1031427Sroot
1041427Sroot static
dcmp(dp,dp2)1051427Sroot dcmp(dp, dp2)
1061427Sroot register struct tm *dp, *dp2;
1071427Sroot {
1081427Sroot
1091427Sroot DECIDE(tm_year);
1101427Sroot DECIDE(tm_mon);
1111427Sroot DECIDE(tm_mday);
1121427Sroot DECIDE(tm_hour);
1131427Sroot DECIDE(tm_min);
1141427Sroot DECIDE(tm_sec);
1151427Sroot return(0);
1161427Sroot }
117