14617Seric # include <ctype.h>
25200Seric # include "useful.h"
34617Seric 
4*11996Seric SCCSID(@(#)convtime.c	3.4		04/19/83);
54617Seric 
64617Seric /*
74617Seric **  CONVTIME -- convert time
84617Seric **
95200Seric **	Takes a time as an ascii string with a trailing character
105200Seric **	giving units:
115200Seric **	  s -- seconds
125200Seric **	  m -- minutes
135200Seric **	  h -- hours
145200Seric **	  d -- days (default)
155200Seric **	  w -- weeks
169373Seric **	For example, "3d12h" is three and a half days.
175200Seric **
184617Seric **	Parameters:
194617Seric **		p -- pointer to ascii time.
204617Seric **
214617Seric **	Returns:
224617Seric **		time in seconds.
234617Seric **
244617Seric **	Side Effects:
254617Seric **		none.
264617Seric */
274617Seric 
284617Seric time_t
294617Seric convtime(p)
304617Seric 	char *p;
314617Seric {
325200Seric 	register time_t t, r;
33*11996Seric 	register char c;
344617Seric 
355200Seric 	r = 0;
365200Seric 	while (*p != '\0')
374617Seric 	{
385200Seric 		t = 0;
39*11996Seric 		while (isdigit(c = *p++))
40*11996Seric 			t = t * 10 + (c - '0');
41*11996Seric 		if (c == '\0')
42*11996Seric 			p--;
43*11996Seric 		switch (c)
445200Seric 		{
455200Seric 		  case 'w':		/* weeks */
465200Seric 			t *= 7;
474617Seric 
485200Seric 		  case 'd':		/* days */
495200Seric 		  default:
505200Seric 			t *= 24;
514617Seric 
525200Seric 		  case 'h':		/* hours */
535200Seric 			t *= 60;
544617Seric 
555200Seric 		  case 'm':		/* minutes */
565200Seric 			t *= 60;
575200Seric 
585200Seric 		  case 's':		/* seconds */
595200Seric 			break;
605200Seric 		}
615200Seric 		r += t;
624617Seric 	}
634617Seric 
645200Seric 	return (r);
654617Seric }
669373Seric /*
679373Seric **  PINTVL -- produce printable version of a time interval
689373Seric **
699373Seric **	Parameters:
709373Seric **		intvl -- the interval to be converted
719373Seric **		brief -- if TRUE, print this in an extremely compact form
729373Seric **			(basically used for logging).
739373Seric **
749373Seric **	Returns:
759373Seric **		A pointer to a string version of intvl suitable for
769373Seric **			printing or framing.
779373Seric **
789373Seric **	Side Effects:
799373Seric **		none.
809373Seric **
819373Seric **	Warning:
829373Seric **		The string returned is in a static buffer.
839373Seric */
849373Seric 
859373Seric # define PLURAL(n)	((n) == 1 ? "" : "s")
869373Seric 
879373Seric char *
889373Seric pintvl(intvl, brief)
899373Seric 	time_t intvl;
909373Seric 	bool brief;
919373Seric {
929373Seric 	static char buf[256];
939373Seric 	register char *p;
949373Seric 	int wk, dy, hr, mi, se;
959373Seric 
969373Seric 	if (intvl == 0 && !brief)
979373Seric 		return ("zero seconds");
989373Seric 
999373Seric 	/* decode the interval into weeks, days, hours, minutes, seconds */
1009373Seric 	se = intvl % 60;
1019373Seric 	intvl /= 60;
1029373Seric 	mi = intvl % 60;
1039373Seric 	intvl /= 60;
1049373Seric 	hr = intvl % 24;
1059373Seric 	intvl /= 24;
1069373Seric 	if (brief)
1079373Seric 		dy = intvl;
1089373Seric 	else
1099373Seric 	{
1109373Seric 		dy = intvl % 7;
1119373Seric 		intvl /= 7;
1129373Seric 		wk = intvl;
1139373Seric 	}
1149373Seric 
1159373Seric 	/* now turn it into a sexy form */
1169373Seric 	p = buf;
1179373Seric 	if (brief)
1189373Seric 	{
1199373Seric 		if (dy > 0)
1209373Seric 		{
1219373Seric 			(void) sprintf(p, "%d+", dy);
1229373Seric 			p += strlen(p);
1239373Seric 		}
1249373Seric 		(void) sprintf(p, "%02d:%02d:%02d", hr, mi, se);
1259373Seric 		return (buf);
1269373Seric 	}
1279373Seric 
1289373Seric 	/* use the verbose form */
1299373Seric 	if (wk > 0)
1309373Seric 	{
1319373Seric 		(void) sprintf(p, ", %d week%s", wk, PLURAL(wk));
1329373Seric 		p += strlen(p);
1339373Seric 	}
1349373Seric 	if (dy > 0)
1359373Seric 	{
1369373Seric 		(void) sprintf(p, ", %d day%s", dy, PLURAL(dy));
1379373Seric 		p += strlen(p);
1389373Seric 	}
1399373Seric 	if (hr > 0)
1409373Seric 	{
1419373Seric 		(void) sprintf(p, ", %d hour%s", hr, PLURAL(hr));
1429373Seric 		p += strlen(p);
1439373Seric 	}
1449373Seric 	if (mi > 0)
1459373Seric 	{
1469373Seric 		(void) sprintf(p, ", %d minute%s", mi, PLURAL(mi));
1479373Seric 		p += strlen(p);
1489373Seric 	}
1499373Seric 	if (se > 0)
1509373Seric 	{
1519373Seric 		(void) sprintf(p, ", %d second%s", se, PLURAL(se));
1529373Seric 		p += strlen(p);
1539373Seric 	}
1549373Seric 
1559373Seric 	return (buf + 2);
1569373Seric }
157