]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/timeval.c
809d411fbf032ce4b19998806e35137110218850
1 /* timeval.c - functions to perform operations on struct timevals */
3 /* Copyright (C) 1999 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
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
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
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. */
23 #if defined (HAVE_TIMEVAL)
25 #include <sys/types.h>
26 #include <posixtime.h>
31 difftimeval (d
, t1
, t2
)
32 struct timeval
*d
, *t1
, *t2
;
34 d
->tv_sec
= t2
->tv_sec
- t1
->tv_sec
;
35 d
->tv_usec
= t2
->tv_usec
- t1
->tv_usec
;
38 d
->tv_usec
+= 1000000;
40 if (d
->tv_sec
< 0) /* ??? -- BSD/OS does this */
50 addtimeval (d
, t1
, t2
)
51 struct timeval
*d
, *t1
, *t2
;
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)
57 d
->tv_usec
-= 1000000;
63 /* Do "cpu = ((user + sys) * 10000) / real;" with timevals.
64 Barely-tested code from Deven T. Corzine <deven@ties.org>. */
66 timeval_to_cpu (rt
, ut
, st
)
67 struct timeval
*rt
, *ut
, *st
; /* real, user, sys */
69 struct timeval t1
, t2
;
72 addtimeval (&t1
, ut
, st
);
73 t2
.tv_sec
= rt
->tv_sec
;
74 t2
.tv_usec
= rt
->tv_usec
;
76 for (i
= 0; i
< 6; i
++)
78 if ((t1
.tv_sec
> 99999999) || (t2
.tv_sec
> 99999999))
81 t1
.tv_sec
+= t1
.tv_usec
/ 100000;
83 t1
.tv_usec
%= 1000000;
85 t2
.tv_sec
+= t2
.tv_usec
/ 100000;
87 t2
.tv_usec
%= 1000000;
89 for (i
= 0; i
< 4; i
++)
91 if (t1
.tv_sec
< 100000000)
97 return ((t2
.tv_sec
== 0) ? 0 : t1
.tv_sec
/ t2
.tv_sec
);
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. */
104 timeval_to_secs (tvp
, sp
, sfp
)
113 *sfp
= tvp
->tv_usec
% 1000000; /* pretty much a no-op */
115 *sfp
= (*sfp
* 1000) / 1000000;
127 /* Print the contents of a struct timeval * in a standard way to stdio
130 print_timeval (fp
, tvp
)
134 int minutes
, seconds_fraction
;
137 timeval_to_secs (tvp
, &seconds
, &seconds_fraction
);
139 minutes
= seconds
/ 60;
142 fprintf (fp
, "%0dm%0ld.%03ds", minutes
, seconds
, seconds_fraction
);
144 #endif /* HAVE_TIMEVAL */