Mercurial > hg > octave-nkf
annotate scripts/time/datevec.m @ 13856:d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
* addtodate.m: Add millisecond functionality. Update docstring and %!tests.
* calendar.m: Implement faster way to add '*' to day display. Update docstring.
* weekday.m: Use more modern coding stytle. Update docstring.
* asctime.m, clock.m, ctime.m, date.m, datenum.m, datestr.m, datevec.m,
eomday.m, etime.m, is_leap_year.m: Update docstring and/or use Octave formatting
spacing conventions for %!tests.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Thu, 10 Nov 2011 13:35:08 -0800 |
parents | 40e32fe44aaa |
children | 72c96de7a403 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 Paul Kienzle |
5687 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5687 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5687 | 18 |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
20 ## @deftypefn {Function File} {@var{v} =} datevec (@var{date}) |
5687 | 21 ## @deftypefnx {Function File} {@var{v} =} datevec (@var{date}, @var{f}) |
22 ## @deftypefnx {Function File} {@var{v} =} datevec (@var{date}, @var{p}) | |
23 ## @deftypefnx {Function File} {@var{v} =} datevec (@var{date}, @var{f}, @var{p}) | |
24 ## @deftypefnx {Function File} {[@var{y}, @var{m}, @var{d}, @var{h}, @var{mi}, @var{s}] =} datevec (@dots{}) | |
25 ## Convert a serial date number (see @code{datenum}) or date string (see | |
26 ## @code{datestr}) into a date vector. | |
27 ## | |
28 ## A date vector is a row vector with six members, representing the year, | |
29 ## month, day, hour, minute, and seconds respectively. | |
30 ## | |
31 ## @var{f} is the format string used to interpret date strings | |
32 ## (see @code{datestr}). | |
33 ## | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
34 ## @var{p} is the year at the start of the century to which two-digit years |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
35 ## will be referenced. If not specified, it defaults to the current year |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
36 ## minus 50. |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13848
diff
changeset
|
37 ## @seealso{datenum, datestr, clock, now, date} |
5687 | 38 ## @end deftypefn |
39 | |
40 ## Algorithm: Peter Baum (http://vsg.cape.com/~pbaum/date/date0.htm) | |
41 | |
42 ## Author: pkienzle <pkienzle@users.sf.net> | |
43 ## Modified: bdenney <bill@givebillmoney.com> | |
44 ## Created: 10 October 2001 (CVS) | |
45 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com> | |
46 | |
47 ## The function __date_str2vec__ is based on datesplit by Bill Denney. | |
48 | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
49 function [y, m, d, h, mi, s] = datevec (date, f = [], p = []) |
5687 | 50 |
51 persistent std_formats nfmt; | |
52 | |
53 if (isempty (std_formats)) | |
54 std_formats = cell (); | |
55 nfmt = 0; | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
56 ## These formats are specified by Matlab to be parsed |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
57 ## The '# XX' refers to the datestr numerical format code |
5687 | 58 std_formats{++nfmt} = "dd-mmm-yyyy HH:MM:SS"; # 0 |
59 std_formats{++nfmt} = "dd-mmm-yyyy"; # 1 | |
60 std_formats{++nfmt} = "mm/dd/yy"; # 2 | |
61 std_formats{++nfmt} = "mm/dd"; # 6 | |
62 std_formats{++nfmt} = "HH:MM:SS"; # 13 | |
63 std_formats{++nfmt} = "HH:MM:SS PM"; # 14 | |
64 std_formats{++nfmt} = "HH:MM"; # 15 | |
65 std_formats{++nfmt} = "HH:MM PM"; # 16 | |
66 std_formats{++nfmt} = "mm/dd/yyyy"; # 23 | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
67 |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
68 ## These are other formats that Octave tries |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
69 std_formats{++nfmt} = "mmm-dd-yyyy HH:MM:SS"; |
6044 | 70 std_formats{++nfmt} = "mmm-dd-yyyy"; |
71 std_formats{++nfmt} = "dd mmm yyyy HH:MM:SS"; | |
72 std_formats{++nfmt} = "dd mmm yyyy"; | |
73 std_formats{++nfmt} = "mmm dd yyyy HH:MM:SS"; | |
74 std_formats{++nfmt} = "mmm dd yyyy"; | |
75 std_formats{++nfmt} = "dd.mmm.yyyy HH:MM:SS"; | |
76 std_formats{++nfmt} = "dd.mmm.yyyy"; | |
77 std_formats{++nfmt} = "mmm.dd.yyyy HH:MM:SS"; | |
78 std_formats{++nfmt} = "mmm.dd.yyyy"; | |
5687 | 79 std_formats{++nfmt} = "mmmyy"; # 12 |
80 std_formats{++nfmt} = "mm/dd/yyyy HH:MM"; | |
81 endif | |
82 | |
83 if (nargin < 1 || nargin > 3) | |
6046 | 84 print_usage (); |
5687 | 85 endif |
86 | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
87 if (ischar (date)) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
88 date = cellstr (date); |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
89 endif |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
90 |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
91 if (isnumeric (f)) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
92 p = f; |
5687 | 93 f = []; |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
94 endif |
5687 | 95 |
96 if (isempty (f)) | |
97 f = -1; | |
98 endif | |
99 | |
100 if (isempty (p)) | |
11400 | 101 p = (localtime (time ())).year + 1900 - 50; |
5687 | 102 endif |
103 | |
104 if (iscell (date)) | |
105 | |
106 nd = numel (date); | |
107 | |
108 y = m = d = h = mi = s = zeros (nd, 1); | |
109 | |
110 if (f == -1) | |
111 for k = 1:nd | |
112 found = false; | |
113 for l = 1:nfmt | |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
114 [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (std_formats{l}); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
115 [found y(k) m(k) d(k) h(k) mi(k) s(k)] = __date_str2vec__ (date{k}, p, f, rY, ry, fy, fm, fd, fh, fmi, fs); |
5687 | 116 if (found) |
117 break; | |
118 endif | |
119 endfor | |
120 if (! found) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11400
diff
changeset
|
121 error ("datevec: none of the standard formats match the DATE string"); |
5687 | 122 endif |
123 endfor | |
124 else | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
125 ## Decipher the format string just once for speed. |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
126 [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f); |
5687 | 127 for k = 1:nd |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
128 [found y(k) m(k) d(k) h(k) mi(k) s(k)] = __date_str2vec__ (date{k}, p, f, rY, ry, fy, fm, fd, fh, fmi, fs); |
5687 | 129 if (! found) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11400
diff
changeset
|
130 error ("datevec: DATE not parsed correctly with given format"); |
5687 | 131 endif |
132 endfor | |
133 endif | |
134 | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
135 else # datenum input |
5687 | 136 |
137 date = date(:); | |
138 | |
139 ## Move day 0 from midnight -0001-12-31 to midnight 0000-3-1 | |
140 z = floor (date) - 60; | |
141 ## Calculate number of centuries; K1 = 0.25 is to avoid rounding problems. | |
142 a = floor ((z - 0.25) / 36524.25); | |
143 ## Days within century; K2 = 0.25 is to avoid rounding problems. | |
144 b = z - 0.25 + a - floor (a / 4); | |
145 ## Calculate the year (year starts on March 1). | |
146 y = floor (b / 365.25); | |
147 ## Calculate day in year. | |
148 c = fix (b - floor (365.25 * y)) + 1; | |
149 ## Calculate month in year. | |
150 m = fix ((5 * c + 456) / 153); | |
151 d = c - fix ((153 * m - 457) / 5); | |
152 ## Move to Jan 1 as start of year. | |
153 ++y(m > 12); | |
154 m(m > 12) -= 12; | |
155 | |
5859 | 156 ## Convert hour-minute-seconds. Attempt to account for precision of |
157 ## datenum format. | |
158 | |
159 fracd = date - floor (date); | |
5873 | 160 tmps = abs (eps*86400*date); |
161 tmps(tmps == 0) = 1; | |
162 srnd = 2 .^ floor (- log2 (tmps)); | |
163 s = round (86400 * fracd .* srnd) ./ srnd; | |
5687 | 164 h = floor (s / 3600); |
165 s = s - 3600 * h; | |
166 mi = floor (s / 60); | |
167 s = s - 60 * mi; | |
168 | |
169 endif | |
170 | |
171 if (nargout <= 1) | |
5873 | 172 y = [y, m, d, h, mi, s]; |
5687 | 173 endif |
174 | |
11086
af03ff97df7b
datevec.m: use endfunction to mark end of primary function and subfunctions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
175 endfunction |
5687 | 176 |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
177 function [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f) |
5687 | 178 |
8506 | 179 ## Play safe with percent signs. |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
180 f = strrep (f, "%", "%%"); |
5687 | 181 |
182 if (! isempty (strfind (f, "PM")) || ! isempty (strfind (f, "AM"))) | |
183 ampm = true; | |
184 else | |
185 ampm = false; | |
186 endif | |
187 | |
8506 | 188 ## Date part. |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
189 f = regexprep (f, '[Yy][Yy][Yy][Yy]', "%Y"); |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
190 f = regexprep (f, '[Yy][Yy]', "%y"); |
5687 | 191 f = strrep (f, "mmmm", "%B"); |
192 f = strrep (f, "mmm", "%b"); | |
193 f = strrep (f, "mm", "%m"); | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
194 f = regexprep (f, '[Dd][Dd][Dd][Dd]', "%A"); |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
195 f = regexprep (f, '[Dd][Dd][Dd]', "%a"); |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
196 f = regexprep (f, '[Dd][Dd]', "%d"); |
5687 | 197 |
8506 | 198 ## Time part. |
5687 | 199 if (ampm) |
200 f = strrep (f, "HH", "%I"); | |
201 f = strrep (f, "PM", "%p"); | |
202 f = strrep (f, "AM", "%p"); | |
203 else | |
204 f = strrep (f, "HH", "%H"); | |
205 endif | |
206 f = strrep (f, "MM", "%M"); | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
207 f = regexprep (f, '[Ss][Ss]', "%S"); |
5687 | 208 |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
209 rY = rindex (f, "%Y"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
210 ry = rindex (f, "%y"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
211 |
8506 | 212 ## Check whether we need to give default values. |
213 ## Possible error when string contains "%%". | |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
214 fy = rY || ry; |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
215 fm = index (f, "%m") || index (f, "%b") || index (f, "%B"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
216 fd = index (f, "%d") || index (f, "%a") || index (f, "%A"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
217 fh = index (f, "%H") || index (f, "%I"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
218 fmi = index (f, "%M"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
219 fs = index (f, "%S"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
220 |
11086
af03ff97df7b
datevec.m: use endfunction to mark end of primary function and subfunctions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
221 endfunction |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
222 |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
223 function [found, y, m, d, h, mi, s] = __date_str2vec__ (ds, p, f, rY, ry, fy, fm, fd, fh, fmi, fs) |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
224 |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
225 idx = strfind (f, "FFF"); |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
226 if (! isempty (idx)) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
227 ## Kludge to handle FFF millisecond format since strptime does not |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
228 f(idx:idx+2) = []; |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
229 [~, nc] = strptime (ds, f); |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
230 if (nc > 0) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
231 msec = ds(nc:min(nc+2, end)); |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
232 f = [f(1:idx-1) msec f(idx:end)]; |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
233 [tm, nc] = strptime (ds, f); |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
234 tm.usec = 1000 * str2double (msec); |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
235 endif |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
236 else |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
237 [tm, nc] = strptime (ds, f); |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
238 endif |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
239 |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
240 if (nc == columns (ds) + 1) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
241 found = true; |
5687 | 242 y = tm.year + 1900; m = tm.mon + 1; d = tm.mday; |
243 h = tm.hour; mi = tm.min; s = tm.sec + tm.usec / 1e6; | |
244 if (rY < ry) | |
245 if (y > 1999) | |
246 y -= 2000; | |
247 else | |
248 y -= 1900; | |
249 endif | |
250 y += p - mod (p, 100); | |
251 if (y < p) | |
252 y += 100; | |
253 endif | |
254 endif | |
255 if (! fy && ! fm && ! fd) | |
8506 | 256 tmp = localtime (time ()); |
257 y = tmp.year + 1900; | |
258 m = tmp.mon + 1; | |
259 d = tmp.mday; | |
5687 | 260 elseif (! fy && fm && fd) |
8506 | 261 tmp = localtime (time ()); |
262 y = tmp.year + 1900; | |
5687 | 263 elseif (fy && fm && ! fd) |
264 d = 1; | |
265 endif | |
266 if (! fh && ! fmi && ! fs) | |
267 h = mi = s = 0; | |
268 elseif (fh && fmi && ! fs) | |
269 s = 0; | |
270 endif | |
271 else | |
272 y = m = d = h = mi = s = 0; | |
273 found = false; | |
274 endif | |
275 | |
11086
af03ff97df7b
datevec.m: use endfunction to mark end of primary function and subfunctions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
276 endfunction |
5687 | 277 |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
278 |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
279 %!demo |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
280 %! ## Current date and time |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
281 %! datevec (now ()) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
282 |
5687 | 283 %!shared nowvec |
284 %! nowvec = datevec (now); # Some tests could fail around midnight! | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
285 %!# tests for standard formats: 0, 1, 2, 6, 13, 14, 15, 16, 23 |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
286 %!assert (datevec ("07-Sep-2000 15:38:09"), [2000,9,7,15,38,9]) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
287 %!assert (datevec ("07-Sep-2000"), [2000,9,7,0,0,0]) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
288 %!assert (datevec ("09/07/00"), [2000,9,7,0,0,0]) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
289 %!assert (datevec ("09/13"), [nowvec(1),9,13,0,0,0]) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
290 %!assert (datevec ("15:38:09"), [nowvec(1:3),15,38,9]) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
291 %!assert (datevec ("3:38:09 PM"), [nowvec(1:3),15,38,9]) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
292 %!assert (datevec ("15:38"), [nowvec(1:3),15,38,0]) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
293 %!assert (datevec ("03:38 PM"), [nowvec(1:3),15,38,0]) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
294 %!assert (datevec ("03/13/1962"), [1962,3,13,0,0,0]) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
295 |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
296 %% Test millisecond format FFF |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
297 %!assert (datevec ("15:38:21.25", "HH:MM:SS.FFF"), [nowvec(1:3),15,38,21.025]) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
298 |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
299 # Other tests |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
300 %!assert (datenum (datevec ([-1e4:1e4])), [-1e4:1e4]') |
5687 | 301 %!test |
302 %! t = linspace (-2e5, 2e5, 10993); | |
5860 | 303 %! assert (all (abs (datenum (datevec (t)) - t') < 1e-5)); |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
304 |