File: clock.c

package info (click to toggle)
bash 5.0-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 38,540 kB
  • sloc: ansic: 107,425; sh: 7,641; yacc: 5,401; makefile: 4,410; perl: 4,227; asm: 48; awk: 23; sed: 16
file content (87 lines) | stat: -rw-r--r-- 2,055 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* clock.c - operations on struct tms and clock_t's */

/* Copyright (C) 1999 Free Software Foundation, Inc.

   This file is part of GNU Bash, the Bourne Again SHell.

   Bash is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Bash is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

#if defined (HAVE_TIMES)

#include <sys/types.h>
#include <posixtime.h>

#if defined (HAVE_SYS_TIMES_H)
#  include <sys/times.h>
#endif

#include <stdio.h>
#include <stdc.h>

#include <bashintl.h>

#ifndef locale_decpoint
extern int locale_decpoint PARAMS((void));
#endif

extern long get_clk_tck PARAMS((void));

void
clock_t_to_secs (t, sp, sfp)
     clock_t t;
     time_t *sp;
     int *sfp;
{
  static long clk_tck = -1;

  if (clk_tck == -1)
    clk_tck = get_clk_tck ();

  *sfp = t % clk_tck;
  *sfp = (*sfp * 1000) / clk_tck;

  *sp = t / clk_tck;

  /* Sanity check */
  if (*sfp >= 1000)
    {
      *sp += 1;
      *sfp -= 1000;
    }
}

/* Print the time defined by a clock_t (returned by the `times' and `time'
   system calls) in a standard way to stdio stream FP.  This is scaled in
   terms of the value of CLK_TCK, which is what is returned by the
   `times' call. */
void
print_clock_t (fp, t)
     FILE *fp;
     clock_t t;
{
  time_t timestamp;
  long minutes;
  int seconds, seconds_fraction;

  clock_t_to_secs (t, &timestamp, &seconds_fraction);

  minutes = timestamp / 60;
  seconds = timestamp % 60;

  fprintf (fp, "%ldm%d%c%03ds",  minutes, seconds, locale_decpoint(), seconds_fraction);
}
#endif /* HAVE_TIMES */