1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * return pointer to formatted elapsed time for u 1/n secs
1N/A * compatible with strelapsed()
1N/A * return value length is at most 7
1N/A */
1N/A
1N/A#include <ast.h>
1N/A
1N/Achar*
1N/Afmtelapsed(register unsigned long u, register int n)
1N/A{
1N/A register unsigned long t;
1N/A char* buf;
1N/A int z;
1N/A
1N/A if (u == 0L)
1N/A return "0";
1N/A if (u == ~0L)
1N/A return "%";
1N/A buf = fmtbuf(z = 8);
1N/A t = u / n;
1N/A if (t < 60)
1N/A sfsprintf(buf, z, "%lu.%02lus", t, (u * 100 / n) % 100);
1N/A else if (t < 60*60)
1N/A sfsprintf(buf, z, "%lum%02lus", t / 60, t - (t / 60) * 60);
1N/A else if (t < 24*60*60)
1N/A sfsprintf(buf, z, "%luh%02lum", t / (60*60), (t - (t / (60*60)) * (60*60)) / 60);
1N/A else if (t < 7*24*60*60)
1N/A sfsprintf(buf, z, "%lud%02luh", t / (24*60*60), (t - (t / (24*60*60)) * (24*60*60)) / (60*60));
1N/A else if (t < 31*24*60*60)
1N/A sfsprintf(buf, z, "%luw%02lud", t / (7*24*60*60), (t - (t / (7*24*60*60)) * (7*24*60*60)) / (24*60*60));
1N/A else if (t < 365*24*60*60)
1N/A 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));
1N/A else if (t < (365UL*4UL+1UL)*24UL*60UL*60UL)
1N/A 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));
1N/A else
1N/A 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));
1N/A return buf;
1N/A}