122042Sdist /* 222042Sdist * Copyright (c) 1980 Regents of the University of California. 322042Sdist * All rights reserved. The Berkeley software License Agreement 422042Sdist * specifies the terms and conditions for redistribution. 522042Sdist */ 622042Sdist 714593Ssam #ifndef lint 8*46587Storek static char sccsid[] = "@(#)unctime.c 5.2 (Berkeley) 02/23/91"; 9*46587Storek #endif /* not lint */ 1014593Ssam 111427Sroot #include <sys/types.h> 1213756Ssam #include <sys/time.h> 133548Sroot #include <stdio.h> 14*46587Storek #include <stdlib.h> 15*46587Storek #include <string.h> 16*46587Storek 171427Sroot /* 181427Sroot * Convert a ctime(3) format string into a system format date. 191427Sroot * Return the date thus calculated. 201427Sroot * 211427Sroot * Return -1 if the string is not in ctime format. 221427Sroot */ 231427Sroot 241427Sroot /* 251427Sroot * Offsets into the ctime string to various parts. 261427Sroot */ 271427Sroot 281427Sroot #define E_MONTH 4 291427Sroot #define E_DAY 8 301427Sroot #define E_HOUR 11 311427Sroot #define E_MINUTE 14 321427Sroot #define E_SECOND 17 331427Sroot #define E_YEAR 20 341427Sroot 35*46587Storek static int lookup(); 36*46587Storek 37*46587Storek time_t 38*46587Storek unctime(str) 391427Sroot char *str; 401427Sroot { 411427Sroot struct tm then; 421427Sroot char dbuf[30]; 431427Sroot time_t emitl(); 441427Sroot 451427Sroot if (strlen(str) != 25) 461427Sroot str[25] = 0; 471427Sroot strcpy(dbuf, str); 481427Sroot dbuf[E_MONTH+3] = 0; 49*46587Storek if ((then.tm_mon = lookup(&dbuf[E_MONTH])) < 0) 50*46587Storek return (-1); 511427Sroot then.tm_mday = atoi(&dbuf[E_DAY]); 521427Sroot then.tm_hour = atoi(&dbuf[E_HOUR]); 531427Sroot then.tm_min = atoi(&dbuf[E_MINUTE]); 541427Sroot then.tm_sec = atoi(&dbuf[E_SECOND]); 551427Sroot then.tm_year = atoi(&dbuf[E_YEAR]) - 1900; 561427Sroot return(emitl(&then)); 571427Sroot } 581427Sroot 591427Sroot static char months[] = 601427Sroot "JanFebMarAprMayJunJulAugSepOctNovDec"; 611427Sroot 62*46587Storek static int 631427Sroot lookup(str) 641427Sroot char *str; 651427Sroot { 661427Sroot register char *cp, *cp2; 671427Sroot 681427Sroot for (cp = months, cp2 = str; *cp != 0; cp += 3) 691427Sroot if (strncmp(cp, cp2, 3) == 0) 701427Sroot return((cp-months) / 3); 711427Sroot return(-1); 721427Sroot } 731427Sroot /* 741427Sroot * Routine to convert a localtime(3) format date back into 751427Sroot * a system format date. 761427Sroot * 771427Sroot * Use a binary search. 781427Sroot */ 791427Sroot 801427Sroot struct tm *localtime(); 81*46587Storek static int dcmp(); 821427Sroot 83*46587Storek time_t 84*46587Storek emitl(dp) 851427Sroot struct tm *dp; 861427Sroot { 871427Sroot time_t conv; 881427Sroot register int i, bit; 891427Sroot struct tm dcopy; 901427Sroot 911427Sroot dcopy = *dp; 921427Sroot dp = &dcopy; 931427Sroot conv = 0; 943548Sroot for (i = 30; i >= 0; i--) { 951427Sroot bit = 1 << i; 961427Sroot conv |= bit; 971427Sroot if (dcmp(localtime(&conv), dp) > 0) 981427Sroot conv &= ~bit; 991427Sroot } 1001427Sroot return(conv); 1011427Sroot } 1021427Sroot 1031427Sroot /* 1041427Sroot * Compare two localtime dates, return result. 1051427Sroot */ 1061427Sroot 1071427Sroot #define DECIDE(a) \ 1081427Sroot if (dp->a > dp2->a) \ 1091427Sroot return(1); \ 1101427Sroot if (dp->a < dp2->a) \ 1111427Sroot return(-1) 1121427Sroot 113*46587Storek static int 1141427Sroot dcmp(dp, dp2) 1151427Sroot register struct tm *dp, *dp2; 1161427Sroot { 1171427Sroot 1181427Sroot DECIDE(tm_year); 1191427Sroot DECIDE(tm_mon); 1201427Sroot DECIDE(tm_mday); 1211427Sroot DECIDE(tm_hour); 1221427Sroot DECIDE(tm_min); 1231427Sroot DECIDE(tm_sec); 1241427Sroot return(0); 1251427Sroot } 126