523
|
1 // timefns.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
523
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #include "dMatrix.h" |
|
29 |
|
30 #include "tree-const.h" |
|
31 #include "oct-obj.h" |
792
|
32 #include "systime.h" |
523
|
33 #include "defun.h" |
|
34 |
940
|
35 #ifdef HAVE_SYS_RESOURCE_H |
|
36 extern "C" |
|
37 { |
902
|
38 #include <sys/resource.h> |
940
|
39 } |
|
40 #endif |
902
|
41 |
|
42 #ifndef RUSAGE_SELF |
|
43 #define RUSAGE_SELF 0 |
|
44 #endif |
|
45 |
935
|
46 |
523
|
47 DEFUN ("clock", Fclock, Sclock, 1, 0, |
|
48 "clock (): return current date and time in vector with elements\n\ |
|
49 \n\ |
|
50 [ year, month, day-of-month, hour, minute, second ]") |
|
51 { |
|
52 time_t now; |
792
|
53 double fraction = 0.0; |
523
|
54 |
792
|
55 #ifdef HAVE_GETTIMEOFDAY |
902
|
56 |
792
|
57 struct timeval tp; |
902
|
58 |
792
|
59 gettimeofday (&tp, 0); |
902
|
60 |
792
|
61 now = tp.tv_sec; |
902
|
62 |
792
|
63 fraction = tp.tv_usec / 1e6; |
902
|
64 |
792
|
65 #else |
902
|
66 |
523
|
67 time (&now); |
902
|
68 |
792
|
69 #endif |
|
70 |
902
|
71 struct tm *tm = localtime (&now); |
523
|
72 |
|
73 Matrix m (1, 6); |
|
74 m.elem (0, 0) = tm->tm_year + 1900; |
|
75 m.elem (0, 1) = tm->tm_mon + 1; |
|
76 m.elem (0, 2) = tm->tm_mday; |
|
77 m.elem (0, 3) = tm->tm_hour; |
|
78 m.elem (0, 4) = tm->tm_min; |
792
|
79 m.elem (0, 5) = tm->tm_sec + fraction; |
523
|
80 |
|
81 return m; |
|
82 } |
|
83 |
902
|
84 DEFUN ("cputime", Fcputime, Scputime, 0, 0, |
|
85 "[total, user, system] = cputime ()\n\ |
|
86 \n\ |
|
87 Return CPU time statistics.") |
|
88 { |
|
89 Octave_object retval (3, Matrix (1, 1, 0.0)); |
|
90 |
|
91 #if defined (HAVE_GETRUSAGE) |
|
92 |
|
93 struct rusage resource_stats; |
|
94 |
|
95 getrusage (RUSAGE_SELF, &resource_stats); |
|
96 |
|
97 struct timeval usr = resource_stats.ru_utime; |
|
98 struct timeval sys = resource_stats.ru_stime; |
|
99 |
|
100 double usr_time = usr.tv_sec + usr.tv_usec / 1e6; |
|
101 double sys_time = sys.tv_sec + sys.tv_usec / 1e6; |
|
102 |
|
103 retval (2) = sys_time; |
|
104 retval (1) = usr_time; |
|
105 retval (0) = usr_time + sys_time; |
|
106 |
|
107 #endif |
|
108 |
|
109 return retval; |
|
110 } |
|
111 |
523
|
112 DEFUN ("date", Fdate, Sdate, 1, 0, |
|
113 "date (): return current date in a string, in the form `18-Jul-94'") |
|
114 { |
|
115 Octave_object retval; |
|
116 |
|
117 time_t now; |
|
118 struct tm *tm; |
|
119 |
|
120 time (&now); |
|
121 tm = localtime (&now); |
|
122 char date[32]; |
|
123 int len = strftime (date, 31, "%d-%b-%y", tm); |
|
124 if (len > 0) |
|
125 retval = date; |
|
126 |
|
127 return retval; |
|
128 } |
|
129 |
|
130 /* |
|
131 ;;; Local Variables: *** |
|
132 ;;; mode: C++ *** |
|
133 ;;; page-delimiter: "^/\\*" *** |
|
134 ;;; End: *** |
|
135 */ |