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