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