122699Sdist /*
2*34920Sbostic  * Copyright (c) 1983 Eric P. Allman
333728Sbostic  * Copyright (c) 1988 Regents of the University of California.
433728Sbostic  * All rights reserved.
533728Sbostic  *
633728Sbostic  * Redistribution and use in source and binary forms are permitted
7*34920Sbostic  * provided that the above copyright notice and this paragraph are
8*34920Sbostic  * duplicated in all such forms and that any documentation,
9*34920Sbostic  * advertising materials, and other materials related to such
10*34920Sbostic  * distribution and use acknowledge that the software was developed
11*34920Sbostic  * by the University of California, Berkeley.  The name of the
12*34920Sbostic  * University may not be used to endorse or promote products derived
13*34920Sbostic  * from this software without specific prior written permission.
14*34920Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15*34920Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16*34920Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1733728Sbostic  */
1822699Sdist 
1922699Sdist #ifndef lint
20*34920Sbostic static char sccsid[] = "@(#)convtime.c	5.3 (Berkeley) 06/30/88";
2133728Sbostic #endif /* not lint */
2222699Sdist 
234617Seric # include <ctype.h>
245200Seric # include "useful.h"
254617Seric 
264617Seric /*
274617Seric **  CONVTIME -- convert time
284617Seric **
295200Seric **	Takes a time as an ascii string with a trailing character
305200Seric **	giving units:
315200Seric **	  s -- seconds
325200Seric **	  m -- minutes
335200Seric **	  h -- hours
345200Seric **	  d -- days (default)
355200Seric **	  w -- weeks
369373Seric **	For example, "3d12h" is three and a half days.
375200Seric **
384617Seric **	Parameters:
394617Seric **		p -- pointer to ascii time.
404617Seric **
414617Seric **	Returns:
424617Seric **		time in seconds.
434617Seric **
444617Seric **	Side Effects:
454617Seric **		none.
464617Seric */
474617Seric 
484617Seric time_t
494617Seric convtime(p)
504617Seric 	char *p;
514617Seric {
525200Seric 	register time_t t, r;
5311996Seric 	register char c;
544617Seric 
555200Seric 	r = 0;
565200Seric 	while (*p != '\0')
574617Seric 	{
585200Seric 		t = 0;
5911996Seric 		while (isdigit(c = *p++))
6011996Seric 			t = t * 10 + (c - '0');
6111996Seric 		if (c == '\0')
6211996Seric 			p--;
6311996Seric 		switch (c)
645200Seric 		{
655200Seric 		  case 'w':		/* weeks */
665200Seric 			t *= 7;
674617Seric 
685200Seric 		  case 'd':		/* days */
695200Seric 		  default:
705200Seric 			t *= 24;
714617Seric 
725200Seric 		  case 'h':		/* hours */
735200Seric 			t *= 60;
744617Seric 
755200Seric 		  case 'm':		/* minutes */
765200Seric 			t *= 60;
775200Seric 
785200Seric 		  case 's':		/* seconds */
795200Seric 			break;
805200Seric 		}
815200Seric 		r += t;
824617Seric 	}
834617Seric 
845200Seric 	return (r);
854617Seric }
869373Seric /*
879373Seric **  PINTVL -- produce printable version of a time interval
889373Seric **
899373Seric **	Parameters:
909373Seric **		intvl -- the interval to be converted
919373Seric **		brief -- if TRUE, print this in an extremely compact form
929373Seric **			(basically used for logging).
939373Seric **
949373Seric **	Returns:
959373Seric **		A pointer to a string version of intvl suitable for
969373Seric **			printing or framing.
979373Seric **
989373Seric **	Side Effects:
999373Seric **		none.
1009373Seric **
1019373Seric **	Warning:
1029373Seric **		The string returned is in a static buffer.
1039373Seric */
1049373Seric 
1059373Seric # define PLURAL(n)	((n) == 1 ? "" : "s")
1069373Seric 
1079373Seric char *
1089373Seric pintvl(intvl, brief)
1099373Seric 	time_t intvl;
1109373Seric 	bool brief;
1119373Seric {
1129373Seric 	static char buf[256];
1139373Seric 	register char *p;
1149373Seric 	int wk, dy, hr, mi, se;
1159373Seric 
1169373Seric 	if (intvl == 0 && !brief)
1179373Seric 		return ("zero seconds");
1189373Seric 
1199373Seric 	/* decode the interval into weeks, days, hours, minutes, seconds */
1209373Seric 	se = intvl % 60;
1219373Seric 	intvl /= 60;
1229373Seric 	mi = intvl % 60;
1239373Seric 	intvl /= 60;
1249373Seric 	hr = intvl % 24;
1259373Seric 	intvl /= 24;
1269373Seric 	if (brief)
1279373Seric 		dy = intvl;
1289373Seric 	else
1299373Seric 	{
1309373Seric 		dy = intvl % 7;
1319373Seric 		intvl /= 7;
1329373Seric 		wk = intvl;
1339373Seric 	}
1349373Seric 
1359373Seric 	/* now turn it into a sexy form */
1369373Seric 	p = buf;
1379373Seric 	if (brief)
1389373Seric 	{
1399373Seric 		if (dy > 0)
1409373Seric 		{
1419373Seric 			(void) sprintf(p, "%d+", dy);
1429373Seric 			p += strlen(p);
1439373Seric 		}
1449373Seric 		(void) sprintf(p, "%02d:%02d:%02d", hr, mi, se);
1459373Seric 		return (buf);
1469373Seric 	}
1479373Seric 
1489373Seric 	/* use the verbose form */
1499373Seric 	if (wk > 0)
1509373Seric 	{
1519373Seric 		(void) sprintf(p, ", %d week%s", wk, PLURAL(wk));
1529373Seric 		p += strlen(p);
1539373Seric 	}
1549373Seric 	if (dy > 0)
1559373Seric 	{
1569373Seric 		(void) sprintf(p, ", %d day%s", dy, PLURAL(dy));
1579373Seric 		p += strlen(p);
1589373Seric 	}
1599373Seric 	if (hr > 0)
1609373Seric 	{
1619373Seric 		(void) sprintf(p, ", %d hour%s", hr, PLURAL(hr));
1629373Seric 		p += strlen(p);
1639373Seric 	}
1649373Seric 	if (mi > 0)
1659373Seric 	{
1669373Seric 		(void) sprintf(p, ", %d minute%s", mi, PLURAL(mi));
1679373Seric 		p += strlen(p);
1689373Seric 	}
1699373Seric 	if (se > 0)
1709373Seric 	{
1719373Seric 		(void) sprintf(p, ", %d second%s", se, PLURAL(se));
1729373Seric 		p += strlen(p);
1739373Seric 	}
1749373Seric 
1759373Seric 	return (buf + 2);
1769373Seric }
177