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
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_TIMEVAL)
24
25#include <sys/types.h>
26#include <posixtime.h>
27
28#include <stdio.h>
29
30struct timeval *
31difftimeval (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
49struct timeval *
50addtimeval (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>. */
65int
66timeval_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. */
103void
104timeval_to_secs (tvp, sp, sfp)
105     struct timeval *tvp;
106     time_t *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.  */
129void
130print_timeval (fp, tvp)
131     FILE *fp;
132     struct timeval *tvp;
133{
134  time_t timestamp;
135  long minutes;
136  int seconds, seconds_fraction;
137
138  timeval_to_secs (tvp, &timestamp, &seconds_fraction);
139
140  minutes = timestamp / 60;
141  seconds = timestamp % 60;
142
143  fprintf (fp, "%ldm%d.%03ds",  minutes, seconds, seconds_fraction);
144}
145#endif /* HAVE_TIMEVAL */
146