122699Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 333728Sbostic * Copyright (c) 1988 Regents of the University of California. 433728Sbostic * All rights reserved. 533728Sbostic * 642825Sbostic * %sccs.include.redist.c% 733728Sbostic */ 822699Sdist 922699Sdist #ifndef lint 10*58796Seric static char sccsid[] = "@(#)convtime.c 6.3 (Berkeley) 03/23/93"; 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. 30*58796Seric ** units -- default units if none specified. 314617Seric ** 324617Seric ** Returns: 334617Seric ** time in seconds. 344617Seric ** 354617Seric ** Side Effects: 364617Seric ** none. 374617Seric */ 384617Seric 394617Seric time_t 40*58796Seric convtime(p, units) 414617Seric char *p; 42*58796Seric 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; 5158050Seric while (isascii(c = *p++) && isdigit(c)) 5211996Seric t = t * 10 + (c - '0'); 5311996Seric if (c == '\0') 54*58796Seric { 55*58796Seric c = units; 5611996Seric p--; 57*58796Seric } 5811996Seric switch (c) 595200Seric { 605200Seric case 'w': /* weeks */ 615200Seric t *= 7; 624617Seric 635200Seric case 'd': /* days */ 645200Seric default: 655200Seric t *= 24; 664617Seric 675200Seric case 'h': /* hours */ 685200Seric t *= 60; 694617Seric 705200Seric case 'm': /* minutes */ 715200Seric t *= 60; 725200Seric 735200Seric case 's': /* seconds */ 745200Seric break; 755200Seric } 765200Seric r += t; 774617Seric } 784617Seric 795200Seric return (r); 804617Seric } 819373Seric /* 829373Seric ** PINTVL -- produce printable version of a time interval 839373Seric ** 849373Seric ** Parameters: 859373Seric ** intvl -- the interval to be converted 869373Seric ** brief -- if TRUE, print this in an extremely compact form 879373Seric ** (basically used for logging). 889373Seric ** 899373Seric ** Returns: 909373Seric ** A pointer to a string version of intvl suitable for 919373Seric ** printing or framing. 929373Seric ** 939373Seric ** Side Effects: 949373Seric ** none. 959373Seric ** 969373Seric ** Warning: 979373Seric ** The string returned is in a static buffer. 989373Seric */ 999373Seric 1009373Seric # define PLURAL(n) ((n) == 1 ? "" : "s") 1019373Seric 1029373Seric char * 1039373Seric pintvl(intvl, brief) 1049373Seric time_t intvl; 1059373Seric bool brief; 1069373Seric { 1079373Seric static char buf[256]; 1089373Seric register char *p; 1099373Seric int wk, dy, hr, mi, se; 1109373Seric 1119373Seric if (intvl == 0 && !brief) 1129373Seric return ("zero seconds"); 1139373Seric 1149373Seric /* decode the interval into weeks, days, hours, minutes, seconds */ 1159373Seric se = intvl % 60; 1169373Seric intvl /= 60; 1179373Seric mi = intvl % 60; 1189373Seric intvl /= 60; 1199373Seric hr = intvl % 24; 1209373Seric intvl /= 24; 1219373Seric if (brief) 1229373Seric dy = intvl; 1239373Seric else 1249373Seric { 1259373Seric dy = intvl % 7; 1269373Seric intvl /= 7; 1279373Seric wk = intvl; 1289373Seric } 1299373Seric 1309373Seric /* now turn it into a sexy form */ 1319373Seric p = buf; 1329373Seric if (brief) 1339373Seric { 1349373Seric if (dy > 0) 1359373Seric { 1369373Seric (void) sprintf(p, "%d+", dy); 1379373Seric p += strlen(p); 1389373Seric } 1399373Seric (void) sprintf(p, "%02d:%02d:%02d", hr, mi, se); 1409373Seric return (buf); 1419373Seric } 1429373Seric 1439373Seric /* use the verbose form */ 1449373Seric if (wk > 0) 1459373Seric { 1469373Seric (void) sprintf(p, ", %d week%s", wk, PLURAL(wk)); 1479373Seric p += strlen(p); 1489373Seric } 1499373Seric if (dy > 0) 1509373Seric { 1519373Seric (void) sprintf(p, ", %d day%s", dy, PLURAL(dy)); 1529373Seric p += strlen(p); 1539373Seric } 1549373Seric if (hr > 0) 1559373Seric { 1569373Seric (void) sprintf(p, ", %d hour%s", hr, PLURAL(hr)); 1579373Seric p += strlen(p); 1589373Seric } 1599373Seric if (mi > 0) 1609373Seric { 1619373Seric (void) sprintf(p, ", %d minute%s", mi, PLURAL(mi)); 1629373Seric p += strlen(p); 1639373Seric } 1649373Seric if (se > 0) 1659373Seric { 1669373Seric (void) sprintf(p, ", %d second%s", se, PLURAL(se)); 1679373Seric p += strlen(p); 1689373Seric } 1699373Seric 1709373Seric return (buf + 2); 1719373Seric } 172