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