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" |
1184
|
33 #include "oct-map.h" |
523
|
34 #include "defun.h" |
1184
|
35 #include "utils.h" |
|
36 #include "help.h" |
523
|
37 |
940
|
38 #ifdef HAVE_SYS_RESOURCE_H |
|
39 extern "C" |
|
40 { |
902
|
41 #include <sys/resource.h> |
940
|
42 } |
|
43 #endif |
902
|
44 |
|
45 #ifndef RUSAGE_SELF |
|
46 #define RUSAGE_SELF 0 |
|
47 #endif |
|
48 |
1184
|
49 // CPU time functions. |
523
|
50 |
902
|
51 DEFUN ("cputime", Fcputime, Scputime, 0, 0, |
|
52 "[total, user, system] = cputime ()\n\ |
|
53 \n\ |
|
54 Return CPU time statistics.") |
|
55 { |
|
56 Octave_object retval (3, Matrix (1, 1, 0.0)); |
|
57 |
|
58 #if defined (HAVE_GETRUSAGE) |
|
59 |
|
60 struct rusage resource_stats; |
|
61 |
|
62 getrusage (RUSAGE_SELF, &resource_stats); |
|
63 |
|
64 struct timeval usr = resource_stats.ru_utime; |
|
65 struct timeval sys = resource_stats.ru_stime; |
|
66 |
|
67 double usr_time = usr.tv_sec + usr.tv_usec / 1e6; |
|
68 double sys_time = sys.tv_sec + sys.tv_usec / 1e6; |
|
69 |
|
70 retval (2) = sys_time; |
|
71 retval (1) = usr_time; |
|
72 retval (0) = usr_time + sys_time; |
|
73 |
|
74 #endif |
|
75 |
|
76 return retval; |
|
77 } |
|
78 |
1184
|
79 // Date and time functions. |
|
80 |
|
81 static Octave_map |
|
82 mk_tm_map (struct tm *tm, double fraction) |
|
83 { |
|
84 Octave_map m; |
|
85 |
|
86 m ["tm_usec"] = fraction * 1e6; |
|
87 m ["tm_sec"] = (double) tm->tm_sec; |
|
88 m ["tm_min"] = (double) tm->tm_min; |
|
89 m ["tm_hour"] = (double) tm->tm_hour; |
|
90 m ["tm_mday"] = (double) tm->tm_mday; |
|
91 m ["tm_mon"] = (double) tm->tm_mon; |
|
92 m ["tm_year"] = (double) tm->tm_year; |
|
93 m ["tm_wday"] = (double) tm->tm_wday; |
|
94 m ["tm_yday"] = (double) tm->tm_yday; |
|
95 m ["tm_isdst"] = (double) tm->tm_isdst; |
|
96 #if defined (HAVE_TM_ZONE) |
|
97 m ["tm_zone"] = tm->tm_zone; |
|
98 #elif defined (HAVE_TZNAME) |
|
99 if (tm->tm_isdst && tzname[1] && *tzname[1]) |
|
100 m ["tm_zone"] = tzname[1]; |
|
101 else |
|
102 m ["tm_zone"] = tzname[0]; |
|
103 #else |
|
104 m ["tm_zone"] = zone_name (tm); |
|
105 #endif |
|
106 |
|
107 return m; |
|
108 } |
|
109 |
|
110 static struct tm* |
|
111 extract_tm (Octave_map &m, double &fraction) |
|
112 { |
|
113 static struct tm tm; |
|
114 |
|
115 fraction = NINT (m ["tm_usec"] . double_value ()); |
|
116 tm.tm_sec = NINT (m ["tm_sec"] . double_value ()); |
|
117 tm.tm_min = NINT (m ["tm_min"] . double_value ()); |
|
118 tm.tm_hour = NINT (m ["tm_hour"] . double_value ()); |
|
119 tm.tm_mday = NINT (m ["tm_mday"] . double_value ()); |
|
120 tm.tm_mon = NINT (m ["tm_mon"] . double_value ()); |
|
121 tm.tm_year = NINT (m ["tm_year"] . double_value ()); |
|
122 tm.tm_wday = NINT (m ["tm_wday"] . double_value ()); |
|
123 tm.tm_yday = NINT (m ["tm_yday"] . double_value ()); |
|
124 tm.tm_isdst = NINT (m ["tm_isdst"] . double_value ()); |
|
125 tm.tm_zone = (m ["tm_zone"] . string_value ()); |
|
126 |
|
127 return &tm; |
|
128 } |
|
129 |
|
130 DEFUN ("time", Ftime, Stime, 1, 0, |
|
131 "time ()\n\ |
|
132 \n\ |
|
133 Return current time. On Unix systems, this is the number of\n\ |
|
134 seconds since the epoch.") |
|
135 { |
|
136 time_t now; |
|
137 double fraction = 0.0; |
|
138 |
|
139 #ifdef HAVE_GETTIMEOFDAY |
|
140 |
|
141 struct timeval tp; |
|
142 |
|
143 gettimeofday (&tp, 0); |
|
144 |
|
145 now = tp.tv_sec; |
|
146 |
|
147 fraction = tp.tv_usec / 1e6; |
|
148 |
|
149 #else |
|
150 |
|
151 now = time (0); |
|
152 |
|
153 #endif |
|
154 |
|
155 return (double) now + fraction; |
|
156 } |
|
157 |
|
158 DEFUN ("gmtime", Fgmtime, Sgmtime, 1, 1, |
|
159 "gmtime (TIME)\n\ |
|
160 \n\ |
|
161 Given a value returned from time(), return a structure like that\n\ |
|
162 returned from localtime() but with values corresponding to\n\ |
|
163 Coordinated Universal Time (UTC).") |
|
164 { |
|
165 Octave_object retval; |
|
166 |
|
167 if (args.length () == 1) |
|
168 { |
|
169 double tmp = args(0).double_value (); |
|
170 |
|
171 if (! error_state) |
|
172 { |
|
173 time_t timeval = NINT (tmp); |
|
174 double ip; |
|
175 double fraction = modf (tmp, &ip); |
|
176 |
|
177 retval = tree_constant (mk_tm_map (gmtime (&timeval), fraction)); |
|
178 } |
|
179 } |
|
180 else |
|
181 print_usage ("gmtime"); |
|
182 |
|
183 return retval; |
|
184 } |
|
185 |
|
186 DEFUN ("localtime", Flocaltime, Slocaltime, 1, 1, |
|
187 "localtime (TIME)\n\ |
|
188 \n\ |
|
189 Given a value returned from time(), return a structure with\n\ |
|
190 the following elements:\n\ |
|
191 \n\ |
|
192 tm_usec : microseconds after the second (0, 999999)\n\ |
|
193 tm_sec : seconds after the minute (0, 61)\n\ |
|
194 tm_min : minutes after the hour (0, 59)\n\ |
|
195 tm_hour : hours since midnight (0, 23)\n\ |
|
196 tm_mday : day of the month (1, 31)\n\ |
|
197 tm_mon : months since January (0, 11)\n\ |
|
198 tm_year : years since 1900\n\ |
|
199 tm_wday : days since Sunday (0, 6)\n\ |
|
200 tm_yday : days since January 1 (0, 365)\n\ |
|
201 tm_isdst : Daylight Savings Time flag\n\ |
|
202 tm_zone : Time zone") |
523
|
203 { |
|
204 Octave_object retval; |
|
205 |
1184
|
206 if (args.length () == 1) |
|
207 { |
|
208 double tmp = args(0).double_value (); |
|
209 |
|
210 if (! error_state) |
|
211 { |
|
212 time_t timeval = NINT (tmp); |
|
213 double ip; |
|
214 double fraction = modf (tmp, &ip); |
|
215 |
|
216 retval = tree_constant (mk_tm_map (localtime (&timeval), fraction)); |
|
217 } |
|
218 } |
|
219 else |
|
220 print_usage ("localtime"); |
|
221 |
|
222 return retval; |
|
223 } |
|
224 |
|
225 DEFUN ("mktime", Fmktime, Smktime, 1, 2, |
|
226 "mktime (TMSTRUCT)") |
|
227 { |
|
228 Octave_object retval; |
|
229 |
1187
|
230 if (args.length () == 1 && args(0).is_map ()) |
1184
|
231 { |
|
232 Octave_map map = args(0).map_value (); |
|
233 |
|
234 double fraction; |
|
235 |
|
236 struct tm *tm = extract_tm (map, fraction); |
|
237 |
|
238 if (! error_state) |
|
239 retval = (double) mktime (tm) + fraction; |
|
240 } |
|
241 else |
|
242 print_usage ("mktime"); |
|
243 |
|
244 return retval; |
|
245 } |
523
|
246 |
1184
|
247 DEFUN ("strftime", Fstrftime, Sstrftime, 1, 2, |
|
248 "strftime (FMT, TMSTRUCT)\n\ |
|
249 \n\ |
|
250 Performs `%' substitutions similar to those in printf. Except where\n\ |
|
251 noted, substituted fields have a fixed size; numeric fields are\n\ |
|
252 padded if necessary. Padding is with zeros by default; for fields\n\ |
|
253 that display a single number, padding can be changed or inhibited by\n\ |
|
254 following the `%' with one of the modifiers described below.\n\ |
|
255 Unknown field specifiers are copied as normal characters. All other\n\ |
|
256 characters are copied to the output without change.\n\ |
|
257 \n\ |
|
258 Supports a superset of the ANSI C field specifiers.\n\ |
|
259 \n\ |
|
260 Literal character fields:\n\ |
|
261 \n\ |
|
262 % %\n\ |
|
263 n newline\n\ |
|
264 t tab\n\ |
|
265 \n\ |
|
266 Numeric modifiers (a nonstandard extension):\n\ |
|
267 \n\ |
|
268 - do not pad the field\n\ |
|
269 _ pad the field with spaces\n\ |
|
270 \n\ |
|
271 Time fields:\n\ |
|
272 \n\ |
|
273 %H hour (00..23)\n\ |
|
274 %I hour (01..12)\n\ |
|
275 %k hour ( 0..23)\n\ |
|
276 %l hour ( 1..12)\n\ |
|
277 %M minute (00..59)\n\ |
|
278 %p locale's AM or PM\n\ |
|
279 %r time, 12-hour (hh:mm:ss [AP]M)\n\ |
|
280 %R time, 24-hour (hh:mm)\n\ |
|
281 %s time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension)\n\ |
|
282 %S second (00..61)\n\ |
|
283 %T time, 24-hour (hh:mm:ss)\n\ |
|
284 %X locale's time representation (%H:%M:%S)\n\ |
|
285 %Z time zone (EDT), or nothing if no time zone is determinable\n\ |
|
286 \n\ |
|
287 Date fields:\n\ |
|
288 \n\ |
|
289 %a locale's abbreviated weekday name (Sun..Sat)\n\ |
|
290 %A locale's full weekday name, variable length (Sunday..Saturday)\n\ |
|
291 %b locale's abbreviated month name (Jan..Dec)\n\ |
|
292 %B locale's full month name, variable length (January..December)\n\ |
|
293 %c locale's date and time (Sat Nov 04 12:02:33 EST 1989)\n\ |
|
294 %C century (00..99)\n\ |
|
295 %d day of month (01..31)\n\ |
|
296 %e day of month ( 1..31)\n\ |
|
297 %D date (mm/dd/yy)\n\ |
|
298 %h same as %b\n\ |
|
299 %j day of year (001..366)\n\ |
|
300 %m month (01..12)\n\ |
|
301 %U week number of year with Sunday as first day of week (00..53)\n\ |
|
302 %w day of week (0..6)\n\ |
|
303 %W week number of year with Monday as first day of week (00..53)\n\ |
|
304 %x locale's date representation (mm/dd/yy)\n\ |
|
305 %y last two digits of year (00..99)\n\ |
|
306 %Y year (1970...)") |
|
307 { |
|
308 Octave_object retval; |
|
309 |
|
310 if (args.length () == 2 && args(0).is_string () && args(1).is_map ()) |
|
311 { |
|
312 char *fmt = args(0).string_value (); |
|
313 Octave_map map = args(1).map_value (); |
|
314 |
|
315 double fraction; |
|
316 |
|
317 struct tm *tm = extract_tm (map, fraction); |
|
318 |
|
319 if (! error_state) |
|
320 { |
|
321 int bufsize = 128; |
|
322 char *buf = new char [bufsize]; |
|
323 |
|
324 while (! strftime (buf, bufsize, fmt, tm)) |
|
325 { |
|
326 delete [] buf; |
|
327 bufsize *= 2; |
|
328 buf = new char [bufsize]; |
|
329 } |
|
330 |
|
331 retval = buf; |
|
332 |
|
333 delete [] buf; |
|
334 } |
|
335 } |
|
336 else |
|
337 print_usage ("strftime"); |
523
|
338 |
|
339 return retval; |
|
340 } |
|
341 |
|
342 /* |
|
343 ;;; Local Variables: *** |
|
344 ;;; mode: C++ *** |
|
345 ;;; page-delimiter: "^/\\*" *** |
|
346 ;;; End: *** |
|
347 */ |