Mercurial > octave
annotate src/DLD-FUNCTIONS/time.cc @ 10840:89f4d7e294cc
Grammarcheck .cc files
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 31 Jul 2010 11:18:11 -0700 |
parents | f63ee6a20f57 |
children | 0de4eff677d6 |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2004, 2005, 2006, |
8920 | 4 2007, 2008, 2009 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) |
7065 | 92 retval = octave_time (); |
3255 | 93 else |
5823 | 94 print_usage (); |
3255 | 95 |
96 return retval; | |
2928 | 97 } |
98 | |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
99 /* |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
100 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
101 %!assert(time () > 0); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
102 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
103 */ |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
104 |
2928 | 105 DEFUN_DLD (gmtime, args, , |
3301 | 106 "-*- texinfo -*-\n\ |
107 @deftypefn {Loadable Function} {} gmtime (@var{t})\n\ | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
108 Given a value returned from time (or any non-negative integer),\n\ |
10840 | 109 return a time structure corresponding to CUT@. For example:\n\ |
2928 | 110 \n\ |
3301 | 111 @example\n\ |
112 @group\n\ | |
113 gmtime (time ())\n\ | |
114 @result{} @{\n\ | |
115 usec = 0\n\ | |
116 year = 97\n\ | |
117 mon = 1\n\ | |
118 mday = 17\n\ | |
119 sec = 6\n\ | |
120 zone = CST\n\ | |
121 min = 15\n\ | |
122 wday = 1\n\ | |
123 hour = 7\n\ | |
124 isdst = 0\n\ | |
125 yday = 47\n\ | |
126 @}\n\ | |
127 @end group\n\ | |
128 @end example\n\ | |
5688 | 129 @seealso{strftime, strptime, localtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3301 | 130 @end deftypefn") |
2928 | 131 { |
3465 | 132 octave_value retval; |
2928 | 133 |
134 if (args.length () == 1) | |
135 { | |
136 double tmp = args(0).double_value (); | |
137 | |
138 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
139 retval = octave_value (mk_tm_map (octave_gmtime (tmp))); |
2928 | 140 } |
141 else | |
5823 | 142 print_usage (); |
2928 | 143 |
144 return retval; | |
145 } | |
146 | |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
147 /* |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
148 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
149 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
150 %! ts = gmtime (time ()); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
151 %! assert((isstruct (ts) |
10231
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
152 %! && isfield (ts, "usec") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
153 %! && isfield (ts, "year") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
154 %! && isfield (ts, "mon") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
155 %! && isfield (ts, "mday") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
156 %! && isfield (ts, "sec") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
157 %! && isfield (ts, "min") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
158 %! && isfield (ts, "wday") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
159 %! && isfield (ts, "hour") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
160 %! && isfield (ts, "isdst") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
161 %! && isfield (ts, "yday"))); |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
162 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
163 %!error <Invalid call to gmtime.*> gmtime (); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
164 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
165 %!error <Invalid call to gmtime.*> gmtime (1, 2); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
166 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
167 */ |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
168 |
2928 | 169 DEFUN_DLD (localtime, args, , |
3301 | 170 "-*- texinfo -*-\n\ |
171 @deftypefn {Loadable Function} {} localtime (@var{t})\n\ | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
172 Given a value returned from time (or any non-negative integer),\n\ |
3301 | 173 return a time structure corresponding to the local time zone.\n\ |
2928 | 174 \n\ |
3301 | 175 @example\n\ |
176 @group\n\ | |
177 localtime (time ())\n\ | |
178 @result{} @{\n\ | |
179 usec = 0\n\ | |
180 year = 97\n\ | |
181 mon = 1\n\ | |
182 mday = 17\n\ | |
183 sec = 6\n\ | |
184 zone = CST\n\ | |
185 min = 15\n\ | |
186 wday = 1\n\ | |
187 hour = 1\n\ | |
188 isdst = 0\n\ | |
189 yday = 47\n\ | |
190 @}\n\ | |
191 @end group\n\ | |
192 @end example\n\ | |
5688 | 193 @seealso{strftime, strptime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3301 | 194 @end deftypefn") |
2928 | 195 { |
3465 | 196 octave_value retval; |
2928 | 197 |
198 if (args.length () == 1) | |
199 { | |
200 double tmp = args(0).double_value (); | |
201 | |
202 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
203 retval = octave_value (mk_tm_map (octave_localtime (tmp))); |
2928 | 204 } |
205 else | |
5823 | 206 print_usage (); |
2928 | 207 |
208 return retval; | |
209 } | |
210 | |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
211 /* |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
212 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
213 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
214 %! ts = localtime (time ()); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
215 %! assert((isstruct (ts) |
10231
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
216 %! && isfield (ts, "usec") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
217 %! && isfield (ts, "year") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
218 %! && isfield (ts, "mon") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
219 %! && isfield (ts, "mday") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
220 %! && isfield (ts, "sec") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
221 %! && isfield (ts, "min") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
222 %! && isfield (ts, "wday") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
223 %! && isfield (ts, "hour") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
224 %! && isfield (ts, "isdst") |
f63ee6a20f57
Replace removed functions in tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10155
diff
changeset
|
225 %! && isfield (ts, "yday"))); |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
226 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
227 %!error <Invalid call to localtime.*> localtime (); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
228 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
229 %!error <Invalid call to localtime.*> localtime (1, 2); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
230 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
231 */ |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
232 |
2928 | 233 DEFUN_DLD (mktime, args, , |
3301 | 234 "-*- texinfo -*-\n\ |
235 @deftypefn {Loadable Function} {} mktime (@var{tm_struct})\n\ | |
236 Convert a time structure corresponding to the local time to the number\n\ | |
10840 | 237 of seconds since the epoch. For example:\n\ |
3301 | 238 \n\ |
239 @example\n\ | |
240 @group\n\ | |
7097 | 241 mktime (localtime (time ()))\n\ |
3301 | 242 @result{} 856163706\n\ |
243 @end group\n\ | |
244 @end example\n\ | |
5688 | 245 @seealso{strftime, strptime, localtime, gmtime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3301 | 246 @end deftypefn") |
2928 | 247 { |
3465 | 248 octave_value retval; |
2928 | 249 |
3254 | 250 if (args.length () == 1) |
2928 | 251 { |
252 Octave_map map = args(0).map_value (); | |
253 | |
3254 | 254 if (! error_state) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
255 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
256 octave_base_tm tm = extract_tm (map); |
3254 | 257 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
258 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
259 retval = octave_time (tm); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
260 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
261 error ("mktime: invalid TMSTRUCT argument"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
262 } |
3254 | 263 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
264 error ("mktime: expecting structure argument"); |
2928 | 265 } |
266 else | |
5823 | 267 print_usage (); |
2928 | 268 |
269 return retval; | |
270 } | |
271 | |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
272 /* |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
273 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
274 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
275 %! t = time (); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
276 %! assert(fix (mktime (localtime (t))) == fix (t)); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
277 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
278 %!error <Invalid call to mktime.*> mktime (); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
279 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
280 %!error <Invalid call to mktime.*> mktime (1, 2, 3); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
281 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
282 */ |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
283 |
2928 | 284 DEFUN_DLD (strftime, args, , |
3295 | 285 "-*- texinfo -*-\n\ |
5675 | 286 @deftypefn {Loadable Function} {} strftime (@var{fmt}, @var{tm_struct})\n\ |
287 Format the time structure @var{tm_struct} in a flexible way using the\n\ | |
288 format string @var{fmt} that contains @samp{%} substitutions\n\ | |
3295 | 289 similar to those in @code{printf}. Except where noted, substituted\n\ |
290 fields have a fixed size; numeric fields are padded if necessary.\n\ | |
291 Padding is with zeros by default; for fields that display a single\n\ | |
292 number, padding can be changed or inhibited by following the @samp{%}\n\ | |
293 with one of the modifiers described below. Unknown field specifiers are\n\ | |
294 copied as normal characters. All other characters are copied to the\n\ | |
10840 | 295 output without change. For example:\n\ |
2928 | 296 \n\ |
3295 | 297 @example\n\ |
298 @group\n\ | |
5020 | 299 strftime (\"%r (%Z) %A %e %B %Y\", localtime (time ()))\n\ |
3295 | 300 @result{} \"01:15:06 AM (CST) Monday 17 February 1997\"\n\ |
301 @end group\n\ | |
302 @end example\n\ | |
2928 | 303 \n\ |
3295 | 304 Octave's @code{strftime} function supports a superset of the ANSI C\n\ |
305 field specifiers.\n\ | |
2928 | 306 \n\ |
3295 | 307 @noindent\n\ |
2928 | 308 Literal character fields:\n\ |
309 \n\ | |
3295 | 310 @table @code\n\ |
311 @item %\n\ | |
312 % character.\n\ | |
2928 | 313 \n\ |
3295 | 314 @item n\n\ |
315 Newline character.\n\ | |
316 \n\ | |
317 @item t\n\ | |
318 Tab character.\n\ | |
319 @end table\n\ | |
320 \n\ | |
321 @noindent\n\ | |
2928 | 322 Numeric modifiers (a nonstandard extension):\n\ |
323 \n\ | |
3295 | 324 @table @code\n\ |
325 @item - (dash)\n\ | |
326 Do not pad the field.\n\ | |
2928 | 327 \n\ |
3295 | 328 @item _ (underscore)\n\ |
329 Pad the field with spaces.\n\ | |
330 @end table\n\ | |
331 \n\ | |
332 @noindent\n\ | |
2928 | 333 Time fields:\n\ |
334 \n\ | |
3295 | 335 @table @code\n\ |
336 @item %H\n\ | |
337 Hour (00-23).\n\ | |
338 \n\ | |
339 @item %I\n\ | |
340 Hour (01-12).\n\ | |
341 \n\ | |
342 @item %k\n\ | |
343 Hour (0-23).\n\ | |
344 \n\ | |
345 @item %l\n\ | |
346 Hour (1-12).\n\ | |
347 \n\ | |
348 @item %M\n\ | |
349 Minute (00-59).\n\ | |
350 \n\ | |
351 @item %p\n\ | |
352 Locale's AM or PM.\n\ | |
2928 | 353 \n\ |
3295 | 354 @item %r\n\ |
355 Time, 12-hour (hh:mm:ss [AP]M).\n\ | |
356 \n\ | |
357 @item %R\n\ | |
358 Time, 24-hour (hh:mm).\n\ | |
359 \n\ | |
360 @item %s\n\ | |
361 Time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension).\n\ | |
362 \n\ | |
363 @item %S\n\ | |
364 Second (00-61).\n\ | |
365 \n\ | |
366 @item %T\n\ | |
367 Time, 24-hour (hh:mm:ss).\n\ | |
368 \n\ | |
369 @item %X\n\ | |
370 Locale's time representation (%H:%M:%S).\n\ | |
371 \n\ | |
372 @item %Z\n\ | |
373 Time zone (EDT), or nothing if no time zone is determinable.\n\ | |
374 @end table\n\ | |
375 \n\ | |
376 @noindent\n\ | |
2928 | 377 Date fields:\n\ |
378 \n\ | |
3295 | 379 @table @code\n\ |
380 @item %a\n\ | |
381 Locale's abbreviated weekday name (Sun-Sat).\n\ | |
382 \n\ | |
383 @item %A\n\ | |
384 Locale's full weekday name, variable length (Sunday-Saturday).\n\ | |
385 \n\ | |
386 @item %b\n\ | |
387 Locale's abbreviated month name (Jan-Dec).\n\ | |
388 \n\ | |
389 @item %B\n\ | |
390 Locale's full month name, variable length (January-December).\n\ | |
391 \n\ | |
392 @item %c\n\ | |
393 Locale's date and time (Sat Nov 04 12:02:33 EST 1989).\n\ | |
394 \n\ | |
395 @item %C\n\ | |
396 Century (00-99).\n\ | |
397 \n\ | |
398 @item %d\n\ | |
399 Day of month (01-31).\n\ | |
400 \n\ | |
401 @item %e\n\ | |
402 Day of month ( 1-31).\n\ | |
403 \n\ | |
404 @item %D\n\ | |
405 Date (mm/dd/yy).\n\ | |
406 \n\ | |
407 @item %h\n\ | |
408 Same as %b.\n\ | |
409 \n\ | |
410 @item %j\n\ | |
411 Day of year (001-366).\n\ | |
412 \n\ | |
413 @item %m\n\ | |
414 Month (01-12).\n\ | |
415 \n\ | |
416 @item %U\n\ | |
417 Week number of year with Sunday as first day of week (00-53).\n\ | |
418 \n\ | |
419 @item %w\n\ | |
420 Day of week (0-6).\n\ | |
421 \n\ | |
422 @item %W\n\ | |
423 Week number of year with Monday as first day of week (00-53).\n\ | |
424 \n\ | |
425 @item %x\n\ | |
426 Locale's date representation (mm/dd/yy).\n\ | |
427 \n\ | |
428 @item %y\n\ | |
429 Last two digits of year (00-99).\n\ | |
430 \n\ | |
431 @item %Y\n\ | |
432 Year (1970-).\n\ | |
433 @end table\n\ | |
5688 | 434 @seealso{strptime, localtime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
5646 | 435 @end deftypefn") |
2928 | 436 { |
3465 | 437 octave_value retval; |
2928 | 438 |
3254 | 439 if (args.length () == 2) |
2928 | 440 { |
3523 | 441 std::string fmt = args(0).string_value (); |
2928 | 442 |
443 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
444 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
445 Octave_map map = args(1).map_value (); |
2928 | 446 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
447 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
448 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
449 octave_base_tm tm = extract_tm (map); |
3233 | 450 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
451 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
452 retval = tm.strftime (fmt); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
453 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
454 error ("strftime: invalid TMSTRUCT argument"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
455 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
456 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
457 error ("strftime: expecting structure as second argument"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
458 } |
3254 | 459 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
460 error ("strftime: expecting format string as first argument"); |
2928 | 461 } |
462 else | |
5823 | 463 print_usage (); |
2928 | 464 |
465 return retval; | |
466 } | |
467 | |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
468 /* |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
469 |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7562
diff
changeset
|
470 %!assert((ischar (strftime ("%%%n%t%H%I%k%l", localtime (time ()))) |
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7562
diff
changeset
|
471 %! && ischar (strftime ("%M%p%r%R%s%S%T", localtime (time ()))) |
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7562
diff
changeset
|
472 %! && ischar (strftime ("%X%Z%z%a%A%b%B", localtime (time ()))) |
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7562
diff
changeset
|
473 %! && ischar (strftime ("%c%C%d%e%D%h%j", localtime (time ()))) |
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7562
diff
changeset
|
474 %! && ischar (strftime ("%m%U%w%W%x%y%Y", localtime (time ()))))); |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
475 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
476 %!error <Invalid call to strftime.*> strftime (); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
477 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
478 %!error <Invalid call to strftime.*> strftime ("foo", localtime (time ()), 1); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
479 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
480 */ |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
481 |
3465 | 482 DEFUN_DLD (strptime, args, , |
483 "-*- texinfo -*-\n\ | |
4367 | 484 @deftypefn {Loadable Function} {[@var{tm_struct}, @var{nchars}] =} strptime (@var{str}, @var{fmt})\n\ |
5675 | 485 Convert the string @var{str} to the time structure @var{tm_struct} under\n\ |
486 the control of the format string @var{fmt}.\n\ | |
487 \n\ | |
488 If @var{fmt} fails to match, @var{nchars} is 0; otherwise it is set to the\n\ | |
489 position of last matched character plus 1. Always check for this unless\n\ | |
490 you're absolutely sure the date string will be parsed correctly.\n\ | |
5688 | 491 @seealso{strftime, localtime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3465 | 492 @end deftypefn") |
493 { | |
494 octave_value_list retval; | |
495 | |
496 if (args.length () == 2) | |
497 { | |
3523 | 498 std::string str = args(0).string_value (); |
3465 | 499 |
500 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
501 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
502 std::string fmt = args(1).string_value (); |
3465 | 503 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
504 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
505 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
506 octave_strptime t (str, fmt); |
3465 | 507 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
508 retval(1) = t.characters_converted (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
509 retval(0) = octave_value (mk_tm_map (t)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
510 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
511 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
512 error ("strptime: expecting format string as second argument"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
513 } |
3465 | 514 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
515 error ("strptime: expecting string as first argument"); |
3465 | 516 } |
517 else | |
5823 | 518 print_usage (); |
3465 | 519 |
520 return retval; | |
521 } |