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