2928
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2004, 2005, 2006, |
|
4 2007 John W. Eaton |
2928
|
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 |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2928
|
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 |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
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\ |
5688
|
86 @seealso{strftime, strptime, localtime, gmtime, mktime, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3301
|
87 @end deftypefn") |
2928
|
88 { |
3255
|
89 octave_value retval; |
2928
|
90 |
3255
|
91 if (args.length () == 0) |
|
92 retval = static_cast<double> (octave_time ()); |
|
93 else |
5823
|
94 print_usage (); |
3255
|
95 |
|
96 return retval; |
2928
|
97 } |
|
98 |
|
99 DEFUN_DLD (gmtime, args, , |
3301
|
100 "-*- texinfo -*-\n\ |
|
101 @deftypefn {Loadable Function} {} gmtime (@var{t})\n\ |
|
102 Given a value returned from time (or any nonnegative integer),\n\ |
|
103 return a time structure corresponding to CUT. For example,\n\ |
2928
|
104 \n\ |
3301
|
105 @example\n\ |
|
106 @group\n\ |
|
107 gmtime (time ())\n\ |
|
108 @result{} @{\n\ |
|
109 usec = 0\n\ |
|
110 year = 97\n\ |
|
111 mon = 1\n\ |
|
112 mday = 17\n\ |
|
113 sec = 6\n\ |
|
114 zone = CST\n\ |
|
115 min = 15\n\ |
|
116 wday = 1\n\ |
|
117 hour = 7\n\ |
|
118 isdst = 0\n\ |
|
119 yday = 47\n\ |
|
120 @}\n\ |
|
121 @end group\n\ |
|
122 @end example\n\ |
5688
|
123 @seealso{strftime, strptime, localtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3301
|
124 @end deftypefn") |
2928
|
125 { |
3465
|
126 octave_value retval; |
2928
|
127 |
|
128 if (args.length () == 1) |
|
129 { |
|
130 double tmp = args(0).double_value (); |
|
131 |
|
132 if (! error_state) |
3254
|
133 retval = octave_value (mk_tm_map (octave_gmtime (tmp))); |
2928
|
134 } |
|
135 else |
5823
|
136 print_usage (); |
2928
|
137 |
|
138 return retval; |
|
139 } |
|
140 |
|
141 DEFUN_DLD (localtime, args, , |
3301
|
142 "-*- texinfo -*-\n\ |
|
143 @deftypefn {Loadable Function} {} localtime (@var{t})\n\ |
|
144 Given a value returned from time (or any nonnegative integer),\n\ |
|
145 return a time structure corresponding to the local time zone.\n\ |
2928
|
146 \n\ |
3301
|
147 @example\n\ |
|
148 @group\n\ |
|
149 localtime (time ())\n\ |
|
150 @result{} @{\n\ |
|
151 usec = 0\n\ |
|
152 year = 97\n\ |
|
153 mon = 1\n\ |
|
154 mday = 17\n\ |
|
155 sec = 6\n\ |
|
156 zone = CST\n\ |
|
157 min = 15\n\ |
|
158 wday = 1\n\ |
|
159 hour = 1\n\ |
|
160 isdst = 0\n\ |
|
161 yday = 47\n\ |
|
162 @}\n\ |
|
163 @end group\n\ |
|
164 @end example\n\ |
5688
|
165 @seealso{strftime, strptime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3301
|
166 @end deftypefn") |
2928
|
167 { |
3465
|
168 octave_value retval; |
2928
|
169 |
|
170 if (args.length () == 1) |
|
171 { |
|
172 double tmp = args(0).double_value (); |
|
173 |
|
174 if (! error_state) |
3254
|
175 retval = octave_value (mk_tm_map (octave_localtime (tmp))); |
2928
|
176 } |
|
177 else |
5823
|
178 print_usage (); |
2928
|
179 |
|
180 return retval; |
|
181 } |
|
182 |
|
183 DEFUN_DLD (mktime, args, , |
3301
|
184 "-*- texinfo -*-\n\ |
|
185 @deftypefn {Loadable Function} {} mktime (@var{tm_struct})\n\ |
|
186 Convert a time structure corresponding to the local time to the number\n\ |
|
187 of seconds since the epoch. For example,\n\ |
|
188 \n\ |
|
189 @example\n\ |
|
190 @group\n\ |
|
191 mktime (localtime (time ())\n\ |
|
192 @result{} 856163706\n\ |
|
193 @end group\n\ |
|
194 @end example\n\ |
5688
|
195 @seealso{strftime, strptime, localtime, gmtime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3301
|
196 @end deftypefn") |
2928
|
197 { |
3465
|
198 octave_value retval; |
2928
|
199 |
3254
|
200 if (args.length () == 1) |
2928
|
201 { |
|
202 Octave_map map = args(0).map_value (); |
|
203 |
3254
|
204 if (! error_state) |
|
205 { |
|
206 octave_base_tm tm = extract_tm (map); |
|
207 |
|
208 if (! error_state) |
3255
|
209 retval = static_cast<double> (octave_time (tm)); |
3254
|
210 else |
|
211 error ("mktime: invalid TMSTRUCT argument"); |
|
212 } |
|
213 else |
|
214 error ("mktime: expecting structure argument"); |
2928
|
215 } |
|
216 else |
5823
|
217 print_usage (); |
2928
|
218 |
|
219 return retval; |
|
220 } |
|
221 |
|
222 DEFUN_DLD (strftime, args, , |
3295
|
223 "-*- texinfo -*-\n\ |
5675
|
224 @deftypefn {Loadable Function} {} strftime (@var{fmt}, @var{tm_struct})\n\ |
|
225 Format the time structure @var{tm_struct} in a flexible way using the\n\ |
|
226 format string @var{fmt} that contains @samp{%} substitutions\n\ |
3295
|
227 similar to those in @code{printf}. Except where noted, substituted\n\ |
|
228 fields have a fixed size; numeric fields are padded if necessary.\n\ |
|
229 Padding is with zeros by default; for fields that display a single\n\ |
|
230 number, padding can be changed or inhibited by following the @samp{%}\n\ |
|
231 with one of the modifiers described below. Unknown field specifiers are\n\ |
|
232 copied as normal characters. All other characters are copied to the\n\ |
|
233 output without change. For example,\n\ |
2928
|
234 \n\ |
3295
|
235 @example\n\ |
|
236 @group\n\ |
5020
|
237 strftime (\"%r (%Z) %A %e %B %Y\", localtime (time ()))\n\ |
3295
|
238 @result{} \"01:15:06 AM (CST) Monday 17 February 1997\"\n\ |
|
239 @end group\n\ |
|
240 @end example\n\ |
2928
|
241 \n\ |
3295
|
242 Octave's @code{strftime} function supports a superset of the ANSI C\n\ |
|
243 field specifiers.\n\ |
2928
|
244 \n\ |
3295
|
245 @noindent\n\ |
2928
|
246 Literal character fields:\n\ |
|
247 \n\ |
3295
|
248 @table @code\n\ |
|
249 @item %\n\ |
|
250 % character.\n\ |
2928
|
251 \n\ |
3295
|
252 @item n\n\ |
|
253 Newline character.\n\ |
|
254 \n\ |
|
255 @item t\n\ |
|
256 Tab character.\n\ |
|
257 @end table\n\ |
|
258 \n\ |
|
259 @noindent\n\ |
2928
|
260 Numeric modifiers (a nonstandard extension):\n\ |
|
261 \n\ |
3295
|
262 @table @code\n\ |
|
263 @item - (dash)\n\ |
|
264 Do not pad the field.\n\ |
2928
|
265 \n\ |
3295
|
266 @item _ (underscore)\n\ |
|
267 Pad the field with spaces.\n\ |
|
268 @end table\n\ |
|
269 \n\ |
|
270 @noindent\n\ |
2928
|
271 Time fields:\n\ |
|
272 \n\ |
3295
|
273 @table @code\n\ |
|
274 @item %H\n\ |
|
275 Hour (00-23).\n\ |
|
276 \n\ |
|
277 @item %I\n\ |
|
278 Hour (01-12).\n\ |
|
279 \n\ |
|
280 @item %k\n\ |
|
281 Hour (0-23).\n\ |
|
282 \n\ |
|
283 @item %l\n\ |
|
284 Hour (1-12).\n\ |
|
285 \n\ |
|
286 @item %M\n\ |
|
287 Minute (00-59).\n\ |
|
288 \n\ |
|
289 @item %p\n\ |
|
290 Locale's AM or PM.\n\ |
2928
|
291 \n\ |
3295
|
292 @item %r\n\ |
|
293 Time, 12-hour (hh:mm:ss [AP]M).\n\ |
|
294 \n\ |
|
295 @item %R\n\ |
|
296 Time, 24-hour (hh:mm).\n\ |
|
297 \n\ |
|
298 @item %s\n\ |
|
299 Time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension).\n\ |
|
300 \n\ |
|
301 @item %S\n\ |
|
302 Second (00-61).\n\ |
|
303 \n\ |
|
304 @item %T\n\ |
|
305 Time, 24-hour (hh:mm:ss).\n\ |
|
306 \n\ |
|
307 @item %X\n\ |
|
308 Locale's time representation (%H:%M:%S).\n\ |
|
309 \n\ |
|
310 @item %Z\n\ |
|
311 Time zone (EDT), or nothing if no time zone is determinable.\n\ |
|
312 @end table\n\ |
|
313 \n\ |
|
314 @noindent\n\ |
2928
|
315 Date fields:\n\ |
|
316 \n\ |
3295
|
317 @table @code\n\ |
|
318 @item %a\n\ |
|
319 Locale's abbreviated weekday name (Sun-Sat).\n\ |
|
320 \n\ |
|
321 @item %A\n\ |
|
322 Locale's full weekday name, variable length (Sunday-Saturday).\n\ |
|
323 \n\ |
|
324 @item %b\n\ |
|
325 Locale's abbreviated month name (Jan-Dec).\n\ |
|
326 \n\ |
|
327 @item %B\n\ |
|
328 Locale's full month name, variable length (January-December).\n\ |
|
329 \n\ |
|
330 @item %c\n\ |
|
331 Locale's date and time (Sat Nov 04 12:02:33 EST 1989).\n\ |
|
332 \n\ |
|
333 @item %C\n\ |
|
334 Century (00-99).\n\ |
|
335 \n\ |
|
336 @item %d\n\ |
|
337 Day of month (01-31).\n\ |
|
338 \n\ |
|
339 @item %e\n\ |
|
340 Day of month ( 1-31).\n\ |
|
341 \n\ |
|
342 @item %D\n\ |
|
343 Date (mm/dd/yy).\n\ |
|
344 \n\ |
|
345 @item %h\n\ |
|
346 Same as %b.\n\ |
|
347 \n\ |
|
348 @item %j\n\ |
|
349 Day of year (001-366).\n\ |
|
350 \n\ |
|
351 @item %m\n\ |
|
352 Month (01-12).\n\ |
|
353 \n\ |
|
354 @item %U\n\ |
|
355 Week number of year with Sunday as first day of week (00-53).\n\ |
|
356 \n\ |
|
357 @item %w\n\ |
|
358 Day of week (0-6).\n\ |
|
359 \n\ |
|
360 @item %W\n\ |
|
361 Week number of year with Monday as first day of week (00-53).\n\ |
|
362 \n\ |
|
363 @item %x\n\ |
|
364 Locale's date representation (mm/dd/yy).\n\ |
|
365 \n\ |
|
366 @item %y\n\ |
|
367 Last two digits of year (00-99).\n\ |
|
368 \n\ |
|
369 @item %Y\n\ |
|
370 Year (1970-).\n\ |
|
371 @end table\n\ |
5688
|
372 @seealso{strptime, localtime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
5646
|
373 @end deftypefn") |
2928
|
374 { |
3465
|
375 octave_value retval; |
2928
|
376 |
3254
|
377 if (args.length () == 2) |
2928
|
378 { |
3523
|
379 std::string fmt = args(0).string_value (); |
2928
|
380 |
|
381 if (! error_state) |
|
382 { |
3254
|
383 Octave_map map = args(1).map_value (); |
2928
|
384 |
3254
|
385 if (! error_state) |
3233
|
386 { |
3254
|
387 octave_base_tm tm = extract_tm (map); |
3233
|
388 |
3254
|
389 if (! error_state) |
3255
|
390 retval = tm.strftime (fmt); |
3254
|
391 else |
|
392 error ("strftime: invalid TMSTRUCT argument"); |
3233
|
393 } |
3254
|
394 else |
|
395 error ("strftime: expecting structure as second argument"); |
2928
|
396 } |
3254
|
397 else |
|
398 error ("strftime: expecting format string as first argument"); |
2928
|
399 } |
|
400 else |
5823
|
401 print_usage (); |
2928
|
402 |
|
403 return retval; |
|
404 } |
|
405 |
3465
|
406 DEFUN_DLD (strptime, args, , |
|
407 "-*- texinfo -*-\n\ |
4367
|
408 @deftypefn {Loadable Function} {[@var{tm_struct}, @var{nchars}] =} strptime (@var{str}, @var{fmt})\n\ |
5675
|
409 Convert the string @var{str} to the time structure @var{tm_struct} under\n\ |
|
410 the control of the format string @var{fmt}.\n\ |
|
411 \n\ |
|
412 If @var{fmt} fails to match, @var{nchars} is 0; otherwise it is set to the\n\ |
|
413 position of last matched character plus 1. Always check for this unless\n\ |
|
414 you're absolutely sure the date string will be parsed correctly.\n\ |
5688
|
415 @seealso{strftime, localtime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3465
|
416 @end deftypefn") |
|
417 { |
|
418 octave_value_list retval; |
|
419 |
|
420 if (args.length () == 2) |
|
421 { |
3523
|
422 std::string str = args(0).string_value (); |
3465
|
423 |
|
424 if (! error_state) |
|
425 { |
3523
|
426 std::string fmt = args(1).string_value (); |
3465
|
427 |
|
428 if (! error_state) |
|
429 { |
|
430 octave_strptime t (str, fmt); |
|
431 |
|
432 retval(1) = static_cast<double> (t.characters_converted ()); |
|
433 retval(0) = octave_value (mk_tm_map (t)); |
|
434 } |
|
435 else |
|
436 error ("strptime: expecting format string as second argument"); |
|
437 } |
|
438 else |
|
439 error ("strptime: expecting string as first argument"); |
|
440 } |
|
441 else |
5823
|
442 print_usage (); |
3465
|
443 |
|
444 return retval; |
|
445 } |
|
446 |
2928
|
447 /* |
|
448 ;;; Local Variables: *** |
|
449 ;;; mode: C++ *** |
|
450 ;;; End: *** |
|
451 */ |