]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/clock.c
c6c52bf89f7bdb3de365664fabe316c7be237d8d
[thirdparty/bash.git] / lib / sh / clock.c
1 /* clock.c - operations on struct tms and clock_t's */
2
3 /* Copyright (C) 1999 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <config.h>
22
23 #if defined (HAVE_TIMES)
24
25 #include <sys/types.h>
26 #include <posixtime.h>
27
28 #if defined (HAVE_SYS_TIMES_H)
29 # include <sys/times.h>
30 #endif
31
32 #include <stdio.h>
33 #include <stdc.h>
34
35 #include <bashintl.h>
36
37 #ifndef locale_decpoint
38 extern int locale_decpoint PARAMS((void));
39 #endif
40
41 extern long get_clk_tck PARAMS((void));
42
43 void
44 clock_t_to_secs (t, sp, sfp)
45 clock_t t;
46 time_t *sp;
47 int *sfp;
48 {
49 static long clk_tck = -1;
50
51 if (clk_tck == -1)
52 clk_tck = get_clk_tck ();
53
54 *sfp = t % clk_tck;
55 *sfp = (*sfp * 1000) / clk_tck;
56
57 *sp = t / clk_tck;
58
59 /* Sanity check */
60 if (*sfp >= 1000)
61 {
62 *sp += 1;
63 *sfp -= 1000;
64 }
65 }
66
67 /* Print the time defined by a clock_t (returned by the `times' and `time'
68 system calls) in a standard way to stdio stream FP. This is scaled in
69 terms of the value of CLK_TCK, which is what is returned by the
70 `times' call. */
71 void
72 print_clock_t (fp, t)
73 FILE *fp;
74 clock_t t;
75 {
76 time_t timestamp;
77 long minutes;
78 int seconds, seconds_fraction;
79
80 clock_t_to_secs (t, &timestamp, &seconds_fraction);
81
82 minutes = timestamp / 60;
83 seconds = timestamp % 60;
84
85 fprintf (fp, "%ldm%d%c%03ds", minutes, seconds, locale_decpoint(), seconds_fraction);
86 }
87 #endif /* HAVE_TIMES */