122699Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 362522Sbostic * Copyright (c) 1988, 1993 462522Sbostic * The Regents of the University of California. All rights reserved. 533728Sbostic * 642825Sbostic * %sccs.include.redist.c% 733728Sbostic */ 822699Sdist 922699Sdist #ifndef lint 10*68504Seric static char sccsid[] = "@(#)convtime.c 8.2 (Berkeley) 03/08/95"; 1133728Sbostic #endif /* not lint */ 1222699Sdist 134617Seric # include <ctype.h> 145200Seric # include "useful.h" 154617Seric 164617Seric /* 174617Seric ** CONVTIME -- convert time 184617Seric ** 195200Seric ** Takes a time as an ascii string with a trailing character 205200Seric ** giving units: 215200Seric ** s -- seconds 225200Seric ** m -- minutes 235200Seric ** h -- hours 245200Seric ** d -- days (default) 255200Seric ** w -- weeks 269373Seric ** For example, "3d12h" is three and a half days. 275200Seric ** 284617Seric ** Parameters: 294617Seric ** p -- pointer to ascii time. 3058796Seric ** units -- default units if none specified. 314617Seric ** 324617Seric ** Returns: 334617Seric ** time in seconds. 344617Seric ** 354617Seric ** Side Effects: 364617Seric ** none. 374617Seric */ 384617Seric 394617Seric time_t 4058796Seric convtime(p, units) 414617Seric char *p; 4258796Seric char units; 434617Seric { 445200Seric register time_t t, r; 4511996Seric register char c; 464617Seric 475200Seric r = 0; 485200Seric while (*p != '\0') 494617Seric { 505200Seric t = 0; 5160537Seric while ((c = *p++) != '\0' && isascii(c) && isdigit(c)) 5211996Seric t = t * 10 + (c - '0'); 5311996Seric if (c == '\0') 5458796Seric { 5558796Seric c = units; 5611996Seric p--; 5758796Seric } 58*68504Seric else if (strchr("wdhms", c) == NULL) 59*68504Seric { 60*68504Seric usrerr("Invalid time unit `%c'", c); 61*68504Seric c = units; 62*68504Seric } 6311996Seric switch (c) 645200Seric { 655200Seric case 'w': /* weeks */ 665200Seric t *= 7; 674617Seric 685200Seric case 'd': /* days */ 695200Seric default: 705200Seric t *= 24; 714617Seric 725200Seric case 'h': /* hours */ 735200Seric t *= 60; 744617Seric 755200Seric case 'm': /* minutes */ 765200Seric t *= 60; 775200Seric 785200Seric case 's': /* seconds */ 795200Seric break; 805200Seric } 815200Seric r += t; 824617Seric } 834617Seric 845200Seric return (r); 854617Seric } 869373Seric /* 879373Seric ** PINTVL -- produce printable version of a time interval 889373Seric ** 899373Seric ** Parameters: 909373Seric ** intvl -- the interval to be converted 919373Seric ** brief -- if TRUE, print this in an extremely compact form 929373Seric ** (basically used for logging). 939373Seric ** 949373Seric ** Returns: 959373Seric ** A pointer to a string version of intvl suitable for 969373Seric ** printing or framing. 979373Seric ** 989373Seric ** Side Effects: 999373Seric ** none. 1009373Seric ** 1019373Seric ** Warning: 1029373Seric ** The string returned is in a static buffer. 1039373Seric */ 1049373Seric 1059373Seric # define PLURAL(n) ((n) == 1 ? "" : "s") 1069373Seric 1079373Seric char * 1089373Seric pintvl(intvl, brief) 1099373Seric time_t intvl; 1109373Seric bool brief; 1119373Seric { 1129373Seric static char buf[256]; 1139373Seric register char *p; 1149373Seric int wk, dy, hr, mi, se; 1159373Seric 1169373Seric if (intvl == 0 && !brief) 1179373Seric return ("zero seconds"); 1189373Seric 1199373Seric /* decode the interval into weeks, days, hours, minutes, seconds */ 1209373Seric se = intvl % 60; 1219373Seric intvl /= 60; 1229373Seric mi = intvl % 60; 1239373Seric intvl /= 60; 1249373Seric hr = intvl % 24; 1259373Seric intvl /= 24; 1269373Seric if (brief) 1279373Seric dy = intvl; 1289373Seric else 1299373Seric { 1309373Seric dy = intvl % 7; 1319373Seric intvl /= 7; 1329373Seric wk = intvl; 1339373Seric } 1349373Seric 1359373Seric /* now turn it into a sexy form */ 1369373Seric p = buf; 1379373Seric if (brief) 1389373Seric { 1399373Seric if (dy > 0) 1409373Seric { 1419373Seric (void) sprintf(p, "%d+", dy); 1429373Seric p += strlen(p); 1439373Seric } 1449373Seric (void) sprintf(p, "%02d:%02d:%02d", hr, mi, se); 1459373Seric return (buf); 1469373Seric } 1479373Seric 1489373Seric /* use the verbose form */ 1499373Seric if (wk > 0) 1509373Seric { 1519373Seric (void) sprintf(p, ", %d week%s", wk, PLURAL(wk)); 1529373Seric p += strlen(p); 1539373Seric } 1549373Seric if (dy > 0) 1559373Seric { 1569373Seric (void) sprintf(p, ", %d day%s", dy, PLURAL(dy)); 1579373Seric p += strlen(p); 1589373Seric } 1599373Seric if (hr > 0) 1609373Seric { 1619373Seric (void) sprintf(p, ", %d hour%s", hr, PLURAL(hr)); 1629373Seric p += strlen(p); 1639373Seric } 1649373Seric if (mi > 0) 1659373Seric { 1669373Seric (void) sprintf(p, ", %d minute%s", mi, PLURAL(mi)); 1679373Seric p += strlen(p); 1689373Seric } 1699373Seric if (se > 0) 1709373Seric { 1719373Seric (void) sprintf(p, ", %d second%s", se, PLURAL(se)); 1729373Seric p += strlen(p); 1739373Seric } 1749373Seric 1759373Seric return (buf + 2); 1769373Seric } 177