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 |
3254
|
43 m ["usec"] = static_cast<double> (t.usec ()); |
|
44 m ["sec"] = static_cast<double> (t.sec ()); |
|
45 m ["min"] = static_cast<double> (t.min ()); |
|
46 m ["hour"] = static_cast<double> (t.hour ()); |
|
47 m ["mday"] = static_cast<double> (t.mday ()); |
|
48 m ["mon"] = static_cast<double> (t.mon ()); |
|
49 m ["year"] = static_cast<double> (t.year ()); |
|
50 m ["wday"] = static_cast<double> (t.wday ()); |
|
51 m ["yday"] = static_cast<double> (t.yday ()); |
|
52 m ["isdst"] = static_cast<double> (t.isdst ()); |
|
53 m ["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 |
3254
|
63 tm.usec (static_cast<int> (m ["usec"] . double_value ())); |
|
64 tm.sec (static_cast<int> (m ["sec"] . double_value ())); |
|
65 tm.min (static_cast<int> (m ["min"] . double_value ())); |
|
66 tm.hour (static_cast<int> (m ["hour"] . double_value ())); |
|
67 tm.mday (static_cast<int> (m ["mday"] . double_value ())); |
|
68 tm.mon (static_cast<int> (m ["mon"] . double_value ())); |
|
69 tm.year (static_cast<int> (m ["year"] . double_value ())); |
|
70 tm.wday (static_cast<int> (m ["wday"] . double_value ())); |
|
71 tm.yday (static_cast<int> (m ["yday"] . double_value ())); |
|
72 tm.isdst (static_cast<int> (m ["isdst"] . double_value ())); |
|
73 tm.zone (m ["zone"] . string_value ()); |
2928
|
74 |
3254
|
75 return tm; |
2928
|
76 } |
|
77 |
3255
|
78 DEFUN_DLD (time, args, , |
2928
|
79 "time ()\n\ |
|
80 \n\ |
|
81 Return current time. On Unix systems, this is the number of\n\ |
|
82 seconds since the epoch.") |
|
83 { |
3255
|
84 octave_value retval; |
2928
|
85 |
3255
|
86 if (args.length () == 0) |
|
87 retval = static_cast<double> (octave_time ()); |
|
88 else |
|
89 print_usage ("time"); |
|
90 |
|
91 return retval; |
2928
|
92 } |
|
93 |
|
94 DEFUN_DLD (gmtime, args, , |
|
95 "gmtime (TIME)\n\ |
|
96 \n\ |
|
97 Given a value returned from time(), return a structure like that\n\ |
|
98 returned from localtime() but with values corresponding to\n\ |
|
99 Coordinated Universal Time (UTC).") |
|
100 { |
|
101 octave_value_list retval; |
|
102 |
|
103 if (args.length () == 1) |
|
104 { |
|
105 double tmp = args(0).double_value (); |
|
106 |
|
107 if (! error_state) |
3254
|
108 retval = octave_value (mk_tm_map (octave_gmtime (tmp))); |
2928
|
109 } |
|
110 else |
|
111 print_usage ("gmtime"); |
|
112 |
|
113 return retval; |
|
114 } |
|
115 |
|
116 DEFUN_DLD (localtime, args, , |
|
117 "localtime (TIME)\n\ |
|
118 \n\ |
|
119 Given a value returned from time(), return a structure with\n\ |
|
120 the following elements:\n\ |
|
121 \n\ |
|
122 usec : microseconds after the second (0, 999999)\n\ |
|
123 sec : seconds after the minute (0, 61)\n\ |
|
124 min : minutes after the hour (0, 59)\n\ |
|
125 hour : hours since midnight (0, 23)\n\ |
|
126 mday : day of the month (1, 31)\n\ |
|
127 mon : months since January (0, 11)\n\ |
|
128 year : years since 1900\n\ |
|
129 wday : days since Sunday (0, 6)\n\ |
|
130 yday : days since January 1 (0, 365)\n\ |
3254
|
131 isdst : daylight savings time flag\n\ |
|
132 zone : time zone") |
2928
|
133 { |
|
134 octave_value_list retval; |
|
135 |
|
136 if (args.length () == 1) |
|
137 { |
|
138 double tmp = args(0).double_value (); |
|
139 |
|
140 if (! error_state) |
3254
|
141 retval = octave_value (mk_tm_map (octave_localtime (tmp))); |
2928
|
142 } |
|
143 else |
|
144 print_usage ("localtime"); |
|
145 |
|
146 return retval; |
|
147 } |
|
148 |
|
149 DEFUN_DLD (mktime, args, , |
|
150 "mktime (TMSTRUCT)") |
|
151 { |
|
152 octave_value_list retval; |
|
153 |
3254
|
154 if (args.length () == 1) |
2928
|
155 { |
|
156 Octave_map map = args(0).map_value (); |
|
157 |
3254
|
158 if (! error_state) |
|
159 { |
|
160 octave_base_tm tm = extract_tm (map); |
|
161 |
|
162 if (! error_state) |
3255
|
163 retval = static_cast<double> (octave_time (tm)); |
3254
|
164 else |
|
165 error ("mktime: invalid TMSTRUCT argument"); |
|
166 } |
|
167 else |
|
168 error ("mktime: expecting structure argument"); |
2928
|
169 } |
|
170 else |
|
171 print_usage ("mktime"); |
|
172 |
|
173 return retval; |
|
174 } |
|
175 |
|
176 DEFUN_DLD (strftime, args, , |
3295
|
177 "-*- texinfo -*-\n\ |
|
178 @deftypefn {Loadable Function} {} strftime (@var{tm_struct})\n\ |
|
179 Format a time structure in a flexible way using @samp{%} substitutions\n\ |
|
180 similar to those in @code{printf}. Except where noted, substituted\n\ |
|
181 fields have a fixed size; numeric fields are padded if necessary.\n\ |
|
182 Padding is with zeros by default; for fields that display a single\n\ |
|
183 number, padding can be changed or inhibited by following the @samp{%}\n\ |
|
184 with one of the modifiers described below. Unknown field specifiers are\n\ |
|
185 copied as normal characters. All other characters are copied to the\n\ |
|
186 output without change. For example,\n\ |
2928
|
187 \n\ |
3295
|
188 @example\n\ |
|
189 @group\n\ |
|
190 strftime (\"%r (%Z) %A %e %B %Y\", localtime (time ())\n\ |
|
191 @result{} \"01:15:06 AM (CST) Monday 17 February 1997\"\n\ |
|
192 @end group\n\ |
|
193 @end example\n\ |
2928
|
194 \n\ |
3295
|
195 Octave's @code{strftime} function supports a superset of the ANSI C\n\ |
|
196 field specifiers.\n\ |
2928
|
197 \n\ |
3295
|
198 @noindent\n\ |
2928
|
199 Literal character fields:\n\ |
|
200 \n\ |
3295
|
201 @table @code\n\ |
|
202 @item %\n\ |
|
203 % character.\n\ |
2928
|
204 \n\ |
3295
|
205 @item n\n\ |
|
206 Newline character.\n\ |
|
207 \n\ |
|
208 @item t\n\ |
|
209 Tab character.\n\ |
|
210 @end table\n\ |
|
211 \n\ |
|
212 @noindent\n\ |
2928
|
213 Numeric modifiers (a nonstandard extension):\n\ |
|
214 \n\ |
3295
|
215 @table @code\n\ |
|
216 @item - (dash)\n\ |
|
217 Do not pad the field.\n\ |
2928
|
218 \n\ |
3295
|
219 @item _ (underscore)\n\ |
|
220 Pad the field with spaces.\n\ |
|
221 @end table\n\ |
|
222 \n\ |
|
223 @noindent\n\ |
2928
|
224 Time fields:\n\ |
|
225 \n\ |
3295
|
226 @table @code\n\ |
|
227 @item %H\n\ |
|
228 Hour (00-23).\n\ |
|
229 \n\ |
|
230 @item %I\n\ |
|
231 Hour (01-12).\n\ |
|
232 \n\ |
|
233 @item %k\n\ |
|
234 Hour (0-23).\n\ |
|
235 \n\ |
|
236 @item %l\n\ |
|
237 Hour (1-12).\n\ |
|
238 \n\ |
|
239 @item %M\n\ |
|
240 Minute (00-59).\n\ |
|
241 \n\ |
|
242 @item %p\n\ |
|
243 Locale's AM or PM.\n\ |
2928
|
244 \n\ |
3295
|
245 @item %r\n\ |
|
246 Time, 12-hour (hh:mm:ss [AP]M).\n\ |
|
247 \n\ |
|
248 @item %R\n\ |
|
249 Time, 24-hour (hh:mm).\n\ |
|
250 \n\ |
|
251 @item %s\n\ |
|
252 Time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension).\n\ |
|
253 \n\ |
|
254 @item %S\n\ |
|
255 Second (00-61).\n\ |
|
256 \n\ |
|
257 @item %T\n\ |
|
258 Time, 24-hour (hh:mm:ss).\n\ |
|
259 \n\ |
|
260 @item %X\n\ |
|
261 Locale's time representation (%H:%M:%S).\n\ |
|
262 \n\ |
|
263 @item %Z\n\ |
|
264 Time zone (EDT), or nothing if no time zone is determinable.\n\ |
|
265 @end table\n\ |
|
266 \n\ |
|
267 @noindent\n\ |
2928
|
268 Date fields:\n\ |
|
269 \n\ |
3295
|
270 @table @code\n\ |
|
271 @item %a\n\ |
|
272 Locale's abbreviated weekday name (Sun-Sat).\n\ |
|
273 \n\ |
|
274 @item %A\n\ |
|
275 Locale's full weekday name, variable length (Sunday-Saturday).\n\ |
|
276 \n\ |
|
277 @item %b\n\ |
|
278 Locale's abbreviated month name (Jan-Dec).\n\ |
|
279 \n\ |
|
280 @item %B\n\ |
|
281 Locale's full month name, variable length (January-December).\n\ |
|
282 \n\ |
|
283 @item %c\n\ |
|
284 Locale's date and time (Sat Nov 04 12:02:33 EST 1989).\n\ |
|
285 \n\ |
|
286 @item %C\n\ |
|
287 Century (00-99).\n\ |
|
288 \n\ |
|
289 @item %d\n\ |
|
290 Day of month (01-31).\n\ |
|
291 \n\ |
|
292 @item %e\n\ |
|
293 Day of month ( 1-31).\n\ |
|
294 \n\ |
|
295 @item %D\n\ |
|
296 Date (mm/dd/yy).\n\ |
|
297 \n\ |
|
298 @item %h\n\ |
|
299 Same as %b.\n\ |
|
300 \n\ |
|
301 @item %j\n\ |
|
302 Day of year (001-366).\n\ |
|
303 \n\ |
|
304 @item %m\n\ |
|
305 Month (01-12).\n\ |
|
306 \n\ |
|
307 @item %U\n\ |
|
308 Week number of year with Sunday as first day of week (00-53).\n\ |
|
309 \n\ |
|
310 @item %w\n\ |
|
311 Day of week (0-6).\n\ |
|
312 \n\ |
|
313 @item %W\n\ |
|
314 Week number of year with Monday as first day of week (00-53).\n\ |
|
315 \n\ |
|
316 @item %x\n\ |
|
317 Locale's date representation (mm/dd/yy).\n\ |
|
318 \n\ |
|
319 @item %y\n\ |
|
320 Last two digits of year (00-99).\n\ |
|
321 \n\ |
|
322 @item %Y\n\ |
|
323 Year (1970-).\n\ |
|
324 @end table\n\ |
|
325 @end deftypefn\n") |
2928
|
326 { |
|
327 octave_value_list retval; |
|
328 |
3254
|
329 if (args.length () == 2) |
2928
|
330 { |
|
331 string fmt = args(0).string_value (); |
|
332 |
|
333 if (! error_state) |
|
334 { |
3254
|
335 Octave_map map = args(1).map_value (); |
2928
|
336 |
3254
|
337 if (! error_state) |
3233
|
338 { |
3254
|
339 octave_base_tm tm = extract_tm (map); |
3233
|
340 |
3254
|
341 if (! error_state) |
3255
|
342 retval = tm.strftime (fmt); |
3254
|
343 else |
|
344 error ("strftime: invalid TMSTRUCT argument"); |
3233
|
345 } |
3254
|
346 else |
|
347 error ("strftime: expecting structure as second argument"); |
2928
|
348 } |
3254
|
349 else |
|
350 error ("strftime: expecting format string as first argument"); |
2928
|
351 } |
|
352 else |
|
353 print_usage ("strftime"); |
|
354 |
|
355 return retval; |
|
356 } |
|
357 |
|
358 /* |
|
359 ;;; Local Variables: *** |
|
360 ;;; mode: C++ *** |
|
361 ;;; End: *** |
|
362 */ |