Mercurial > octave-nkf
annotate libinterp/corefcn/time.cc @ 19731:4197fc428c7d
maint: Update copyright notices for 2015.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 11 Feb 2015 14:19:08 -0500 |
parents | 03067dab10ca |
children | 4f45eaf83908 |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19437
diff
changeset
|
3 Copyright (C) 1996-2015 John W. Eaton |
2928 | 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
2928 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
2928 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
27 #include <string> | |
28 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
29 #include "defun.h" |
2928 | 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 | |
11042
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
38 static octave_scalar_map |
3254 | 39 mk_tm_map (const octave_base_tm& t) |
2928 | 40 { |
11042
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
41 octave_scalar_map m; |
2928 | 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 | |
11042
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
58 static inline int |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
59 intfield (const octave_scalar_map& m, const std::string& k) |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
60 { |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
61 int retval = 0; |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
62 |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
63 octave_value v = m.getfield (k); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
64 |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
65 if (! v.is_empty ()) |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
66 retval = v.int_value (); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
67 |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
68 return retval; |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
69 } |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
70 |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
71 static inline std::string |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
72 stringfield (const octave_scalar_map& m, const std::string& k) |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
73 { |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
74 std::string retval; |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
75 |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
76 octave_value v = m.getfield (k); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
77 |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
78 if (! v.is_empty ()) |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
79 retval = v.string_value (); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
80 |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
81 return retval; |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
82 } |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
83 |
3254 | 84 static octave_base_tm |
11042
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
85 extract_tm (const octave_scalar_map& m) |
2928 | 86 { |
3254 | 87 octave_base_tm tm; |
2928 | 88 |
11042
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
89 tm.usec (intfield (m, "usec")); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
90 tm.sec (intfield (m, "sec")); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
91 tm.min (intfield (m, "min")); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
92 tm.hour (intfield (m, "hour")); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
93 tm.mday (intfield (m, "mday")); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
94 tm.mon (intfield (m, "mon")); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
95 tm.year (intfield (m, "year")); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
96 tm.wday (intfield (m, "wday")); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
97 tm.yday (intfield (m, "yday")); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
98 tm.isdst (intfield (m, "isdst")); |
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
99 tm.zone (stringfield (m, "zone")); |
2928 | 100 |
3254 | 101 return tm; |
2928 | 102 } |
103 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
104 DEFUN (time, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
105 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
106 @deftypefn {Built-in Function} {@var{seconds} =} time ()\n\ |
3301 | 107 Return the current time as the number of seconds since the epoch. The\n\ |
108 epoch is referenced to 00:00:00 CUT (Coordinated Universal Time) 1 Jan\n\ | |
109 1970. For example, on Monday February 17, 1997 at 07:15:06 CUT, the\n\ | |
110 value returned by @code{time} was 856163706.\n\ | |
5688 | 111 @seealso{strftime, strptime, localtime, gmtime, mktime, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3301 | 112 @end deftypefn") |
2928 | 113 { |
3255 | 114 octave_value retval; |
2928 | 115 |
3255 | 116 if (args.length () == 0) |
7065 | 117 retval = octave_time (); |
3255 | 118 else |
5823 | 119 print_usage (); |
3255 | 120 |
121 return retval; | |
2928 | 122 } |
123 | |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
124 /* |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
125 %!assert (time () > 0) |
19176
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
126 |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
127 %!error time (1) |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
128 */ |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
129 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
130 DEFUN (gmtime, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
131 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
132 @deftypefn {Built-in Function} {@var{tm_struct} =} gmtime (@var{t})\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
133 Given a value returned from @code{time}, or any non-negative integer,\n\ |
12642
f96b9b9f141b
doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
11553
diff
changeset
|
134 return a time structure corresponding to CUT (Coordinated Universal Time).\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
135 For example:\n\ |
2928 | 136 \n\ |
3301 | 137 @example\n\ |
138 @group\n\ | |
139 gmtime (time ())\n\ | |
140 @result{} @{\n\ | |
141 usec = 0\n\ | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
142 sec = 6\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
143 min = 15\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
144 hour = 7\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
145 mday = 17\n\ |
3301 | 146 mon = 1\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
147 year = 97\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
148 wday = 1\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
149 yday = 47\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
150 isdst = 0\n\ |
3301 | 151 zone = CST\n\ |
14360
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
152 @}\n\ |
3301 | 153 @end group\n\ |
154 @end example\n\ | |
5688 | 155 @seealso{strftime, strptime, localtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3301 | 156 @end deftypefn") |
2928 | 157 { |
3465 | 158 octave_value retval; |
2928 | 159 |
160 if (args.length () == 1) | |
161 { | |
162 double tmp = args(0).double_value (); | |
163 | |
164 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
165 retval = octave_value (mk_tm_map (octave_gmtime (tmp))); |
2928 | 166 } |
167 else | |
5823 | 168 print_usage (); |
2928 | 169 |
170 return retval; | |
171 } | |
172 | |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
173 /* |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
174 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
175 %! ts = gmtime (time ()); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
176 %! assert (isstruct (ts)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
177 %! assert (isfield (ts, "usec")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
178 %! assert (isfield (ts, "year")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
179 %! assert (isfield (ts, "mon")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
180 %! assert (isfield (ts, "mday")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
181 %! assert (isfield (ts, "sec")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
182 %! assert (isfield (ts, "min")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
183 %! assert (isfield (ts, "wday")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
184 %! assert (isfield (ts, "hour")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
185 %! assert (isfield (ts, "isdst")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
186 %! assert (isfield (ts, "yday")); |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
187 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
188 %!error gmtime () |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
189 %!error gmtime (1, 2) |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
190 */ |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
191 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
192 DEFUN (localtime, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
193 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
194 @deftypefn {Built-in Function} {@var{tm_struct} =} localtime (@var{t})\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
195 Given a value returned from @code{time}, or any non-negative integer,\n\ |
3301 | 196 return a time structure corresponding to the local time zone.\n\ |
2928 | 197 \n\ |
3301 | 198 @example\n\ |
199 @group\n\ | |
200 localtime (time ())\n\ | |
201 @result{} @{\n\ | |
202 usec = 0\n\ | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
203 sec = 6\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
204 min = 15\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
205 hour = 1\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
206 mday = 17\n\ |
3301 | 207 mon = 1\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
208 year = 97\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
209 wday = 1\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
210 yday = 47\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
211 isdst = 0\n\ |
3301 | 212 zone = CST\n\ |
14360
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
213 @}\n\ |
3301 | 214 @end group\n\ |
215 @end example\n\ | |
5688 | 216 @seealso{strftime, strptime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3301 | 217 @end deftypefn") |
2928 | 218 { |
3465 | 219 octave_value retval; |
2928 | 220 |
221 if (args.length () == 1) | |
222 { | |
223 double tmp = args(0).double_value (); | |
224 | |
225 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
226 retval = octave_value (mk_tm_map (octave_localtime (tmp))); |
2928 | 227 } |
228 else | |
5823 | 229 print_usage (); |
2928 | 230 |
231 return retval; | |
232 } | |
233 | |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
234 /* |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
235 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
236 %! ts = localtime (time ()); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
237 %! assert (isstruct (ts)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
238 %! assert (isfield (ts, "usec")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
239 %! assert (isfield (ts, "year")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
240 %! assert (isfield (ts, "mon")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
241 %! assert (isfield (ts, "mday")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
242 %! assert (isfield (ts, "sec")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
243 %! assert (isfield (ts, "min")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
244 %! assert (isfield (ts, "wday")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
245 %! assert (isfield (ts, "hour")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
246 %! assert (isfield (ts, "isdst")); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
247 %! assert (isfield (ts, "yday")); |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
248 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
249 %!error localtime () |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
250 %!error localtime (1, 2) |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
251 */ |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
252 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
253 DEFUN (mktime, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
254 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
255 @deftypefn {Built-in Function} {@var{seconds} =} mktime (@var{tm_struct})\n\ |
3301 | 256 Convert a time structure corresponding to the local time to the number\n\ |
10840 | 257 of seconds since the epoch. For example:\n\ |
3301 | 258 \n\ |
259 @example\n\ | |
260 @group\n\ | |
7097 | 261 mktime (localtime (time ()))\n\ |
3301 | 262 @result{} 856163706\n\ |
263 @end group\n\ | |
264 @end example\n\ | |
5688 | 265 @seealso{strftime, strptime, localtime, gmtime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3301 | 266 @end deftypefn") |
2928 | 267 { |
3465 | 268 octave_value retval; |
2928 | 269 |
3254 | 270 if (args.length () == 1) |
2928 | 271 { |
11042
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
272 octave_scalar_map map = args(0).scalar_map_value (); |
2928 | 273 |
3254 | 274 if (! error_state) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
275 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
276 octave_base_tm tm = extract_tm (map); |
3254 | 277 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
278 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
279 retval = octave_time (tm); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
280 else |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
281 error ("mktime: invalid TM_STRUCT argument"); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
282 } |
3254 | 283 else |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
284 error ("mktime: TM_STRUCT argument must be a structure"); |
2928 | 285 } |
286 else | |
5823 | 287 print_usage (); |
2928 | 288 |
289 return retval; | |
290 } | |
291 | |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
292 /* |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
293 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
294 %! t = time (); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
295 %! assert (fix (mktime (localtime (t))) == fix (t)); |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
296 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
297 ## These tests fail on systems with mktime functions of limited |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
298 ## intelligence: |
11005
0de4eff677d6
use mktime module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10840
diff
changeset
|
299 %!assert (datestr (datenum (1969, 1, 1), 0), "01-Jan-1969 00:00:00") |
0de4eff677d6
use mktime module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10840
diff
changeset
|
300 %!assert (datestr (datenum (1901, 1, 1), 0), "01-Jan-1901 00:00:00") |
0de4eff677d6
use mktime module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10840
diff
changeset
|
301 %!assert (datestr (datenum (1795, 1, 1), 0), "01-Jan-1795 00:00:00") |
0de4eff677d6
use mktime module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10840
diff
changeset
|
302 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
303 %!error mktime () |
19176
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
304 %!error mktime (1) |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
305 %!error mktime (1, 2, 3) |
19176
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
306 %!error mktime (struct ("year", "foo")) |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
307 */ |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
308 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
309 DEFUN (strftime, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
310 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
311 @deftypefn {Built-in Function} {} strftime (@var{fmt}, @var{tm_struct})\n\ |
5675 | 312 Format the time structure @var{tm_struct} in a flexible way using the\n\ |
313 format string @var{fmt} that contains @samp{%} substitutions\n\ | |
3295 | 314 similar to those in @code{printf}. Except where noted, substituted\n\ |
315 fields have a fixed size; numeric fields are padded if necessary.\n\ | |
316 Padding is with zeros by default; for fields that display a single\n\ | |
317 number, padding can be changed or inhibited by following the @samp{%}\n\ | |
318 with one of the modifiers described below. Unknown field specifiers are\n\ | |
319 copied as normal characters. All other characters are copied to the\n\ | |
10840 | 320 output without change. For example:\n\ |
2928 | 321 \n\ |
3295 | 322 @example\n\ |
323 @group\n\ | |
5020 | 324 strftime (\"%r (%Z) %A %e %B %Y\", localtime (time ()))\n\ |
14360
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
325 @result{} \"01:15:06 AM (CST) Monday 17 February 1997\"\n\ |
3295 | 326 @end group\n\ |
327 @end example\n\ | |
2928 | 328 \n\ |
3295 | 329 Octave's @code{strftime} function supports a superset of the ANSI C\n\ |
330 field specifiers.\n\ | |
2928 | 331 \n\ |
3295 | 332 @noindent\n\ |
2928 | 333 Literal character fields:\n\ |
334 \n\ | |
3295 | 335 @table @code\n\ |
12776
944cf42c699e
doc: small documentation fixes for strftime, betainc
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
336 @item %%\n\ |
3295 | 337 % character.\n\ |
2928 | 338 \n\ |
12776
944cf42c699e
doc: small documentation fixes for strftime, betainc
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
339 @item %n\n\ |
3295 | 340 Newline character.\n\ |
341 \n\ | |
12776
944cf42c699e
doc: small documentation fixes for strftime, betainc
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
342 @item %t\n\ |
3295 | 343 Tab character.\n\ |
344 @end table\n\ | |
345 \n\ | |
346 @noindent\n\ | |
2928 | 347 Numeric modifiers (a nonstandard extension):\n\ |
348 \n\ | |
3295 | 349 @table @code\n\ |
350 @item - (dash)\n\ | |
351 Do not pad the field.\n\ | |
2928 | 352 \n\ |
3295 | 353 @item _ (underscore)\n\ |
354 Pad the field with spaces.\n\ | |
355 @end table\n\ | |
356 \n\ | |
357 @noindent\n\ | |
2928 | 358 Time fields:\n\ |
359 \n\ | |
3295 | 360 @table @code\n\ |
361 @item %H\n\ | |
362 Hour (00-23).\n\ | |
363 \n\ | |
364 @item %I\n\ | |
365 Hour (01-12).\n\ | |
366 \n\ | |
367 @item %k\n\ | |
368 Hour (0-23).\n\ | |
369 \n\ | |
370 @item %l\n\ | |
371 Hour (1-12).\n\ | |
372 \n\ | |
373 @item %M\n\ | |
374 Minute (00-59).\n\ | |
375 \n\ | |
376 @item %p\n\ | |
377 Locale's AM or PM.\n\ | |
2928 | 378 \n\ |
3295 | 379 @item %r\n\ |
380 Time, 12-hour (hh:mm:ss [AP]M).\n\ | |
381 \n\ | |
382 @item %R\n\ | |
383 Time, 24-hour (hh:mm).\n\ | |
384 \n\ | |
385 @item %s\n\ | |
386 Time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension).\n\ | |
387 \n\ | |
388 @item %S\n\ | |
389 Second (00-61).\n\ | |
390 \n\ | |
391 @item %T\n\ | |
392 Time, 24-hour (hh:mm:ss).\n\ | |
393 \n\ | |
394 @item %X\n\ | |
395 Locale's time representation (%H:%M:%S).\n\ | |
396 \n\ | |
397 @item %Z\n\ | |
398 Time zone (EDT), or nothing if no time zone is determinable.\n\ | |
399 @end table\n\ | |
400 \n\ | |
401 @noindent\n\ | |
2928 | 402 Date fields:\n\ |
403 \n\ | |
3295 | 404 @table @code\n\ |
405 @item %a\n\ | |
406 Locale's abbreviated weekday name (Sun-Sat).\n\ | |
407 \n\ | |
408 @item %A\n\ | |
409 Locale's full weekday name, variable length (Sunday-Saturday).\n\ | |
410 \n\ | |
411 @item %b\n\ | |
412 Locale's abbreviated month name (Jan-Dec).\n\ | |
413 \n\ | |
414 @item %B\n\ | |
415 Locale's full month name, variable length (January-December).\n\ | |
416 \n\ | |
417 @item %c\n\ | |
418 Locale's date and time (Sat Nov 04 12:02:33 EST 1989).\n\ | |
419 \n\ | |
420 @item %C\n\ | |
421 Century (00-99).\n\ | |
422 \n\ | |
423 @item %d\n\ | |
424 Day of month (01-31).\n\ | |
425 \n\ | |
426 @item %e\n\ | |
427 Day of month ( 1-31).\n\ | |
428 \n\ | |
429 @item %D\n\ | |
430 Date (mm/dd/yy).\n\ | |
431 \n\ | |
432 @item %h\n\ | |
433 Same as %b.\n\ | |
434 \n\ | |
435 @item %j\n\ | |
436 Day of year (001-366).\n\ | |
437 \n\ | |
438 @item %m\n\ | |
439 Month (01-12).\n\ | |
440 \n\ | |
441 @item %U\n\ | |
442 Week number of year with Sunday as first day of week (00-53).\n\ | |
443 \n\ | |
444 @item %w\n\ | |
445 Day of week (0-6).\n\ | |
446 \n\ | |
447 @item %W\n\ | |
448 Week number of year with Monday as first day of week (00-53).\n\ | |
449 \n\ | |
450 @item %x\n\ | |
451 Locale's date representation (mm/dd/yy).\n\ | |
452 \n\ | |
453 @item %y\n\ | |
454 Last two digits of year (00-99).\n\ | |
455 \n\ | |
456 @item %Y\n\ | |
457 Year (1970-).\n\ | |
458 @end table\n\ | |
5688 | 459 @seealso{strptime, localtime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
5646 | 460 @end deftypefn") |
2928 | 461 { |
3465 | 462 octave_value retval; |
2928 | 463 |
3254 | 464 if (args.length () == 2) |
2928 | 465 { |
19437
03067dab10ca
Use stricter input validation when looking for a string as input (bug #42651).
Rik <rik@octave.org>
parents:
19176
diff
changeset
|
466 if (args(0).is_string ()) |
03067dab10ca
Use stricter input validation when looking for a string as input (bug #42651).
Rik <rik@octave.org>
parents:
19176
diff
changeset
|
467 { |
03067dab10ca
Use stricter input validation when looking for a string as input (bug #42651).
Rik <rik@octave.org>
parents:
19176
diff
changeset
|
468 std::string fmt = args(0).string_value (); |
2928 | 469 |
11042
f6b7b6e5b8b8
time.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
470 octave_scalar_map map = args(1).scalar_map_value (); |
2928 | 471 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
472 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
473 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
474 octave_base_tm tm = extract_tm (map); |
3233 | 475 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
476 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
477 retval = tm.strftime (fmt); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
478 else |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
479 error ("strftime: invalid TM_STRUCT argument"); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
480 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
481 else |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
482 error ("strftime: TM_STRUCT must be a structure"); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
483 } |
3254 | 484 else |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
485 error ("strftime: FMT must be a string"); |
2928 | 486 } |
487 else | |
5823 | 488 print_usage (); |
2928 | 489 |
490 return retval; | |
491 } | |
492 | |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
493 /* |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
494 %!assert (ischar (strftime ("%%%n%t%H%I%k%l", localtime (time ())))); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
495 %!assert (ischar (strftime ("%M%p%r%R%s%S%T", localtime (time ())))); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
496 %!assert (ischar (strftime ("%X%Z%z%a%A%b%B", localtime (time ())))); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
497 %!assert (ischar (strftime ("%c%C%d%e%D%h%j", localtime (time ())))); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
498 %!assert (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
|
499 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
500 %!error strftime () |
19176
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
501 %!error strftime ("foo", 1) |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
502 %!error strftime ("foo", struct ("year", "foo")) |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
503 %!error strftime ("foo", localtime (time ()), 1) |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
504 */ |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7097
diff
changeset
|
505 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
506 DEFUN (strptime, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
507 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
508 @deftypefn {Built-in Function} {[@var{tm_struct}, @var{nchars}] =} strptime (@var{str}, @var{fmt})\n\ |
5675 | 509 Convert the string @var{str} to the time structure @var{tm_struct} under\n\ |
510 the control of the format string @var{fmt}.\n\ | |
511 \n\ | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
512 If @var{fmt} fails to match, @var{nchars} is 0; otherwise, it is set to the\n\ |
5675 | 513 position of last matched character plus 1. Always check for this unless\n\ |
514 you're absolutely sure the date string will be parsed correctly.\n\ | |
5688 | 515 @seealso{strftime, localtime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\ |
3465 | 516 @end deftypefn") |
517 { | |
518 octave_value_list retval; | |
519 | |
520 if (args.length () == 2) | |
521 { | |
19437
03067dab10ca
Use stricter input validation when looking for a string as input (bug #42651).
Rik <rik@octave.org>
parents:
19176
diff
changeset
|
522 if (args(0).is_string ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
523 { |
19437
03067dab10ca
Use stricter input validation when looking for a string as input (bug #42651).
Rik <rik@octave.org>
parents:
19176
diff
changeset
|
524 std::string str = args(0).string_value (); |
3465 | 525 |
19437
03067dab10ca
Use stricter input validation when looking for a string as input (bug #42651).
Rik <rik@octave.org>
parents:
19176
diff
changeset
|
526 if (args(1).is_string ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
527 { |
19437
03067dab10ca
Use stricter input validation when looking for a string as input (bug #42651).
Rik <rik@octave.org>
parents:
19176
diff
changeset
|
528 std::string fmt = args(1).string_value (); |
03067dab10ca
Use stricter input validation when looking for a string as input (bug #42651).
Rik <rik@octave.org>
parents:
19176
diff
changeset
|
529 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
530 octave_strptime t (str, fmt); |
3465 | 531 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
532 retval(1) = t.characters_converted (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
533 retval(0) = octave_value (mk_tm_map (t)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
534 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
535 else |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
536 error ("strptime: FMT must be a string"); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9209
diff
changeset
|
537 } |
3465 | 538 else |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
539 error ("strptime: argument STR must be a string"); |
3465 | 540 } |
541 else | |
5823 | 542 print_usage (); |
3465 | 543 |
544 return retval; | |
545 } | |
19176
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
546 |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
547 /* |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
548 %!test |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
549 %! fmt = "%Y-%m-%d %H:%M:%S"; |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
550 %! s = strftime (fmt, localtime (time ())); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
551 %! ts = strptime (s, fmt); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
552 %! assert (isstruct (ts)); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
553 %! assert (isfield (ts, "usec")); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
554 %! assert (isfield (ts, "year")); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
555 %! assert (isfield (ts, "mon")); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
556 %! assert (isfield (ts, "mday")); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
557 %! assert (isfield (ts, "sec")); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
558 %! assert (isfield (ts, "min")); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
559 %! assert (isfield (ts, "wday")); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
560 %! assert (isfield (ts, "hour")); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
561 %! assert (isfield (ts, "isdst")); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
562 %! assert (isfield (ts, "yday")); |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
563 |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
564 %!error strptime () |
b8bd0b55af45
codesprint: Add tests to time handling functions
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
565 */ |