1 /* 2 * Copyright (c) 1998,2001 by Sun Microsystems, Inc. 3 * All rights reserved. 4 */ 5 6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 7 /* All Rights Reserved */ 8 9 /* 10 * Copyright (c) 1980 Regents of the University of California. 11 * All rights reserved. The Berkeley software License Agreement 12 * specifies the terms and conditions for redistribution. 13 */ 14 15 #include <sys/types.h> 16 #include <time.h> 17 #include <string.h> 18 #include <stdlib.h> 19 /* 20 * Convert a ctime(3) format string into a system format date. 21 * Return the date thus calculated. 22 * 23 * Return -1 if the string is not in ctime format. 24 */ 25 26 /* 27 * Offsets into the ctime string to various parts. 28 */ 29 30 #define E_MONTH 4 31 #define E_DAY 8 32 #define E_HOUR 11 33 #define E_MINUTE 14 34 #define E_SECOND 17 35 #define E_YEAR 20 36 37 #ifdef __STDC__ 38 static int lookup(char *); 39 static time_t emitl(struct tm *); 40 #else 41 static int lookup(); 42 static time_t emitl(); 43 #endif 44 45 time_t 46 unctime(str) 47 char *str; 48 { 49 struct tm then; 50 char dbuf[30]; 51 52 /* Definition of ctime(3) is 24 characters + newline + NUL */ 53 (void) strncpy(dbuf, str, 24); 54 dbuf[24] = '\0'; 55 dbuf[E_MONTH+3] = '\0'; 56 then.tm_mon = lookup(&dbuf[E_MONTH]); 57 if (then.tm_mon < 0) { 58 return (-1); 59 } 60 then.tm_mday = atoi(&dbuf[E_DAY]); 61 then.tm_hour = atoi(&dbuf[E_HOUR]); 62 then.tm_min = atoi(&dbuf[E_MINUTE]); 63 then.tm_sec = atoi(&dbuf[E_SECOND]); 64 then.tm_year = atoi(&dbuf[E_YEAR]) - 1900; 65 return (emitl(&then)); 66 } 67 68 static char months[] = 69 "JanFebMarAprMayJunJulAugSepOctNovDec"; 70 71 static int 72 lookup(str) 73 char *str; 74 { 75 char *cp, *cp2; 76 77 for (cp = months, cp2 = str; *cp != 0; cp += 3) 78 if (strncmp(cp, cp2, 3) == 0) 79 /* LINTED ptr arith will give < INT_MAX result */ 80 return (((int)(cp-months)) / 3); 81 return (-1); 82 } 83 /* 84 * Routine to convert a localtime(3) format date back into 85 * a system format date. 86 */ 87 static time_t 88 emitl(dp) 89 struct tm *dp; 90 { 91 dp->tm_isdst = -1; 92 return (mktime(dp)); 93 } 94