Debian Codegraph @

libhylafax/FmtTime.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1990-1996 Sam Leffler
4 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 #include <time.h>
27
28 /*
29 * Convert a time_t to a compact printable
30 * ``HH:MM:SS'' string. Note that time values
31 * are expected to not overflow this range;
32 * if they do, the ``upper part'' of the value
33 * is discarded.
34 */
35 const char*
36 fmtTime(time_t t)
37 {
38 static char tbuf[10];
39 const char* digits = "0123456789";
40 char* cp = tbuf;
41 long v;
42
43 if (t < 0)
44 return ("0:00:00");
45 if (t > 365*24*60*60)
46 return ("??:??:??");
47 v = t/3600;
48 if (v >= 1000)
49 *cp++ = digits[v / 1000];
50 if (v >= 100)
51 *cp++ = digits[(v % 1000) / 100];
52 if (v >= 10)
53 *cp++ = digits[((v % 1000) % 100) / 10];
54 *cp++ = digits[((v % 1000) % 100) % 10];
55 *cp++ = ':';
56 t -= v*3600;
57 v = t/60;
58 *cp++ = digits[v / 10];
59 *cp++ = digits[v % 10];
60 t -= v*60;
61 *cp++ = ':';
62 *cp++ = digits[t / 10];
63 *cp++ = digits[t % 10];
64 *cp = '\0';
65 return tbuf;
66 }