Mercurial > hg > octave-nkf
annotate scripts/time/datevec.m @ 20441:83792dd9bcc1
Use in-place operators in m-files where possible.
* scripts/audio/@audioplayer/set.m, scripts/audio/@audiorecorder/set.m,
scripts/audio/mu2lin.m, scripts/elfun/cosd.m, scripts/general/del2.m,
scripts/general/profexplore.m, scripts/general/quadl.m, scripts/general/rat.m,
scripts/general/rotdim.m, scripts/help/get_first_help_sentence.m,
scripts/help/private/__strip_html_tags__.m, scripts/image/cubehelix.m,
scripts/io/textread.m, scripts/linear-algebra/duplication_matrix.m,
scripts/linear-algebra/housh.m, scripts/linear-algebra/krylov.m,
scripts/linear-algebra/logm.m, scripts/linear-algebra/normest.m,
scripts/linear-algebra/onenormest.m, scripts/optimization/fminsearch.m,
scripts/optimization/lsqnonneg.m, scripts/optimization/qp.m,
scripts/plot/appearance/annotation.m, scripts/plot/appearance/axis.m,
scripts/plot/appearance/legend.m, scripts/plot/appearance/specular.m,
scripts/plot/draw/colorbar.m, scripts/plot/draw/hist.m,
scripts/plot/draw/plotmatrix.m, scripts/plot/draw/private/__stem__.m,
scripts/plot/util/__actual_axis_position__.m,
scripts/plot/util/__gnuplot_drawnow__.m, scripts/plot/util/findobj.m,
scripts/plot/util/print.m, scripts/plot/util/private/__go_draw_axes__.m,
scripts/plot/util/private/__print_parse_opts__.m, scripts/plot/util/rotate.m,
scripts/polynomial/pchip.m, scripts/polynomial/polyaffine.m,
scripts/polynomial/polyder.m, scripts/polynomial/private/__splinefit__.m,
scripts/polynomial/residue.m, scripts/signal/arch_fit.m,
scripts/signal/arch_rnd.m, scripts/signal/bartlett.m,
scripts/signal/blackman.m, scripts/signal/freqz.m, scripts/signal/hamming.m,
scripts/signal/hanning.m, scripts/signal/spectral_adf.m,
scripts/signal/spectral_xdf.m, scripts/signal/stft.m,
scripts/sparse/bicgstab.m, scripts/sparse/cgs.m,
scripts/sparse/private/__sprand_impl__.m, scripts/sparse/qmr.m,
scripts/sparse/sprandsym.m, scripts/sparse/svds.m, scripts/specfun/legendre.m,
scripts/special-matrix/gallery.m, scripts/statistics/base/gls.m,
scripts/statistics/models/logistic_regression.m,
scripts/statistics/tests/kruskal_wallis_test.m,
scripts/statistics/tests/manova.m, scripts/statistics/tests/wilcoxon_test.m,
scripts/time/datevec.m:
Use in-place operators in m-files where possible.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 26 May 2015 21:07:42 -0700 |
parents | 2645f9ef8c88 |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19793
diff
changeset
|
1 ## Copyright (C) 2000-2015 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 | |
14743
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
32 ## (see @code{datestr}). If @var{date} is a string, but no format is |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
33 ## specified, then a relatively slow search is performed through various |
20372
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
34 ## formats. It is always preferable to specify the format string @var{f} if it |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
35 ## is known. Formats which do not specify a particular time component will |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
36 ## have the value set to zero. Formats which do not specify a date will |
14743
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
37 ## default to January 1st of the current year. |
5687 | 38 ## |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
39 ## @var{p} is the year at the start of the century to which two-digit years |
20372
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
40 ## will be referenced. If not specified, it defaults to the current year minus |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
41 ## 50. |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13848
diff
changeset
|
42 ## @seealso{datenum, datestr, clock, now, date} |
5687 | 43 ## @end deftypefn |
44 | |
45 ## Algorithm: Peter Baum (http://vsg.cape.com/~pbaum/date/date0.htm) | |
46 | |
47 ## Author: pkienzle <pkienzle@users.sf.net> | |
48 ## Modified: bdenney <bill@givebillmoney.com> | |
49 ## Created: 10 October 2001 (CVS) | |
50 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com> | |
51 | |
52 ## The function __date_str2vec__ is based on datesplit by Bill Denney. | |
53 | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
54 function [y, m, d, h, mi, s] = datevec (date, f = [], p = []) |
5687 | 55 |
56 persistent std_formats nfmt; | |
57 | |
58 if (isempty (std_formats)) | |
59 std_formats = cell (); | |
60 nfmt = 0; | |
14743
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
61 ## These formats are specified by Matlab documentation to be parsed |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
62 ## The '# XX' refers to the datestr numerical format code |
5687 | 63 std_formats{++nfmt} = "dd-mmm-yyyy HH:MM:SS"; # 0 |
64 std_formats{++nfmt} = "dd-mmm-yyyy"; # 1 | |
65 std_formats{++nfmt} = "mm/dd/yy"; # 2 | |
66 std_formats{++nfmt} = "mm/dd"; # 6 | |
67 std_formats{++nfmt} = "HH:MM:SS"; # 13 | |
68 std_formats{++nfmt} = "HH:MM:SS PM"; # 14 | |
69 std_formats{++nfmt} = "HH:MM"; # 15 | |
70 std_formats{++nfmt} = "HH:MM PM"; # 16 | |
71 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
|
72 |
14743
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
73 ## These formats are undocumented but parsed by Matlab |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
74 std_formats{++nfmt} = "mmmyy"; # 12 |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
75 std_formats{++nfmt} = "mmm.dd,yyyy HH:MM:SS"; # 21 |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
76 std_formats{++nfmt} = "mmm.dd,yyyy"; # 22 |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
77 std_formats{++nfmt} = "yyyy/mm/dd"; # 26 |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
78 std_formats{++nfmt} = "yyyy-mm-dd"; # 29 |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
79 std_formats{++nfmt} = "yyyy-mm-dd HH:MM:SS"; # 31 |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
80 |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
81 ## 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
|
82 std_formats{++nfmt} = "mmm-dd-yyyy HH:MM:SS"; |
6044 | 83 std_formats{++nfmt} = "mmm-dd-yyyy"; |
84 std_formats{++nfmt} = "dd mmm yyyy HH:MM:SS"; | |
85 std_formats{++nfmt} = "dd mmm yyyy"; | |
86 std_formats{++nfmt} = "mmm dd yyyy HH:MM:SS"; | |
87 std_formats{++nfmt} = "mmm dd yyyy"; | |
88 std_formats{++nfmt} = "dd.mmm.yyyy HH:MM:SS"; | |
89 std_formats{++nfmt} = "dd.mmm.yyyy"; | |
90 std_formats{++nfmt} = "mmm.dd.yyyy HH:MM:SS"; | |
91 std_formats{++nfmt} = "mmm.dd.yyyy"; | |
5687 | 92 std_formats{++nfmt} = "mm/dd/yyyy HH:MM"; |
93 endif | |
94 | |
95 if (nargin < 1 || nargin > 3) | |
6046 | 96 print_usage (); |
5687 | 97 endif |
98 | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
99 if (ischar (date)) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
100 date = cellstr (date); |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
101 endif |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
102 |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
103 if (isnumeric (f)) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
104 p = f; |
5687 | 105 f = []; |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
106 endif |
5687 | 107 |
108 if (isempty (f)) | |
109 f = -1; | |
110 endif | |
111 | |
112 if (isempty (p)) | |
11400 | 113 p = (localtime (time ())).year + 1900 - 50; |
5687 | 114 endif |
115 | |
18919
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
116 do_resize = false; |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
117 |
5687 | 118 if (iscell (date)) |
119 | |
120 nd = numel (date); | |
121 | |
122 y = m = d = h = mi = s = zeros (nd, 1); | |
123 | |
124 if (f == -1) | |
125 for k = 1:nd | |
126 found = false; | |
127 for l = 1:nfmt | |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
128 [f, rY, ry, fy, fm, fd, fh, fmi, fs] = ... |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
129 __date_vfmt2sfmt__ (std_formats{l}); |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
130 [found y(k) m(k) d(k) h(k) mi(k) s(k)] = ... |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
131 __date_str2vec__ (date{k}, p, f, rY, ry, fy, fm, fd, fh, fmi, fs); |
5687 | 132 if (found) |
133 break; | |
134 endif | |
135 endfor | |
136 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
|
137 error ("datevec: none of the standard formats match the DATE string"); |
5687 | 138 endif |
139 endfor | |
140 else | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
141 ## 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
|
142 [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f); |
5687 | 143 for k = 1:nd |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
144 [found y(k) m(k) d(k) h(k) mi(k) s(k)] = ... |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
145 __date_str2vec__ (date{k}, p, f, rY, ry, fy, fm, fd, fh, fmi, fs); |
5687 | 146 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
|
147 error ("datevec: DATE not parsed correctly with given format"); |
5687 | 148 endif |
149 endfor | |
150 endif | |
151 | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
152 else # datenum input |
5687 | 153 |
18921
38925538ec14
datevec.m: Change isrow to iscolumn in cset 9eb72fa5f8b5.
Rik <rik@octave.org>
parents:
18919
diff
changeset
|
154 if (! iscolumn (date)) |
18919
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
155 date_sz = size (date); |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
156 do_resize = true; |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
157 endif |
5687 | 158 date = date(:); |
159 | |
160 ## Move day 0 from midnight -0001-12-31 to midnight 0000-3-1 | |
19579
7d272300a880
datevec.m: Fix negative number outputs with integer (non-float) inputs (bug #43753).
Nir Krakauer <nkrakauer@ccny.cuny.edu>
parents:
17744
diff
changeset
|
161 z = double (floor (date) - 60); |
5687 | 162 ## Calculate number of centuries; K1 = 0.25 is to avoid rounding problems. |
163 a = floor ((z - 0.25) / 36524.25); | |
164 ## Days within century; K2 = 0.25 is to avoid rounding problems. | |
165 b = z - 0.25 + a - floor (a / 4); | |
166 ## Calculate the year (year starts on March 1). | |
167 y = floor (b / 365.25); | |
168 ## Calculate day in year. | |
169 c = fix (b - floor (365.25 * y)) + 1; | |
170 ## Calculate month in year. | |
171 m = fix ((5 * c + 456) / 153); | |
172 d = c - fix ((153 * m - 457) / 5); | |
173 ## Move to Jan 1 as start of year. | |
174 ++y(m > 12); | |
175 m(m > 12) -= 12; | |
176 | |
5859 | 177 ## Convert hour-minute-seconds. Attempt to account for precision of |
178 ## datenum format. | |
179 | |
180 fracd = date - floor (date); | |
5873 | 181 tmps = abs (eps*86400*date); |
182 tmps(tmps == 0) = 1; | |
183 srnd = 2 .^ floor (- log2 (tmps)); | |
184 s = round (86400 * fracd .* srnd) ./ srnd; | |
5687 | 185 h = floor (s / 3600); |
20441
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20372
diff
changeset
|
186 s -= 3600 * h; |
5687 | 187 mi = floor (s / 60); |
20441
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20372
diff
changeset
|
188 s -= 60 * mi; |
5687 | 189 |
190 endif | |
191 | |
192 if (nargout <= 1) | |
5873 | 193 y = [y, m, d, h, mi, s]; |
18919
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
194 elseif (do_resize) |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
195 y = reshape (y, date_sz); |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
196 m = reshape (m, date_sz); |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
197 d = reshape (d, date_sz); |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
198 h = reshape (h, date_sz); |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
199 mi = reshape (mi, date_sz); |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
200 s = reshape (s, date_sz); |
5687 | 201 endif |
202 | |
11086
af03ff97df7b
datevec.m: use endfunction to mark end of primary function and subfunctions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
203 endfunction |
5687 | 204 |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
205 function [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f) |
5687 | 206 |
8506 | 207 ## 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
|
208 f = strrep (f, "%", "%%"); |
5687 | 209 |
210 if (! isempty (strfind (f, "PM")) || ! isempty (strfind (f, "AM"))) | |
211 ampm = true; | |
212 else | |
213 ampm = false; | |
214 endif | |
215 | |
8506 | 216 ## Date part. |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
217 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
|
218 f = regexprep (f, '[Yy][Yy]', "%y"); |
5687 | 219 f = strrep (f, "mmmm", "%B"); |
220 f = strrep (f, "mmm", "%b"); | |
221 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
|
222 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
|
223 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
|
224 f = regexprep (f, '[Dd][Dd]', "%d"); |
5687 | 225 |
8506 | 226 ## Time part. |
5687 | 227 if (ampm) |
228 f = strrep (f, "HH", "%I"); | |
229 f = strrep (f, "PM", "%p"); | |
230 f = strrep (f, "AM", "%p"); | |
231 else | |
232 f = strrep (f, "HH", "%H"); | |
233 endif | |
234 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
|
235 f = regexprep (f, '[Ss][Ss]', "%S"); |
5687 | 236 |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
237 rY = rindex (f, "%Y"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
238 ry = rindex (f, "%y"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
239 |
8506 | 240 ## Check whether we need to give default values. |
241 ## 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
|
242 fy = rY || ry; |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
243 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
|
244 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
|
245 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
|
246 fmi = index (f, "%M"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
247 fs = index (f, "%S"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
248 |
11086
af03ff97df7b
datevec.m: use endfunction to mark end of primary function and subfunctions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
249 endfunction |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
250 |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
251 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
|
252 |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
253 idx = strfind (f, "FFF"); |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19579
diff
changeset
|
254 if (! isempty (idx)) |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
255 ## Kludge to handle FFF millisecond format since strptime does not |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19579
diff
changeset
|
256 f(idx:idx+2) = []; |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
257 [~, 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
|
258 if (nc > 0) |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19579
diff
changeset
|
259 msec = ds(nc:min(nc+2, end)); |
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19579
diff
changeset
|
260 f = [f(1:idx-1) msec f(idx:end)]; |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
261 [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
|
262 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
|
263 endif |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
264 else |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
265 [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
|
266 endif |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19579
diff
changeset
|
267 |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
268 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
|
269 found = true; |
5687 | 270 y = tm.year + 1900; m = tm.mon + 1; d = tm.mday; |
271 h = tm.hour; mi = tm.min; s = tm.sec + tm.usec / 1e6; | |
272 if (rY < ry) | |
273 if (y > 1999) | |
274 y -= 2000; | |
275 else | |
276 y -= 1900; | |
277 endif | |
278 y += p - mod (p, 100); | |
279 if (y < p) | |
280 y += 100; | |
281 endif | |
282 endif | |
283 if (! fy && ! fm && ! fd) | |
8506 | 284 tmp = localtime (time ()); |
14743
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
285 ## default is January 1st of current year |
8506 | 286 y = tmp.year + 1900; |
14743
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
287 m = 1; |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
288 d = 1; |
5687 | 289 elseif (! fy && fm && fd) |
8506 | 290 tmp = localtime (time ()); |
291 y = tmp.year + 1900; | |
5687 | 292 elseif (fy && fm && ! fd) |
293 d = 1; | |
294 endif | |
295 if (! fh && ! fmi && ! fs) | |
296 h = mi = s = 0; | |
297 elseif (fh && fmi && ! fs) | |
298 s = 0; | |
299 endif | |
300 else | |
301 y = m = d = h = mi = s = 0; | |
302 found = false; | |
303 endif | |
304 | |
11086
af03ff97df7b
datevec.m: use endfunction to mark end of primary function and subfunctions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
305 endfunction |
5687 | 306 |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
307 |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
308 %!demo |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
309 %! ## 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
|
310 %! datevec (now ()) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
311 |
14743
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
312 %!shared yr |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
313 %! yr = datevec (now)(1); # Some tests could fail around midnight! |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
314 ## tests for standard formats: 0, 1, 2, 6, 13, 14, 15, 16, 23 |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
315 %!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
|
316 %!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
|
317 %!assert (datevec ("09/07/00"), [2000,9,7,0,0,0]) |
14743
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
318 %!assert (datevec ("09/13"), [yr,9,13,0,0,0]) |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
319 %!assert (datevec ("15:38:09"), [yr,1,1,15,38,9]) |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
320 %!assert (datevec ("3:38:09 PM"), [yr,1,1,15,38,9]) |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
321 %!assert (datevec ("15:38"), [yr,1,1,15,38,0]) |
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
322 %!assert (datevec ("03:38 PM"), [yr,1,1,15,38,0]) |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
323 %!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
|
324 |
18919
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
325 ## Test millisecond format FFF |
14743
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
326 %!assert (datevec ("15:38:21.25", "HH:MM:SS.FFF"), [yr,1,1,15,38,21.025]) |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
327 |
18919
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
328 ## Test structure of return value (bug #42334) |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
329 %!test |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
330 %! [~, ~, d] = datevec ([1 2; 3 4]); |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
331 %! assert (d, [1 2; 3 4]); |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
332 |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
333 ## Other tests |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
334 %!assert (datenum (datevec ([-1e4:1e4])), [-1e4:1e4]'); |
5687 | 335 %!test |
336 %! t = linspace (-2e5, 2e5, 10993); | |
5860 | 337 %! assert (all (abs (datenum (datevec (t)) - t') < 1e-5)); |
19579
7d272300a880
datevec.m: Fix negative number outputs with integer (non-float) inputs (bug #43753).
Nir Krakauer <nkrakauer@ccny.cuny.edu>
parents:
17744
diff
changeset
|
338 %!assert (double (datevec (int64 (datenum ([2014 6 1])))), datevec (datenum ([2014 6 1]))) |
7d272300a880
datevec.m: Fix negative number outputs with integer (non-float) inputs (bug #43753).
Nir Krakauer <nkrakauer@ccny.cuny.edu>
parents:
17744
diff
changeset
|
339 %!assert (double (datevec (int64 (datenum ([2014 6 18])))), datevec (datenum ([2014 6 18]))) |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
340 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
341 ## Test input validation |
18919
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
342 %!error datevec () |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
343 %!error datevec (1,2,3,4) |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
344 %!error <none of the standard formats match> datevec ("foobar") |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
345 %!error <DATE not parsed correctly with given format> datevec ("foobar", "%d") |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
346 |