122699Sdist /*
268839Seric * Copyright (c) 1983, 1995 Eric P. Allman
362522Sbostic * Copyright (c) 1988, 1993
462522Sbostic * The Regents of the University of California. All rights reserved.
533728Sbostic *
642825Sbostic * %sccs.include.redist.c%
733728Sbostic */
822699Sdist
922699Sdist #ifndef lint
10*69562Seric static char sccsid[] = "@(#)convtime.c 8.4 (Berkeley) 05/19/95";
1133728Sbostic #endif /* not lint */
1222699Sdist
134617Seric # include <ctype.h>
14*69562Seric # include <string.h>
155200Seric # include "useful.h"
164617Seric
174617Seric /*
184617Seric ** CONVTIME -- convert time
194617Seric **
205200Seric ** Takes a time as an ascii string with a trailing character
215200Seric ** giving units:
225200Seric ** s -- seconds
235200Seric ** m -- minutes
245200Seric ** h -- hours
255200Seric ** d -- days (default)
265200Seric ** w -- weeks
279373Seric ** For example, "3d12h" is three and a half days.
285200Seric **
294617Seric ** Parameters:
304617Seric ** p -- pointer to ascii time.
3158796Seric ** units -- default units if none specified.
324617Seric **
334617Seric ** Returns:
344617Seric ** time in seconds.
354617Seric **
364617Seric ** Side Effects:
374617Seric ** none.
384617Seric */
394617Seric
404617Seric time_t
convtime(p,units)4158796Seric convtime(p, units)
424617Seric char *p;
4358796Seric char units;
444617Seric {
455200Seric register time_t t, r;
4611996Seric register char c;
474617Seric
485200Seric r = 0;
495200Seric while (*p != '\0')
504617Seric {
515200Seric t = 0;
5260537Seric while ((c = *p++) != '\0' && isascii(c) && isdigit(c))
5311996Seric t = t * 10 + (c - '0');
5411996Seric if (c == '\0')
5558796Seric {
5658796Seric c = units;
5711996Seric p--;
5858796Seric }
5968504Seric else if (strchr("wdhms", c) == NULL)
6068504Seric {
6168504Seric usrerr("Invalid time unit `%c'", c);
6268504Seric c = units;
6368504Seric }
6411996Seric switch (c)
655200Seric {
665200Seric case 'w': /* weeks */
675200Seric t *= 7;
684617Seric
695200Seric case 'd': /* days */
705200Seric default:
715200Seric t *= 24;
724617Seric
735200Seric case 'h': /* hours */
745200Seric t *= 60;
754617Seric
765200Seric case 'm': /* minutes */
775200Seric t *= 60;
785200Seric
795200Seric case 's': /* seconds */
805200Seric break;
815200Seric }
825200Seric r += t;
834617Seric }
844617Seric
855200Seric return (r);
864617Seric }
879373Seric /*
889373Seric ** PINTVL -- produce printable version of a time interval
899373Seric **
909373Seric ** Parameters:
919373Seric ** intvl -- the interval to be converted
929373Seric ** brief -- if TRUE, print this in an extremely compact form
939373Seric ** (basically used for logging).
949373Seric **
959373Seric ** Returns:
969373Seric ** A pointer to a string version of intvl suitable for
979373Seric ** printing or framing.
989373Seric **
999373Seric ** Side Effects:
1009373Seric ** none.
1019373Seric **
1029373Seric ** Warning:
1039373Seric ** The string returned is in a static buffer.
1049373Seric */
1059373Seric
1069373Seric # define PLURAL(n) ((n) == 1 ? "" : "s")
1079373Seric
1089373Seric char *
pintvl(intvl,brief)1099373Seric pintvl(intvl, brief)
1109373Seric time_t intvl;
1119373Seric bool brief;
1129373Seric {
1139373Seric static char buf[256];
1149373Seric register char *p;
1159373Seric int wk, dy, hr, mi, se;
1169373Seric
1179373Seric if (intvl == 0 && !brief)
1189373Seric return ("zero seconds");
1199373Seric
1209373Seric /* decode the interval into weeks, days, hours, minutes, seconds */
1219373Seric se = intvl % 60;
1229373Seric intvl /= 60;
1239373Seric mi = intvl % 60;
1249373Seric intvl /= 60;
1259373Seric hr = intvl % 24;
1269373Seric intvl /= 24;
1279373Seric if (brief)
1289373Seric dy = intvl;
1299373Seric else
1309373Seric {
1319373Seric dy = intvl % 7;
1329373Seric intvl /= 7;
1339373Seric wk = intvl;
1349373Seric }
1359373Seric
1369373Seric /* now turn it into a sexy form */
1379373Seric p = buf;
1389373Seric if (brief)
1399373Seric {
1409373Seric if (dy > 0)
1419373Seric {
1429373Seric (void) sprintf(p, "%d+", dy);
1439373Seric p += strlen(p);
1449373Seric }
1459373Seric (void) sprintf(p, "%02d:%02d:%02d", hr, mi, se);
1469373Seric return (buf);
1479373Seric }
1489373Seric
1499373Seric /* use the verbose form */
1509373Seric if (wk > 0)
1519373Seric {
1529373Seric (void) sprintf(p, ", %d week%s", wk, PLURAL(wk));
1539373Seric p += strlen(p);
1549373Seric }
1559373Seric if (dy > 0)
1569373Seric {
1579373Seric (void) sprintf(p, ", %d day%s", dy, PLURAL(dy));
1589373Seric p += strlen(p);
1599373Seric }
1609373Seric if (hr > 0)
1619373Seric {
1629373Seric (void) sprintf(p, ", %d hour%s", hr, PLURAL(hr));
1639373Seric p += strlen(p);
1649373Seric }
1659373Seric if (mi > 0)
1669373Seric {
1679373Seric (void) sprintf(p, ", %d minute%s", mi, PLURAL(mi));
1689373Seric p += strlen(p);
1699373Seric }
1709373Seric if (se > 0)
1719373Seric {
1729373Seric (void) sprintf(p, ", %d second%s", se, PLURAL(se));
1739373Seric p += strlen(p);
1749373Seric }
1759373Seric
1769373Seric return (buf + 2);
1779373Seric }
178