14617Seric # include <ctype.h>
25200Seric # include "useful.h"
34617Seric 
4*9373Seric SCCSID(@(#)convtime.c	3.3		11/28/82);
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
16*9373Seric **	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;
334617Seric 
345200Seric 	r = 0;
355200Seric 	while (*p != '\0')
364617Seric 	{
375200Seric 		t = 0;
385200Seric 		while (isdigit(*p))
395200Seric 			t = t * 10 + (*p++ - '0');
405200Seric 		switch (*p++)
415200Seric 		{
425200Seric 		  case 'w':		/* weeks */
435200Seric 			t *= 7;
444617Seric 
455200Seric 		  case '\0':
465200Seric 			p--;
475200Seric 			/* fall through... */
484617Seric 
495200Seric 		  case 'd':		/* days */
505200Seric 		  default:
515200Seric 			t *= 24;
524617Seric 
535200Seric 		  case 'h':		/* hours */
545200Seric 			t *= 60;
554617Seric 
565200Seric 		  case 'm':		/* minutes */
575200Seric 			t *= 60;
585200Seric 
595200Seric 		  case 's':		/* seconds */
605200Seric 			break;
615200Seric 		}
625200Seric 		r += t;
634617Seric 	}
644617Seric 
655200Seric 	return (r);
664617Seric }
67*9373Seric /*
68*9373Seric **  PINTVL -- produce printable version of a time interval
69*9373Seric **
70*9373Seric **	Parameters:
71*9373Seric **		intvl -- the interval to be converted
72*9373Seric **		brief -- if TRUE, print this in an extremely compact form
73*9373Seric **			(basically used for logging).
74*9373Seric **
75*9373Seric **	Returns:
76*9373Seric **		A pointer to a string version of intvl suitable for
77*9373Seric **			printing or framing.
78*9373Seric **
79*9373Seric **	Side Effects:
80*9373Seric **		none.
81*9373Seric **
82*9373Seric **	Warning:
83*9373Seric **		The string returned is in a static buffer.
84*9373Seric */
85*9373Seric 
86*9373Seric # define PLURAL(n)	((n) == 1 ? "" : "s")
87*9373Seric 
88*9373Seric char *
89*9373Seric pintvl(intvl, brief)
90*9373Seric 	time_t intvl;
91*9373Seric 	bool brief;
92*9373Seric {
93*9373Seric 	static char buf[256];
94*9373Seric 	register char *p;
95*9373Seric 	int wk, dy, hr, mi, se;
96*9373Seric 
97*9373Seric 	if (intvl == 0 && !brief)
98*9373Seric 		return ("zero seconds");
99*9373Seric 
100*9373Seric 	/* decode the interval into weeks, days, hours, minutes, seconds */
101*9373Seric 	se = intvl % 60;
102*9373Seric 	intvl /= 60;
103*9373Seric 	mi = intvl % 60;
104*9373Seric 	intvl /= 60;
105*9373Seric 	hr = intvl % 24;
106*9373Seric 	intvl /= 24;
107*9373Seric 	if (brief)
108*9373Seric 		dy = intvl;
109*9373Seric 	else
110*9373Seric 	{
111*9373Seric 		dy = intvl % 7;
112*9373Seric 		intvl /= 7;
113*9373Seric 		wk = intvl;
114*9373Seric 	}
115*9373Seric 
116*9373Seric 	/* now turn it into a sexy form */
117*9373Seric 	p = buf;
118*9373Seric 	if (brief)
119*9373Seric 	{
120*9373Seric 		if (dy > 0)
121*9373Seric 		{
122*9373Seric 			(void) sprintf(p, "%d+", dy);
123*9373Seric 			p += strlen(p);
124*9373Seric 		}
125*9373Seric 		(void) sprintf(p, "%02d:%02d:%02d", hr, mi, se);
126*9373Seric 		return (buf);
127*9373Seric 	}
128*9373Seric 
129*9373Seric 	/* use the verbose form */
130*9373Seric 	if (wk > 0)
131*9373Seric 	{
132*9373Seric 		(void) sprintf(p, ", %d week%s", wk, PLURAL(wk));
133*9373Seric 		p += strlen(p);
134*9373Seric 	}
135*9373Seric 	if (dy > 0)
136*9373Seric 	{
137*9373Seric 		(void) sprintf(p, ", %d day%s", dy, PLURAL(dy));
138*9373Seric 		p += strlen(p);
139*9373Seric 	}
140*9373Seric 	if (hr > 0)
141*9373Seric 	{
142*9373Seric 		(void) sprintf(p, ", %d hour%s", hr, PLURAL(hr));
143*9373Seric 		p += strlen(p);
144*9373Seric 	}
145*9373Seric 	if (mi > 0)
146*9373Seric 	{
147*9373Seric 		(void) sprintf(p, ", %d minute%s", mi, PLURAL(mi));
148*9373Seric 		p += strlen(p);
149*9373Seric 	}
150*9373Seric 	if (se > 0)
151*9373Seric 	{
152*9373Seric 		(void) sprintf(p, ", %d second%s", se, PLURAL(se));
153*9373Seric 		p += strlen(p);
154*9373Seric 	}
155*9373Seric 
156*9373Seric 	return (buf + 2);
157*9373Seric }
158