1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin /*
24*4887Schin  * return pointer to formatted elapsed time for u 1/n secs
25*4887Schin  * compatible with strelapsed()
26*4887Schin  * return value length is at most 7
27*4887Schin  */
28*4887Schin 
29*4887Schin #include <ast.h>
30*4887Schin 
31*4887Schin char*
32*4887Schin fmtelapsed(register unsigned long u, register int n)
33*4887Schin {
34*4887Schin 	register unsigned long	t;
35*4887Schin 	char*			buf;
36*4887Schin 	int			z;
37*4887Schin 
38*4887Schin 	if (u == 0L)
39*4887Schin 		return "0";
40*4887Schin 	if (u == ~0L)
41*4887Schin 		return "%";
42*4887Schin 	buf = fmtbuf(z = 8);
43*4887Schin 	t = u / n;
44*4887Schin 	if (t < 60)
45*4887Schin 		sfsprintf(buf, z, "%lu.%02lus", t, (u * 100 / n) % 100);
46*4887Schin 	else if (t < 60*60)
47*4887Schin 		sfsprintf(buf, z, "%lum%02lus", t / 60, t - (t / 60) * 60);
48*4887Schin 	else if (t < 24*60*60)
49*4887Schin 		sfsprintf(buf, z, "%luh%02lum", t / (60*60), (t - (t / (60*60)) * (60*60)) / 60);
50*4887Schin 	else if (t < 7*24*60*60)
51*4887Schin 		sfsprintf(buf, z, "%lud%02luh", t / (24*60*60), (t - (t / (24*60*60)) * (24*60*60)) / (60*60));
52*4887Schin 	else if (t < 31*24*60*60)
53*4887Schin 		sfsprintf(buf, z, "%luw%02lud", t / (7*24*60*60), (t - (t / (7*24*60*60)) * (7*24*60*60)) / (24*60*60));
54*4887Schin 	else if (t < 365*24*60*60)
55*4887Schin 		sfsprintf(buf, z, "%luM%02lud", (t * 12) / (365*24*60*60), ((t * 12) - ((t * 12) / (365*24*60*60)) * (365*24*60*60)) / (12*24*60*60));
56*4887Schin 	else if (t < (365UL*4UL+1UL)*24UL*60UL*60UL)
57*4887Schin 		sfsprintf(buf, z, "%luY%02luM", t / (365*24*60*60), ((t - (t / (365*24*60*60)) * (365*24*60*60)) * 5) / (152 * 24 * 60 * 60));
58*4887Schin 	else
59*4887Schin 		sfsprintf(buf, z, "%luY%02luM", (t * 4) / ((365UL*4UL+1UL)*24UL*60UL*60UL), (((t * 4) - ((t * 4) / ((365UL*4UL+1UL)*24UL*60UL*60UL)) * ((365UL*4UL+1UL)*24UL*60UL*60UL)) * 5) / ((4 * 152 + 1) * 24 * 60 * 60));
60*4887Schin 	return buf;
61*4887Schin }
62