1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that the above copyright notice and this paragraph are
8  * duplicated in all such forms and that any documentation,
9  * advertising materials, and other materials related to such
10  * distribution and use acknowledge that the software was developed
11  * by the University of California, Berkeley.  The name of the
12  * University may not be used to endorse or promote products derived
13  * from this software without specific prior written permission.
14  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17  */
18 
19 #ifndef lint
20 static char sccsid[] = "@(#)convtime.c	5.3 (Berkeley) 06/30/88";
21 #endif /* not lint */
22 
23 # include <ctype.h>
24 # include "useful.h"
25 
26 /*
27 **  CONVTIME -- convert time
28 **
29 **	Takes a time as an ascii string with a trailing character
30 **	giving units:
31 **	  s -- seconds
32 **	  m -- minutes
33 **	  h -- hours
34 **	  d -- days (default)
35 **	  w -- weeks
36 **	For example, "3d12h" is three and a half days.
37 **
38 **	Parameters:
39 **		p -- pointer to ascii time.
40 **
41 **	Returns:
42 **		time in seconds.
43 **
44 **	Side Effects:
45 **		none.
46 */
47 
48 time_t
49 convtime(p)
50 	char *p;
51 {
52 	register time_t t, r;
53 	register char c;
54 
55 	r = 0;
56 	while (*p != '\0')
57 	{
58 		t = 0;
59 		while (isdigit(c = *p++))
60 			t = t * 10 + (c - '0');
61 		if (c == '\0')
62 			p--;
63 		switch (c)
64 		{
65 		  case 'w':		/* weeks */
66 			t *= 7;
67 
68 		  case 'd':		/* days */
69 		  default:
70 			t *= 24;
71 
72 		  case 'h':		/* hours */
73 			t *= 60;
74 
75 		  case 'm':		/* minutes */
76 			t *= 60;
77 
78 		  case 's':		/* seconds */
79 			break;
80 		}
81 		r += t;
82 	}
83 
84 	return (r);
85 }
86 /*
87 **  PINTVL -- produce printable version of a time interval
88 **
89 **	Parameters:
90 **		intvl -- the interval to be converted
91 **		brief -- if TRUE, print this in an extremely compact form
92 **			(basically used for logging).
93 **
94 **	Returns:
95 **		A pointer to a string version of intvl suitable for
96 **			printing or framing.
97 **
98 **	Side Effects:
99 **		none.
100 **
101 **	Warning:
102 **		The string returned is in a static buffer.
103 */
104 
105 # define PLURAL(n)	((n) == 1 ? "" : "s")
106 
107 char *
108 pintvl(intvl, brief)
109 	time_t intvl;
110 	bool brief;
111 {
112 	static char buf[256];
113 	register char *p;
114 	int wk, dy, hr, mi, se;
115 
116 	if (intvl == 0 && !brief)
117 		return ("zero seconds");
118 
119 	/* decode the interval into weeks, days, hours, minutes, seconds */
120 	se = intvl % 60;
121 	intvl /= 60;
122 	mi = intvl % 60;
123 	intvl /= 60;
124 	hr = intvl % 24;
125 	intvl /= 24;
126 	if (brief)
127 		dy = intvl;
128 	else
129 	{
130 		dy = intvl % 7;
131 		intvl /= 7;
132 		wk = intvl;
133 	}
134 
135 	/* now turn it into a sexy form */
136 	p = buf;
137 	if (brief)
138 	{
139 		if (dy > 0)
140 		{
141 			(void) sprintf(p, "%d+", dy);
142 			p += strlen(p);
143 		}
144 		(void) sprintf(p, "%02d:%02d:%02d", hr, mi, se);
145 		return (buf);
146 	}
147 
148 	/* use the verbose form */
149 	if (wk > 0)
150 	{
151 		(void) sprintf(p, ", %d week%s", wk, PLURAL(wk));
152 		p += strlen(p);
153 	}
154 	if (dy > 0)
155 	{
156 		(void) sprintf(p, ", %d day%s", dy, PLURAL(dy));
157 		p += strlen(p);
158 	}
159 	if (hr > 0)
160 	{
161 		(void) sprintf(p, ", %d hour%s", hr, PLURAL(hr));
162 		p += strlen(p);
163 	}
164 	if (mi > 0)
165 	{
166 		(void) sprintf(p, ", %d minute%s", mi, PLURAL(mi));
167 		p += strlen(p);
168 	}
169 	if (se > 0)
170 	{
171 		(void) sprintf(p, ", %d second%s", se, PLURAL(se));
172 		p += strlen(p);
173 	}
174 
175 	return (buf + 2);
176 }
177