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*58050Seric static char sccsid[] = "@(#)convtime.c	6.2 (Berkeley) 02/18/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.
304617Seric **
314617Seric **	Returns:
324617Seric **		time in seconds.
334617Seric **
344617Seric **	Side Effects:
354617Seric **		none.
364617Seric */
374617Seric 
384617Seric time_t
394617Seric convtime(p)
404617Seric 	char *p;
414617Seric {
425200Seric 	register time_t t, r;
4311996Seric 	register char c;
444617Seric 
455200Seric 	r = 0;
465200Seric 	while (*p != '\0')
474617Seric 	{
485200Seric 		t = 0;
49*58050Seric 		while (isascii(c = *p++) && isdigit(c))
5011996Seric 			t = t * 10 + (c - '0');
5111996Seric 		if (c == '\0')
5211996Seric 			p--;
5311996Seric 		switch (c)
545200Seric 		{
555200Seric 		  case 'w':		/* weeks */
565200Seric 			t *= 7;
574617Seric 
585200Seric 		  case 'd':		/* days */
595200Seric 		  default:
605200Seric 			t *= 24;
614617Seric 
625200Seric 		  case 'h':		/* hours */
635200Seric 			t *= 60;
644617Seric 
655200Seric 		  case 'm':		/* minutes */
665200Seric 			t *= 60;
675200Seric 
685200Seric 		  case 's':		/* seconds */
695200Seric 			break;
705200Seric 		}
715200Seric 		r += t;
724617Seric 	}
734617Seric 
745200Seric 	return (r);
754617Seric }
769373Seric /*
779373Seric **  PINTVL -- produce printable version of a time interval
789373Seric **
799373Seric **	Parameters:
809373Seric **		intvl -- the interval to be converted
819373Seric **		brief -- if TRUE, print this in an extremely compact form
829373Seric **			(basically used for logging).
839373Seric **
849373Seric **	Returns:
859373Seric **		A pointer to a string version of intvl suitable for
869373Seric **			printing or framing.
879373Seric **
889373Seric **	Side Effects:
899373Seric **		none.
909373Seric **
919373Seric **	Warning:
929373Seric **		The string returned is in a static buffer.
939373Seric */
949373Seric 
959373Seric # define PLURAL(n)	((n) == 1 ? "" : "s")
969373Seric 
979373Seric char *
989373Seric pintvl(intvl, brief)
999373Seric 	time_t intvl;
1009373Seric 	bool brief;
1019373Seric {
1029373Seric 	static char buf[256];
1039373Seric 	register char *p;
1049373Seric 	int wk, dy, hr, mi, se;
1059373Seric 
1069373Seric 	if (intvl == 0 && !brief)
1079373Seric 		return ("zero seconds");
1089373Seric 
1099373Seric 	/* decode the interval into weeks, days, hours, minutes, seconds */
1109373Seric 	se = intvl % 60;
1119373Seric 	intvl /= 60;
1129373Seric 	mi = intvl % 60;
1139373Seric 	intvl /= 60;
1149373Seric 	hr = intvl % 24;
1159373Seric 	intvl /= 24;
1169373Seric 	if (brief)
1179373Seric 		dy = intvl;
1189373Seric 	else
1199373Seric 	{
1209373Seric 		dy = intvl % 7;
1219373Seric 		intvl /= 7;
1229373Seric 		wk = intvl;
1239373Seric 	}
1249373Seric 
1259373Seric 	/* now turn it into a sexy form */
1269373Seric 	p = buf;
1279373Seric 	if (brief)
1289373Seric 	{
1299373Seric 		if (dy > 0)
1309373Seric 		{
1319373Seric 			(void) sprintf(p, "%d+", dy);
1329373Seric 			p += strlen(p);
1339373Seric 		}
1349373Seric 		(void) sprintf(p, "%02d:%02d:%02d", hr, mi, se);
1359373Seric 		return (buf);
1369373Seric 	}
1379373Seric 
1389373Seric 	/* use the verbose form */
1399373Seric 	if (wk > 0)
1409373Seric 	{
1419373Seric 		(void) sprintf(p, ", %d week%s", wk, PLURAL(wk));
1429373Seric 		p += strlen(p);
1439373Seric 	}
1449373Seric 	if (dy > 0)
1459373Seric 	{
1469373Seric 		(void) sprintf(p, ", %d day%s", dy, PLURAL(dy));
1479373Seric 		p += strlen(p);
1489373Seric 	}
1499373Seric 	if (hr > 0)
1509373Seric 	{
1519373Seric 		(void) sprintf(p, ", %d hour%s", hr, PLURAL(hr));
1529373Seric 		p += strlen(p);
1539373Seric 	}
1549373Seric 	if (mi > 0)
1559373Seric 	{
1569373Seric 		(void) sprintf(p, ", %d minute%s", mi, PLURAL(mi));
1579373Seric 		p += strlen(p);
1589373Seric 	}
1599373Seric 	if (se > 0)
1609373Seric 	{
1619373Seric 		(void) sprintf(p, ", %d second%s", se, PLURAL(se));
1629373Seric 		p += strlen(p);
1639373Seric 	}
1649373Seric 
1659373Seric 	return (buf + 2);
1669373Seric }
167