122699Sdist /* 2*33728Sbostic * Copyright (c) 1988 Regents of the University of California. 3*33728Sbostic * All rights reserved. 4*33728Sbostic * 5*33728Sbostic * Redistribution and use in source and binary forms are permitted 6*33728Sbostic * provided that this notice is preserved and that due credit is given 7*33728Sbostic * to the University of California at Berkeley. The name of the University 8*33728Sbostic * may not be used to endorse or promote products derived from this 9*33728Sbostic * software without specific prior written permission. This software 10*33728Sbostic * is provided ``as is'' without express or implied warranty. 11*33728Sbostic * 12*33728Sbostic * Sendmail 13*33728Sbostic * Copyright (c) 1983 Eric P. Allman 14*33728Sbostic * Berkeley, California 15*33728Sbostic */ 1622699Sdist 1722699Sdist #ifndef lint 18*33728Sbostic static char sccsid[] = "@(#)convtime.c 5.2 (Berkeley) 03/13/88"; 19*33728Sbostic #endif /* not lint */ 2022699Sdist 214617Seric # include <ctype.h> 225200Seric # include "useful.h" 234617Seric 244617Seric /* 254617Seric ** CONVTIME -- convert time 264617Seric ** 275200Seric ** Takes a time as an ascii string with a trailing character 285200Seric ** giving units: 295200Seric ** s -- seconds 305200Seric ** m -- minutes 315200Seric ** h -- hours 325200Seric ** d -- days (default) 335200Seric ** w -- weeks 349373Seric ** For example, "3d12h" is three and a half days. 355200Seric ** 364617Seric ** Parameters: 374617Seric ** p -- pointer to ascii time. 384617Seric ** 394617Seric ** Returns: 404617Seric ** time in seconds. 414617Seric ** 424617Seric ** Side Effects: 434617Seric ** none. 444617Seric */ 454617Seric 464617Seric time_t 474617Seric convtime(p) 484617Seric char *p; 494617Seric { 505200Seric register time_t t, r; 5111996Seric register char c; 524617Seric 535200Seric r = 0; 545200Seric while (*p != '\0') 554617Seric { 565200Seric t = 0; 5711996Seric while (isdigit(c = *p++)) 5811996Seric t = t * 10 + (c - '0'); 5911996Seric if (c == '\0') 6011996Seric p--; 6111996Seric switch (c) 625200Seric { 635200Seric case 'w': /* weeks */ 645200Seric t *= 7; 654617Seric 665200Seric case 'd': /* days */ 675200Seric default: 685200Seric t *= 24; 694617Seric 705200Seric case 'h': /* hours */ 715200Seric t *= 60; 724617Seric 735200Seric case 'm': /* minutes */ 745200Seric t *= 60; 755200Seric 765200Seric case 's': /* seconds */ 775200Seric break; 785200Seric } 795200Seric r += t; 804617Seric } 814617Seric 825200Seric return (r); 834617Seric } 849373Seric /* 859373Seric ** PINTVL -- produce printable version of a time interval 869373Seric ** 879373Seric ** Parameters: 889373Seric ** intvl -- the interval to be converted 899373Seric ** brief -- if TRUE, print this in an extremely compact form 909373Seric ** (basically used for logging). 919373Seric ** 929373Seric ** Returns: 939373Seric ** A pointer to a string version of intvl suitable for 949373Seric ** printing or framing. 959373Seric ** 969373Seric ** Side Effects: 979373Seric ** none. 989373Seric ** 999373Seric ** Warning: 1009373Seric ** The string returned is in a static buffer. 1019373Seric */ 1029373Seric 1039373Seric # define PLURAL(n) ((n) == 1 ? "" : "s") 1049373Seric 1059373Seric char * 1069373Seric pintvl(intvl, brief) 1079373Seric time_t intvl; 1089373Seric bool brief; 1099373Seric { 1109373Seric static char buf[256]; 1119373Seric register char *p; 1129373Seric int wk, dy, hr, mi, se; 1139373Seric 1149373Seric if (intvl == 0 && !brief) 1159373Seric return ("zero seconds"); 1169373Seric 1179373Seric /* decode the interval into weeks, days, hours, minutes, seconds */ 1189373Seric se = intvl % 60; 1199373Seric intvl /= 60; 1209373Seric mi = intvl % 60; 1219373Seric intvl /= 60; 1229373Seric hr = intvl % 24; 1239373Seric intvl /= 24; 1249373Seric if (brief) 1259373Seric dy = intvl; 1269373Seric else 1279373Seric { 1289373Seric dy = intvl % 7; 1299373Seric intvl /= 7; 1309373Seric wk = intvl; 1319373Seric } 1329373Seric 1339373Seric /* now turn it into a sexy form */ 1349373Seric p = buf; 1359373Seric if (brief) 1369373Seric { 1379373Seric if (dy > 0) 1389373Seric { 1399373Seric (void) sprintf(p, "%d+", dy); 1409373Seric p += strlen(p); 1419373Seric } 1429373Seric (void) sprintf(p, "%02d:%02d:%02d", hr, mi, se); 1439373Seric return (buf); 1449373Seric } 1459373Seric 1469373Seric /* use the verbose form */ 1479373Seric if (wk > 0) 1489373Seric { 1499373Seric (void) sprintf(p, ", %d week%s", wk, PLURAL(wk)); 1509373Seric p += strlen(p); 1519373Seric } 1529373Seric if (dy > 0) 1539373Seric { 1549373Seric (void) sprintf(p, ", %d day%s", dy, PLURAL(dy)); 1559373Seric p += strlen(p); 1569373Seric } 1579373Seric if (hr > 0) 1589373Seric { 1599373Seric (void) sprintf(p, ", %d hour%s", hr, PLURAL(hr)); 1609373Seric p += strlen(p); 1619373Seric } 1629373Seric if (mi > 0) 1639373Seric { 1649373Seric (void) sprintf(p, ", %d minute%s", mi, PLURAL(mi)); 1659373Seric p += strlen(p); 1669373Seric } 1679373Seric if (se > 0) 1689373Seric { 1699373Seric (void) sprintf(p, ", %d second%s", se, PLURAL(se)); 1709373Seric p += strlen(p); 1719373Seric } 1729373Seric 1739373Seric return (buf + 2); 1749373Seric } 175