2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <string> |
|
28 |
|
29 #include "defun-dld.h" |
|
30 #include "error.h" |
|
31 #include "oct-map.h" |
3254
|
32 #include "oct-time.h" |
2928
|
33 #include "ov.h" |
|
34 #include "oct-obj.h" |
|
35 |
|
36 // Date and time functions. |
|
37 |
|
38 static Octave_map |
3254
|
39 mk_tm_map (const octave_base_tm& t) |
2928
|
40 { |
|
41 Octave_map m; |
|
42 |
4675
|
43 m.assign ("usec", static_cast<double> (t.usec ())); |
|
44 m.assign ("sec", static_cast<double> (t.sec ())); |
|
45 m.assign ("min", static_cast<double> (t.min ())); |
|
46 m.assign ("hour", static_cast<double> (t.hour ())); |
|
47 m.assign ("mday", static_cast<double> (t.mday ())); |
|
48 m.assign ("mon", static_cast<double> (t.mon ())); |
|
49 m.assign ("year", static_cast<double> (t.year ())); |
|
50 m.assign ("wday", static_cast<double> (t.wday ())); |
|
51 m.assign ("yday", static_cast<double> (t.yday ())); |
|
52 m.assign ("isdst", static_cast<double> (t.isdst ())); |
|
53 m.assign ("zone", t.zone ()); |
2928
|
54 |
|
55 return m; |
|
56 } |
|
57 |
3254
|
58 static octave_base_tm |
|
59 extract_tm (Octave_map &m) |
2928
|
60 { |
3254
|
61 octave_base_tm tm; |
2928
|
62 |
5156
|
63 tm.usec (m.intfield ("usec")); |
|
64 tm.sec (m.intfield ("sec")); |
|
65 tm.min (m.intfield ("min")); |
|
66 tm.hour (m.intfield ("hour")); |
|
67 tm.mday (m.intfield ("mday")); |
|
68 tm.mon (m.intfield ("mon")); |
|
69 tm.year (m.intfield ("year")); |
|
70 tm.wday (m.intfield ("wday")); |
|
71 tm.yday (m.intfield ("yday")); |
|
72 tm.isdst (m.intfield ("isdst")); |
|
73 tm.zone (m.stringfield ("zone")); |
2928
|
74 |
3254
|
75 return tm; |
2928
|
76 } |
|
77 |
3255
|
78 DEFUN_DLD (time, args, , |
3301
|
79 "-*- texinfo -*-\n\ |
|
80 @deftypefn {Loadable Function} {} time ()\n\ |
|
81 Return the current time as the number of seconds since the epoch. The\n\ |
|
82 epoch is referenced to 00:00:00 CUT (Coordinated Universal Time) 1 Jan\n\ |
|
83 1970. For example, on Monday February 17, 1997 at 07:15:06 CUT, the\n\ |
|
84 value returned by @code{time} was 856163706.\n\ |
|
85 @end deftypefn") |
2928
|
86 { |
3255
|
87 octave_value retval; |
2928
|
88 |
3255
|
89 if (args.length () == 0) |
|
90 retval = static_cast<double> (octave_time ()); |
|
91 else |
|
92 print_usage ("time"); |
|
93 |
|
94 return retval; |
2928
|
95 } |
|
96 |
|
97 DEFUN_DLD (gmtime, args, , |
3301
|
98 "-*- texinfo -*-\n\ |
|
99 @deftypefn {Loadable Function} {} gmtime (@var{t})\n\ |
|
100 Given a value returned from time (or any nonnegative integer),\n\ |
|
101 return a time structure corresponding to CUT. For example,\n\ |
2928
|
102 \n\ |
3301
|
103 @example\n\ |
|
104 @group\n\ |
|
105 gmtime (time ())\n\ |
|
106 @result{} @{\n\ |
|
107 usec = 0\n\ |
|
108 year = 97\n\ |
|
109 mon = 1\n\ |
|
110 mday = 17\n\ |
|
111 sec = 6\n\ |
|
112 zone = CST\n\ |
|
113 min = 15\n\ |
|
114 wday = 1\n\ |
|
115 hour = 7\n\ |
|
116 isdst = 0\n\ |
|
117 yday = 47\n\ |
|
118 @}\n\ |
|
119 @end group\n\ |
|
120 @end example\n\ |
|
121 @end deftypefn") |
2928
|
122 { |
3465
|
123 octave_value retval; |
2928
|
124 |
|
125 if (args.length () == 1) |
|
126 { |
|
127 double tmp = args(0).double_value (); |
|
128 |
|
129 if (! error_state) |
3254
|
130 retval = octave_value (mk_tm_map (octave_gmtime (tmp))); |
2928
|
131 } |
|
132 else |
|
133 print_usage ("gmtime"); |
|
134 |
|
135 return retval; |
|
136 } |
|
137 |
|
138 DEFUN_DLD (localtime, args, , |
3301
|
139 "-*- texinfo -*-\n\ |
|
140 @deftypefn {Loadable Function} {} localtime (@var{t})\n\ |
|
141 Given a value returned from time (or any nonnegative integer),\n\ |
|
142 return a time structure corresponding to the local time zone.\n\ |
2928
|
143 \n\ |
3301
|
144 @example\n\ |
|
145 @group\n\ |
|
146 localtime (time ())\n\ |
|
147 @result{} @{\n\ |
|
148 usec = 0\n\ |
|
149 year = 97\n\ |
|
150 mon = 1\n\ |
|
151 mday = 17\n\ |
|
152 sec = 6\n\ |
|
153 zone = CST\n\ |
|
154 min = 15\n\ |
|
155 wday = 1\n\ |
|
156 hour = 1\n\ |
|
157 isdst = 0\n\ |
|
158 yday = 47\n\ |
|
159 @}\n\ |
|
160 @end group\n\ |
|
161 @end example\n\ |
|
162 @end deftypefn") |
2928
|
163 { |
3465
|
164 octave_value retval; |
2928
|
165 |
|
166 if (args.length () == 1) |
|
167 { |
|
168 double tmp = args(0).double_value (); |
|
169 |
|
170 if (! error_state) |
3254
|
171 retval = octave_value (mk_tm_map (octave_localtime (tmp))); |
2928
|
172 } |
|
173 else |
|
174 print_usage ("localtime"); |
|
175 |
|
176 return retval; |
|
177 } |
|
178 |
|
179 DEFUN_DLD (mktime, args, , |
3301
|
180 "-*- texinfo -*-\n\ |
|
181 @deftypefn {Loadable Function} {} mktime (@var{tm_struct})\n\ |
|
182 Convert a time structure corresponding to the local time to the number\n\ |
|
183 of seconds since the epoch. For example,\n\ |
|
184 \n\ |
|
185 @example\n\ |
|
186 @group\n\ |
|
187 mktime (localtime (time ())\n\ |
|
188 @result{} 856163706\n\ |
|
189 @end group\n\ |
|
190 @end example\n\ |
|
191 @end deftypefn") |
2928
|
192 { |
3465
|
193 octave_value retval; |
2928
|
194 |
3254
|
195 if (args.length () == 1) |
2928
|
196 { |
|
197 Octave_map map = args(0).map_value (); |
|
198 |
3254
|
199 if (! error_state) |
|
200 { |
|
201 octave_base_tm tm = extract_tm (map); |
|
202 |
|
203 if (! error_state) |
3255
|
204 retval = static_cast<double> (octave_time (tm)); |
3254
|
205 else |
|
206 error ("mktime: invalid TMSTRUCT argument"); |
|
207 } |
|
208 else |
|
209 error ("mktime: expecting structure argument"); |
2928
|
210 } |
|
211 else |
|
212 print_usage ("mktime"); |
|
213 |
|
214 return retval; |
|
215 } |
|
216 |
|
217 DEFUN_DLD (strftime, args, , |
3295
|
218 "-*- texinfo -*-\n\ |
|
219 @deftypefn {Loadable Function} {} strftime (@var{tm_struct})\n\ |
|
220 Format a time structure in a flexible way using @samp{%} substitutions\n\ |
|
221 similar to those in @code{printf}. Except where noted, substituted\n\ |
|
222 fields have a fixed size; numeric fields are padded if necessary.\n\ |
|
223 Padding is with zeros by default; for fields that display a single\n\ |
|
224 number, padding can be changed or inhibited by following the @samp{%}\n\ |
|
225 with one of the modifiers described below. Unknown field specifiers are\n\ |
|
226 copied as normal characters. All other characters are copied to the\n\ |
|
227 output without change. For example,\n\ |
2928
|
228 \n\ |
3295
|
229 @example\n\ |
|
230 @group\n\ |
5020
|
231 strftime (\"%r (%Z) %A %e %B %Y\", localtime (time ()))\n\ |
3295
|
232 @result{} \"01:15:06 AM (CST) Monday 17 February 1997\"\n\ |
|
233 @end group\n\ |
|
234 @end example\n\ |
2928
|
235 \n\ |
3295
|
236 Octave's @code{strftime} function supports a superset of the ANSI C\n\ |
|
237 field specifiers.\n\ |
2928
|
238 \n\ |
3295
|
239 @noindent\n\ |
2928
|
240 Literal character fields:\n\ |
|
241 \n\ |
3295
|
242 @table @code\n\ |
|
243 @item %\n\ |
|
244 % character.\n\ |
2928
|
245 \n\ |
3295
|
246 @item n\n\ |
|
247 Newline character.\n\ |
|
248 \n\ |
|
249 @item t\n\ |
|
250 Tab character.\n\ |
|
251 @end table\n\ |
|
252 \n\ |
|
253 @noindent\n\ |
2928
|
254 Numeric modifiers (a nonstandard extension):\n\ |
|
255 \n\ |
3295
|
256 @table @code\n\ |
|
257 @item - (dash)\n\ |
|
258 Do not pad the field.\n\ |
2928
|
259 \n\ |
3295
|
260 @item _ (underscore)\n\ |
|
261 Pad the field with spaces.\n\ |
|
262 @end table\n\ |
|
263 \n\ |
|
264 @noindent\n\ |
2928
|
265 Time fields:\n\ |
|
266 \n\ |
3295
|
267 @table @code\n\ |
|
268 @item %H\n\ |
|
269 Hour (00-23).\n\ |
|
270 \n\ |
|
271 @item %I\n\ |
|
272 Hour (01-12).\n\ |
|
273 \n\ |
|
274 @item %k\n\ |
|
275 Hour (0-23).\n\ |
|
276 \n\ |
|
277 @item %l\n\ |
|
278 Hour (1-12).\n\ |
|
279 \n\ |
|
280 @item %M\n\ |
|
281 Minute (00-59).\n\ |
|
282 \n\ |
|
283 @item %p\n\ |
|
284 Locale's AM or PM.\n\ |
2928
|
285 \n\ |
3295
|
286 @item %r\n\ |
|
287 Time, 12-hour (hh:mm:ss [AP]M).\n\ |
|
288 \n\ |
|
289 @item %R\n\ |
|
290 Time, 24-hour (hh:mm).\n\ |
|
291 \n\ |
|
292 @item %s\n\ |
|
293 Time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension).\n\ |
|
294 \n\ |
|
295 @item %S\n\ |
|
296 Second (00-61).\n\ |
|
297 \n\ |
|
298 @item %T\n\ |
|
299 Time, 24-hour (hh:mm:ss).\n\ |
|
300 \n\ |
|
301 @item %X\n\ |
|
302 Locale's time representation (%H:%M:%S).\n\ |
|
303 \n\ |
|
304 @item %Z\n\ |
|
305 Time zone (EDT), or nothing if no time zone is determinable.\n\ |
|
306 @end table\n\ |
|
307 \n\ |
|
308 @noindent\n\ |
2928
|
309 Date fields:\n\ |
|
310 \n\ |
3295
|
311 @table @code\n\ |
|
312 @item %a\n\ |
|
313 Locale's abbreviated weekday name (Sun-Sat).\n\ |
|
314 \n\ |
|
315 @item %A\n\ |
|
316 Locale's full weekday name, variable length (Sunday-Saturday).\n\ |
|
317 \n\ |
|
318 @item %b\n\ |
|
319 Locale's abbreviated month name (Jan-Dec).\n\ |
|
320 \n\ |
|
321 @item %B\n\ |
|
322 Locale's full month name, variable length (January-December).\n\ |
|
323 \n\ |
|
324 @item %c\n\ |
|
325 Locale's date and time (Sat Nov 04 12:02:33 EST 1989).\n\ |
|
326 \n\ |
|
327 @item %C\n\ |
|
328 Century (00-99).\n\ |
|
329 \n\ |
|
330 @item %d\n\ |
|
331 Day of month (01-31).\n\ |
|
332 \n\ |
|
333 @item %e\n\ |
|
334 Day of month ( 1-31).\n\ |
|
335 \n\ |
|
336 @item %D\n\ |
|
337 Date (mm/dd/yy).\n\ |
|
338 \n\ |
|
339 @item %h\n\ |
|
340 Same as %b.\n\ |
|
341 \n\ |
|
342 @item %j\n\ |
|
343 Day of year (001-366).\n\ |
|
344 \n\ |
|
345 @item %m\n\ |
|
346 Month (01-12).\n\ |
|
347 \n\ |
|
348 @item %U\n\ |
|
349 Week number of year with Sunday as first day of week (00-53).\n\ |
|
350 \n\ |
|
351 @item %w\n\ |
|
352 Day of week (0-6).\n\ |
|
353 \n\ |
|
354 @item %W\n\ |
|
355 Week number of year with Monday as first day of week (00-53).\n\ |
|
356 \n\ |
|
357 @item %x\n\ |
|
358 Locale's date representation (mm/dd/yy).\n\ |
|
359 \n\ |
|
360 @item %y\n\ |
|
361 Last two digits of year (00-99).\n\ |
|
362 \n\ |
|
363 @item %Y\n\ |
|
364 Year (1970-).\n\ |
|
365 @end table\n\ |
|
366 @end deftypefn\n") |
2928
|
367 { |
3465
|
368 octave_value retval; |
2928
|
369 |
3254
|
370 if (args.length () == 2) |
2928
|
371 { |
3523
|
372 std::string fmt = args(0).string_value (); |
2928
|
373 |
|
374 if (! error_state) |
|
375 { |
3254
|
376 Octave_map map = args(1).map_value (); |
2928
|
377 |
3254
|
378 if (! error_state) |
3233
|
379 { |
3254
|
380 octave_base_tm tm = extract_tm (map); |
3233
|
381 |
3254
|
382 if (! error_state) |
3255
|
383 retval = tm.strftime (fmt); |
3254
|
384 else |
|
385 error ("strftime: invalid TMSTRUCT argument"); |
3233
|
386 } |
3254
|
387 else |
|
388 error ("strftime: expecting structure as second argument"); |
2928
|
389 } |
3254
|
390 else |
|
391 error ("strftime: expecting format string as first argument"); |
2928
|
392 } |
|
393 else |
|
394 print_usage ("strftime"); |
|
395 |
|
396 return retval; |
|
397 } |
|
398 |
3465
|
399 DEFUN_DLD (strptime, args, , |
|
400 "-*- texinfo -*-\n\ |
4367
|
401 @deftypefn {Loadable Function} {[@var{tm_struct}, @var{nchars}] =} strptime (@var{str}, @var{fmt})\n\ |
3465
|
402 Convert the string @var{str} to a time structure under the control of\n\ |
|
403 the format @var{fmt}.\n\ |
|
404 @end deftypefn") |
|
405 { |
|
406 octave_value_list retval; |
|
407 |
|
408 if (args.length () == 2) |
|
409 { |
3523
|
410 std::string str = args(0).string_value (); |
3465
|
411 |
|
412 if (! error_state) |
|
413 { |
3523
|
414 std::string fmt = args(1).string_value (); |
3465
|
415 |
|
416 if (! error_state) |
|
417 { |
|
418 octave_strptime t (str, fmt); |
|
419 |
|
420 retval(1) = static_cast<double> (t.characters_converted ()); |
|
421 retval(0) = octave_value (mk_tm_map (t)); |
|
422 } |
|
423 else |
|
424 error ("strptime: expecting format string as second argument"); |
|
425 } |
|
426 else |
|
427 error ("strptime: expecting string as first argument"); |
|
428 } |
|
429 else |
|
430 print_usage ("strptime"); |
|
431 |
|
432 return retval; |
|
433 } |
|
434 |
2928
|
435 /* |
|
436 ;;; Local Variables: *** |
|
437 ;;; mode: C++ *** |
|
438 ;;; End: *** |
|
439 */ |