]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/timeval.c
809d411fbf032ce4b19998806e35137110218850
[thirdparty/bash.git] / lib / sh / timeval.c
1 /* timeval.c - functions to perform operations on struct timevals */
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 it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20
21 #include <config.h>
22
23 #if defined (HAVE_TIMEVAL)
24
25 #include <sys/types.h>
26 #include <posixtime.h>
27
28 #include <stdio.h>
29
30 struct timeval *
31 difftimeval (d, t1, t2)
32 struct timeval *d, *t1, *t2;
33 {
34 d->tv_sec = t2->tv_sec - t1->tv_sec;
35 d->tv_usec = t2->tv_usec - t1->tv_usec;
36 if (d->tv_usec < 0)
37 {
38 d->tv_usec += 1000000;
39 d->tv_sec -= 1;
40 if (d->tv_sec < 0) /* ??? -- BSD/OS does this */
41 {
42 d->tv_sec = 0;
43 d->tv_usec = 0;
44 }
45 }
46 return d;
47 }
48
49 struct timeval *
50 addtimeval (d, t1, t2)
51 struct timeval *d, *t1, *t2;
52 {
53 d->tv_sec = t1->tv_sec + t2->tv_sec;
54 d->tv_usec = t1->tv_usec + t2->tv_usec;
55 if (d->tv_usec >= 1000000)
56 {
57 d->tv_usec -= 1000000;
58 d->tv_sec += 1;
59 }
60 return d;
61 }
62
63 /* Do "cpu = ((user + sys) * 10000) / real;" with timevals.
64 Barely-tested code from Deven T. Corzine <deven@ties.org>. */
65 int
66 timeval_to_cpu (rt, ut, st)
67 struct timeval *rt, *ut, *st; /* real, user, sys */
68 {
69 struct timeval t1, t2;
70 register int i;
71
72 addtimeval (&t1, ut, st);
73 t2.tv_sec = rt->tv_sec;
74 t2.tv_usec = rt->tv_usec;
75
76 for (i = 0; i < 6; i++)
77 {
78 if ((t1.tv_sec > 99999999) || (t2.tv_sec > 99999999))
79 break;
80 t1.tv_sec *= 10;
81 t1.tv_sec += t1.tv_usec / 100000;
82 t1.tv_usec *= 10;
83 t1.tv_usec %= 1000000;
84 t2.tv_sec *= 10;
85 t2.tv_sec += t2.tv_usec / 100000;
86 t2.tv_usec *= 10;
87 t2.tv_usec %= 1000000;
88 }
89 for (i = 0; i < 4; i++)
90 {
91 if (t1.tv_sec < 100000000)
92 t1.tv_sec *= 10;
93 else
94 t2.tv_sec /= 10;
95 }
96
97 return ((t2.tv_sec == 0) ? 0 : t1.tv_sec / t2.tv_sec);
98 }
99
100 /* Convert a pointer to a struct timeval to seconds and thousandths of a
101 second, returning the values in *SP and *SFP, respectively. This does
102 rounding on the fractional part, not just truncation to three places. */
103 void
104 timeval_to_secs (tvp, sp, sfp)
105 struct timeval *tvp;
106 long *sp;
107 int *sfp;
108 {
109 int rest;
110
111 *sp = tvp->tv_sec;
112
113 *sfp = tvp->tv_usec % 1000000; /* pretty much a no-op */
114 rest = *sfp % 1000;
115 *sfp = (*sfp * 1000) / 1000000;
116 if (rest >= 500)
117 *sfp += 1;
118
119 /* Sanity check */
120 if (*sfp >= 1000)
121 {
122 *sp += 1;
123 *sfp -= 1000;
124 }
125 }
126
127 /* Print the contents of a struct timeval * in a standard way to stdio
128 stream FP. */
129 void
130 print_timeval (fp, tvp)
131 FILE *fp;
132 struct timeval *tvp;
133 {
134 int minutes, seconds_fraction;
135 long seconds;
136
137 timeval_to_secs (tvp, &seconds, &seconds_fraction);
138
139 minutes = seconds / 60;
140 seconds %= 60;
141
142 fprintf (fp, "%0dm%0ld.%03ds", minutes, seconds, seconds_fraction);
143 }
144 #endif /* HAVE_TIMEVAL */