1*22699Sdist /* 2*22699Sdist ** Sendmail 3*22699Sdist ** Copyright (c) 1983 Eric P. Allman 4*22699Sdist ** Berkeley, California 5*22699Sdist ** 6*22699Sdist ** Copyright (c) 1983 Regents of the University of California. 7*22699Sdist ** All rights reserved. The Berkeley software License Agreement 8*22699Sdist ** specifies the terms and conditions for redistribution. 9*22699Sdist */ 10*22699Sdist 11*22699Sdist #ifndef lint 12*22699Sdist static char SccsId[] = "@(#)convtime.c 5.1 (Berkeley) 06/07/85"; 13*22699Sdist #endif not lint 14*22699Sdist 154617Seric # include <ctype.h> 165200Seric # include "useful.h" 174617Seric 184617Seric /* 194617Seric ** CONVTIME -- convert time 204617Seric ** 215200Seric ** Takes a time as an ascii string with a trailing character 225200Seric ** giving units: 235200Seric ** s -- seconds 245200Seric ** m -- minutes 255200Seric ** h -- hours 265200Seric ** d -- days (default) 275200Seric ** w -- weeks 289373Seric ** For example, "3d12h" is three and a half days. 295200Seric ** 304617Seric ** Parameters: 314617Seric ** p -- pointer to ascii time. 324617Seric ** 334617Seric ** Returns: 344617Seric ** time in seconds. 354617Seric ** 364617Seric ** Side Effects: 374617Seric ** none. 384617Seric */ 394617Seric 404617Seric time_t 414617Seric convtime(p) 424617Seric char *p; 434617Seric { 445200Seric register time_t t, r; 4511996Seric register char c; 464617Seric 475200Seric r = 0; 485200Seric while (*p != '\0') 494617Seric { 505200Seric t = 0; 5111996Seric while (isdigit(c = *p++)) 5211996Seric t = t * 10 + (c - '0'); 5311996Seric if (c == '\0') 5411996Seric p--; 5511996Seric switch (c) 565200Seric { 575200Seric case 'w': /* weeks */ 585200Seric t *= 7; 594617Seric 605200Seric case 'd': /* days */ 615200Seric default: 625200Seric t *= 24; 634617Seric 645200Seric case 'h': /* hours */ 655200Seric t *= 60; 664617Seric 675200Seric case 'm': /* minutes */ 685200Seric t *= 60; 695200Seric 705200Seric case 's': /* seconds */ 715200Seric break; 725200Seric } 735200Seric r += t; 744617Seric } 754617Seric 765200Seric return (r); 774617Seric } 789373Seric /* 799373Seric ** PINTVL -- produce printable version of a time interval 809373Seric ** 819373Seric ** Parameters: 829373Seric ** intvl -- the interval to be converted 839373Seric ** brief -- if TRUE, print this in an extremely compact form 849373Seric ** (basically used for logging). 859373Seric ** 869373Seric ** Returns: 879373Seric ** A pointer to a string version of intvl suitable for 889373Seric ** printing or framing. 899373Seric ** 909373Seric ** Side Effects: 919373Seric ** none. 929373Seric ** 939373Seric ** Warning: 949373Seric ** The string returned is in a static buffer. 959373Seric */ 969373Seric 979373Seric # define PLURAL(n) ((n) == 1 ? "" : "s") 989373Seric 999373Seric char * 1009373Seric pintvl(intvl, brief) 1019373Seric time_t intvl; 1029373Seric bool brief; 1039373Seric { 1049373Seric static char buf[256]; 1059373Seric register char *p; 1069373Seric int wk, dy, hr, mi, se; 1079373Seric 1089373Seric if (intvl == 0 && !brief) 1099373Seric return ("zero seconds"); 1109373Seric 1119373Seric /* decode the interval into weeks, days, hours, minutes, seconds */ 1129373Seric se = intvl % 60; 1139373Seric intvl /= 60; 1149373Seric mi = intvl % 60; 1159373Seric intvl /= 60; 1169373Seric hr = intvl % 24; 1179373Seric intvl /= 24; 1189373Seric if (brief) 1199373Seric dy = intvl; 1209373Seric else 1219373Seric { 1229373Seric dy = intvl % 7; 1239373Seric intvl /= 7; 1249373Seric wk = intvl; 1259373Seric } 1269373Seric 1279373Seric /* now turn it into a sexy form */ 1289373Seric p = buf; 1299373Seric if (brief) 1309373Seric { 1319373Seric if (dy > 0) 1329373Seric { 1339373Seric (void) sprintf(p, "%d+", dy); 1349373Seric p += strlen(p); 1359373Seric } 1369373Seric (void) sprintf(p, "%02d:%02d:%02d", hr, mi, se); 1379373Seric return (buf); 1389373Seric } 1399373Seric 1409373Seric /* use the verbose form */ 1419373Seric if (wk > 0) 1429373Seric { 1439373Seric (void) sprintf(p, ", %d week%s", wk, PLURAL(wk)); 1449373Seric p += strlen(p); 1459373Seric } 1469373Seric if (dy > 0) 1479373Seric { 1489373Seric (void) sprintf(p, ", %d day%s", dy, PLURAL(dy)); 1499373Seric p += strlen(p); 1509373Seric } 1519373Seric if (hr > 0) 1529373Seric { 1539373Seric (void) sprintf(p, ", %d hour%s", hr, PLURAL(hr)); 1549373Seric p += strlen(p); 1559373Seric } 1569373Seric if (mi > 0) 1579373Seric { 1589373Seric (void) sprintf(p, ", %d minute%s", mi, PLURAL(mi)); 1599373Seric p += strlen(p); 1609373Seric } 1619373Seric if (se > 0) 1629373Seric { 1639373Seric (void) sprintf(p, ", %d second%s", se, PLURAL(se)); 1649373Seric p += strlen(p); 1659373Seric } 1669373Seric 1679373Seric return (buf + 2); 1689373Seric } 169