1 /*
2 * This code is from procps-ng (w.c)
3 *
4 * Almost entirely rewritten from scratch by Charles Blake circa
5 * June 1996. Some vestigal traces of the original may exist.
6 * That was done in 1993 by Larry Greenfield with some fixes by
7 * Michael K. Johnson.
8 *
9 * Changes by Albert Cahalan, 2002.
10 * Modified for occtl by Nikos Mavrogiannopoulos, 2014.
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include <config.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <time.h>
30 #include <occtl/occtl.h>
31
32 #define _(x) x
33
34 /* compact 7 char format for time intervals (belongs in libproc?) */
print_time_ival7(char output[MAX_TMPSTR_SIZE],time_t t1,time_t t2)35 void print_time_ival7(char output[MAX_TMPSTR_SIZE], time_t t1, time_t t2)
36 {
37 time_t t = t1 - t2;
38
39 if ((long long)t < (long long)0) {
40 /* system clock changed? */
41 snprintf(output, MAX_TMPSTR_SIZE, " ? ");
42 return;
43 }
44
45 if (t >= 48 * 60 * 60)
46 /* 2 days or more */
47 snprintf(output, MAX_TMPSTR_SIZE, _("%2lludays"), (long long)t / (24 * 60 * 60));
48 else if (t >= 60 * 60)
49 /* 1 hour or more */
50 /* Translation Hint: Hours:Minutes */
51 snprintf(output, MAX_TMPSTR_SIZE, _("%2lluh:%02um"), (long long)t / (60 * 60),
52 (unsigned)((t / 60) % 60));
53 else if (t > 60)
54 /* 1 minute or more */
55 /* Translation Hint: Minutes:Seconds */
56 snprintf(output, MAX_TMPSTR_SIZE, "%2llum:%02us", (long long)t / 60, (unsigned)t % 60);
57 else
58 /* Translation Hint: Seconds:Centiseconds */
59 snprintf(output, MAX_TMPSTR_SIZE, _("%5llus"), (long long)t);
60 }
61