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