14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * David Korn <dgk@research.att.com> *
194887Schin * Phong Vo <kpv@research.att.com> *
204887Schin * *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin * return pointer to formatted elapsed time for u 1/n secs
254887Schin * compatible with strelapsed()
264887Schin * return value length is at most 7
274887Schin */
284887Schin
294887Schin #include <ast.h>
304887Schin
314887Schin char*
fmtelapsed(register unsigned long u,register int n)324887Schin fmtelapsed(register unsigned long u, register int n)
334887Schin {
344887Schin register unsigned long t;
354887Schin char* buf;
364887Schin int z;
374887Schin
384887Schin if (u == 0L)
394887Schin return "0";
404887Schin if (u == ~0L)
414887Schin return "%";
424887Schin buf = fmtbuf(z = 8);
434887Schin t = u / n;
444887Schin if (t < 60)
454887Schin sfsprintf(buf, z, "%lu.%02lus", t, (u * 100 / n) % 100);
464887Schin else if (t < 60*60)
474887Schin sfsprintf(buf, z, "%lum%02lus", t / 60, t - (t / 60) * 60);
484887Schin else if (t < 24*60*60)
494887Schin sfsprintf(buf, z, "%luh%02lum", t / (60*60), (t - (t / (60*60)) * (60*60)) / 60);
504887Schin else if (t < 7*24*60*60)
514887Schin sfsprintf(buf, z, "%lud%02luh", t / (24*60*60), (t - (t / (24*60*60)) * (24*60*60)) / (60*60));
524887Schin else if (t < 31*24*60*60)
534887Schin sfsprintf(buf, z, "%luw%02lud", t / (7*24*60*60), (t - (t / (7*24*60*60)) * (7*24*60*60)) / (24*60*60));
544887Schin else if (t < 365*24*60*60)
554887Schin 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));
564887Schin else if (t < (365UL*4UL+1UL)*24UL*60UL*60UL)
574887Schin 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));
584887Schin else
594887Schin 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));
604887Schin return buf;
614887Schin }
62