Mercurial > octave
annotate scripts/time/datevec.m @ 30645:beac32c0e9c7 stable
datevec.m: Allow day and day of week to occur in the same format string (bug #47627).
* scripts/time/datevec.m (__date_vfmt2sfmt__): Allow day and day of week
specifiers to occur (not more than once respectively) in the same format string.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Wed, 12 Jan 2022 17:55:32 +0100 |
parents | 796f54d4ddbf |
children | e9bfb92dab01 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
30379
diff
changeset
|
3 ## Copyright (C) 2000-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
5687 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23967
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
5687 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23967
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
13 ## (at your option) any later version. |
5687 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
18 ## GNU General Public License for more details. |
5687 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23967
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
5687 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20715
diff
changeset
|
27 ## @deftypefn {} {@var{v} =} datevec (@var{date}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20715
diff
changeset
|
28 ## @deftypefnx {} {@var{v} =} datevec (@var{date}, @var{f}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20715
diff
changeset
|
29 ## @deftypefnx {} {@var{v} =} datevec (@var{date}, @var{p}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20715
diff
changeset
|
30 ## @deftypefnx {} {@var{v} =} datevec (@var{date}, @var{f}, @var{p}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20715
diff
changeset
|
31 ## @deftypefnx {} {[@var{y}, @var{m}, @var{d}, @var{h}, @var{mi}, @var{s}] =} datevec (@dots{}) |
28958
6fd6ad758b10
doc: Use @xref, @pxref rather than "see @code{}" in TexInfo.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
32 ## Convert a serial date number (@pxref{XREFdatenum,,@code{datenum}}) or date |
6fd6ad758b10
doc: Use @xref, @pxref rather than "see @code{}" in TexInfo.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
33 ## string (@pxref{XREFdatestr,,@code{datestr}}) into a date vector. |
5687 | 34 ## |
35 ## A date vector is a row vector with six members, representing the year, | |
36 ## month, day, hour, minute, and seconds respectively. | |
37 ## | |
38 ## @var{f} is the format string used to interpret date strings | |
28958
6fd6ad758b10
doc: Use @xref, @pxref rather than "see @code{}" in TexInfo.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
39 ## (@pxref{XREFdatestr,,@code{datestr}}). If @var{date} is a string, but no |
6fd6ad758b10
doc: Use @xref, @pxref rather than "see @code{}" in TexInfo.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
40 ## format is specified, then a relatively slow search is performed through |
6fd6ad758b10
doc: Use @xref, @pxref rather than "see @code{}" in TexInfo.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
41 ## various formats. It is always preferable to specify the format string |
6fd6ad758b10
doc: Use @xref, @pxref rather than "see @code{}" in TexInfo.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
42 ## @var{f} if it is known. Formats which do not specify a particular time |
6fd6ad758b10
doc: Use @xref, @pxref rather than "see @code{}" in TexInfo.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
43 ## component will have the value set to zero. Formats which do not specify a |
6fd6ad758b10
doc: Use @xref, @pxref rather than "see @code{}" in TexInfo.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
44 ## date will default to January 1st of the current year. |
5687 | 45 ## |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
46 ## @var{p} is the year at the start of the century to which two-digit years |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
47 ## 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:
19833
diff
changeset
|
48 ## 50. |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
13848
diff
changeset
|
49 ## @seealso{datenum, datestr, clock, now, date} |
5687 | 50 ## @end deftypefn |
51 | |
52 ## Algorithm: Peter Baum (http://vsg.cape.com/~pbaum/date/date0.htm) | |
53 | |
54 ## The function __date_str2vec__ is based on datesplit by Bill Denney. | |
55 | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
56 function [y, m, d, h, mi, s] = datevec (date, f = [], p = []) |
5687 | 57 |
58 persistent std_formats nfmt; | |
59 | |
60 if (isempty (std_formats)) | |
61 std_formats = cell (); | |
62 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
|
63 ## 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
|
64 ## The '# XX' refers to the datestr numerical format code |
5687 | 65 std_formats{++nfmt} = "dd-mmm-yyyy HH:MM:SS"; # 0 |
66 std_formats{++nfmt} = "dd-mmm-yyyy"; # 1 | |
67 std_formats{++nfmt} = "mm/dd/yy"; # 2 | |
68 std_formats{++nfmt} = "mm/dd"; # 6 | |
69 std_formats{++nfmt} = "HH:MM:SS"; # 13 | |
70 std_formats{++nfmt} = "HH:MM:SS PM"; # 14 | |
71 std_formats{++nfmt} = "HH:MM"; # 15 | |
72 std_formats{++nfmt} = "HH:MM PM"; # 16 | |
73 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
|
74 |
14743
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
75 ## 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
83 ## 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
|
84 std_formats{++nfmt} = "mmm-dd-yyyy HH:MM:SS"; |
6044 | 85 std_formats{++nfmt} = "mmm-dd-yyyy"; |
86 std_formats{++nfmt} = "dd mmm yyyy HH:MM:SS"; | |
87 std_formats{++nfmt} = "dd mmm yyyy"; | |
88 std_formats{++nfmt} = "mmm dd yyyy HH:MM:SS"; | |
89 std_formats{++nfmt} = "mmm dd yyyy"; | |
90 std_formats{++nfmt} = "dd.mmm.yyyy HH:MM:SS"; | |
91 std_formats{++nfmt} = "dd.mmm.yyyy"; | |
92 std_formats{++nfmt} = "mmm.dd.yyyy HH:MM:SS"; | |
93 std_formats{++nfmt} = "mmm.dd.yyyy"; | |
5687 | 94 std_formats{++nfmt} = "mm/dd/yyyy HH:MM"; |
95 endif | |
96 | |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27984
diff
changeset
|
97 if (nargin < 1) |
6046 | 98 print_usage (); |
5687 | 99 endif |
100 | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
101 if (ischar (date)) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
102 date = cellstr (date); |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
103 endif |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
104 |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
105 if (isnumeric (f)) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
106 p = f; |
5687 | 107 f = []; |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
108 endif |
5687 | 109 |
110 if (isempty (f)) | |
111 f = -1; | |
112 endif | |
113 | |
114 if (isempty (p)) | |
11400 | 115 p = (localtime (time ())).year + 1900 - 50; |
5687 | 116 endif |
117 | |
18747
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
118 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
|
119 |
5687 | 120 if (iscell (date)) |
121 | |
122 nd = numel (date); | |
123 | |
124 y = m = d = h = mi = s = zeros (nd, 1); | |
125 | |
126 if (f == -1) | |
127 for k = 1:nd | |
128 found = false; | |
129 for l = 1:nfmt | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
130 [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:
19697
diff
changeset
|
131 __date_vfmt2sfmt__ (std_formats{l}); |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
132 [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:
19697
diff
changeset
|
133 __date_str2vec__ (date{k}, p, f, rY, ry, fy, fm, fd, fh, fmi, fs); |
5687 | 134 if (found) |
135 break; | |
136 endif | |
137 endfor | |
138 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
|
139 error ("datevec: none of the standard formats match the DATE string"); |
5687 | 140 endif |
141 endfor | |
142 else | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
143 ## 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
|
144 [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f); |
5687 | 145 for k = 1:nd |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
146 [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:
19697
diff
changeset
|
147 __date_str2vec__ (date{k}, p, f, rY, ry, fy, fm, fd, fh, fmi, fs); |
5687 | 148 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
|
149 error ("datevec: DATE not parsed correctly with given format"); |
5687 | 150 endif |
151 endfor | |
152 endif | |
153 | |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
154 else # datenum input |
5687 | 155 |
18749
38925538ec14
datevec.m: Change isrow to iscolumn in cset 9eb72fa5f8b5.
Rik <rik@octave.org>
parents:
18747
diff
changeset
|
156 if (! iscolumn (date)) |
18747
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
157 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
|
158 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
|
159 endif |
5687 | 160 date = date(:); |
161 | |
162 ## Move day 0 from midnight -0001-12-31 to midnight 0000-3-1 | |
19384
7d272300a880
datevec.m: Fix negative number outputs with integer (non-float) inputs (bug #43753).
Nir Krakauer <nkrakauer@ccny.cuny.edu>
parents:
17744
diff
changeset
|
163 z = double (floor (date) - 60); |
5687 | 164 ## Calculate number of centuries; K1 = 0.25 is to avoid rounding problems. |
165 a = floor ((z - 0.25) / 36524.25); | |
166 ## Days within century; K2 = 0.25 is to avoid rounding problems. | |
167 b = z - 0.25 + a - floor (a / 4); | |
168 ## Calculate the year (year starts on March 1). | |
169 y = floor (b / 365.25); | |
170 ## Calculate day in year. | |
171 c = fix (b - floor (365.25 * y)) + 1; | |
172 ## Calculate month in year. | |
173 m = fix ((5 * c + 456) / 153); | |
174 d = c - fix ((153 * m - 457) / 5); | |
175 ## Move to Jan 1 as start of year. | |
176 ++y(m > 12); | |
177 m(m > 12) -= 12; | |
178 | |
5859 | 179 ## Convert hour-minute-seconds. Attempt to account for precision of |
180 ## datenum format. | |
181 | |
182 fracd = date - floor (date); | |
5873 | 183 tmps = abs (eps*86400*date); |
184 tmps(tmps == 0) = 1; | |
185 srnd = 2 .^ floor (- log2 (tmps)); | |
186 s = round (86400 * fracd .* srnd) ./ srnd; | |
5687 | 187 h = floor (s / 3600); |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
188 s -= 3600 * h; |
5687 | 189 mi = floor (s / 60); |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
190 s -= 60 * mi; |
5687 | 191 |
192 endif | |
193 | |
194 if (nargout <= 1) | |
5873 | 195 y = [y, m, d, h, mi, s]; |
18747
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
196 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 s = reshape (s, date_sz); |
5687 | 203 endif |
204 | |
11086
af03ff97df7b
datevec.m: use endfunction to mark end of primary function and subfunctions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
205 endfunction |
5687 | 206 |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
207 function [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f) |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30330
diff
changeset
|
208 |
30314
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
209 original_f = f; # Store for error messages. |
5687 | 210 |
8506 | 211 ## 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
|
212 f = strrep (f, "%", "%%"); |
5687 | 213 |
214 if (! isempty (strfind (f, "PM")) || ! isempty (strfind (f, "AM"))) | |
215 ampm = true; | |
216 else | |
217 ampm = false; | |
218 endif | |
219 | |
8506 | 220 ## Date part. |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
221 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
|
222 f = regexprep (f, '[Yy][Yy]', "%y"); |
5687 | 223 f = strrep (f, "mmmm", "%B"); |
224 f = strrep (f, "mmm", "%b"); | |
225 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
|
226 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
|
227 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
|
228 f = regexprep (f, '[Dd][Dd]', "%d"); |
5687 | 229 |
8506 | 230 ## Time part. |
5687 | 231 if (ampm) |
232 f = strrep (f, "HH", "%I"); | |
233 f = strrep (f, "PM", "%p"); | |
234 f = strrep (f, "AM", "%p"); | |
235 else | |
236 f = strrep (f, "HH", "%H"); | |
237 endif | |
238 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
|
239 f = regexprep (f, '[Ss][Ss]', "%S"); |
5687 | 240 |
30314
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
241 ## Check for conflicting or repeated fields. |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
242 ## Only warn, not error, if we may be confused by an original '%'s. |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
243 if (index (original_f, "%")) |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
244 err_or_warn = @warning; |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
245 else |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
246 err_or_warn = @error; |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
247 endif |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
248 |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
249 if (numel (strfind (f, "%Y")) + numel (strfind (f, "%y")) > 1) |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
250 err_or_warn ("datevec: multiple year specifiers in %s", original_f); |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
251 elseif (numel (strfind (f, "%m")) + numel (strfind (f, "%b")) |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
252 + numel (strfind (f, "%B")) > 1) |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
253 err_or_warn ("datevec: multiple month specifiers in %s", original_f); |
30645
beac32c0e9c7
datevec.m: Allow day and day of week to occur in the same format string (bug #47627).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
254 elseif (numel (strfind (f, "%d")) > 1) |
30314
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
255 err_or_warn ("datevec: multiple day specifiers in %s", original_f); |
30645
beac32c0e9c7
datevec.m: Allow day and day of week to occur in the same format string (bug #47627).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
256 elseif (numel (strfind (f, "%a"))+ numel (strfind (f, "%A")) > 1) |
beac32c0e9c7
datevec.m: Allow day and day of week to occur in the same format string (bug #47627).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
257 err_or_warn ("datevec: multiple day of week specifiers in %s", original_f); |
30314
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
258 elseif (numel (strfind (f, "%H")) + numel (strfind (f, "%I")) > 1) |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
259 err_or_warn ("datevec: multiple hour specifiers in %s", original_f); |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
260 elseif (numel (strfind (f, "%M")) > 1) |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
261 err_or_warn ("datevec: multiple minute specifiers in %s", original_f); |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
262 elseif (numel (strfind (f, "%S")) > 1) |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
263 err_or_warn ("datevec: multiple second specifiers in %s", original_f); |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
264 endif |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
265 |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
266 rY = rindex (f, "%Y"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
267 ry = rindex (f, "%y"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
268 |
8506 | 269 ## Check whether we need to give default values. |
270 ## 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
|
271 fy = rY || ry; |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
272 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
|
273 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
|
274 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
|
275 fmi = index (f, "%M"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
276 fs = index (f, "%S"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
277 |
11086
af03ff97df7b
datevec.m: use endfunction to mark end of primary function and subfunctions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
278 endfunction |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
279 |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
280 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
|
281 |
23967
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
282 ## Local time zone is irrelevant, and potentially dangerous, when using |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
283 ## strptime to simply convert a string into a broken down struct tm. |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
284 ## Set and restore TZ so time is parsed exactly as-is. See bug #36954. |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
285 TZ_orig = getenv ("TZ"); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
286 unwind_protect |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
287 setenv ("TZ", "UTC0"); |
20643
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
288 |
23967
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
289 idx = strfind (f, "FFF"); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
290 if (! isempty (idx)) |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
291 ## Kludge to handle FFF millisecond format since strptime does not. |
20715
5b7643257978
Remove trailing whitespace at end of lines.
Rik <rik@octave.org>
parents:
20677
diff
changeset
|
292 |
23967
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
293 ## Find location of FFF in ds. |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
294 ## Might not match idx because of things like yyyy -> %y. |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
295 [~, nc] = strptime (ds, f(1:idx-1)); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
296 |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
297 msec = ds(nc:min (nc+2,end)); # pull 3-digit fractional seconds. |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
298 msec_idx = find (! isdigit (msec), 1); |
20715
5b7643257978
Remove trailing whitespace at end of lines.
Rik <rik@octave.org>
parents:
20677
diff
changeset
|
299 |
23967
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
300 if (! isempty (msec_idx)) # non-digits in msec |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
301 msec = msec(1:msec_idx-1); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
302 msec(end+1:3) = "0"; # pad msec with trailing zeros |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
303 ds = [ds(1:(nc-1)), msec, ds((nc-1)+msec_idx:end)]; # zero pad ds |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
304 elseif (numel (msec) < 3) # less than three digits in msec |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
305 m_len = numel (msec); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
306 msec(end+1:3) = "0"; # pad msec with trailing zeros |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
307 ds = [ds(1:(nc-1)), msec, ds(nc+m_len:end)]; # zero pad ds as well |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
308 endif |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
309 |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
310 ## replace FFF with digits to guarantee match in strptime. |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
311 f(idx:idx+2) = msec; |
20715
5b7643257978
Remove trailing whitespace at end of lines.
Rik <rik@octave.org>
parents:
20677
diff
changeset
|
312 |
23967
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
313 if (nc > 0) |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
314 [tm, nc] = strptime (ds, f); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
315 tm.usec = 1000 * str2double (msec); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
316 endif |
20715
5b7643257978
Remove trailing whitespace at end of lines.
Rik <rik@octave.org>
parents:
20677
diff
changeset
|
317 |
23967
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
318 else |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
319 [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
|
320 endif |
23967
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
321 unwind_protect_cleanup |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
322 if (isempty (TZ_orig)) |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
323 unsetenv ("TZ"); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
324 else |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
325 setenv ("TZ", TZ_orig); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
326 endif |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
327 end_unwind_protect |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19384
diff
changeset
|
328 |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
329 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
|
330 found = true; |
5687 | 331 y = tm.year + 1900; m = tm.mon + 1; d = tm.mday; |
332 h = tm.hour; mi = tm.min; s = tm.sec + tm.usec / 1e6; | |
333 if (rY < ry) | |
334 if (y > 1999) | |
335 y -= 2000; | |
336 else | |
337 y -= 1900; | |
338 endif | |
339 y += p - mod (p, 100); | |
340 if (y < p) | |
341 y += 100; | |
342 endif | |
343 endif | |
344 if (! fy && ! fm && ! fd) | |
8506 | 345 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
|
346 ## default is January 1st of current year |
8506 | 347 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
|
348 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
|
349 d = 1; |
5687 | 350 elseif (! fy && fm && fd) |
8506 | 351 tmp = localtime (time ()); |
352 y = tmp.year + 1900; | |
5687 | 353 elseif (fy && fm && ! fd) |
354 d = 1; | |
355 endif | |
356 if (! fh && ! fmi && ! fs) | |
357 h = mi = s = 0; | |
358 elseif (fh && fmi && ! fs) | |
359 s = 0; | |
360 endif | |
361 else | |
362 y = m = d = h = mi = s = 0; | |
363 found = false; | |
364 endif | |
365 | |
11086
af03ff97df7b
datevec.m: use endfunction to mark end of primary function and subfunctions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
366 endfunction |
5687 | 367 |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
368 |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
369 %!demo |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
370 %! ## 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
|
371 %! datevec (now ()) |
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
372 |
14743
b7675598094a
datevec.m: Support more date string input formats for Matlab compatibility (bug #36563)
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
373 %!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
|
374 %! 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
|
375 ## 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
|
376 %!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
|
377 %!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
|
378 %!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
|
379 %!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
|
380 %!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
|
381 %!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
|
382 %!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
|
383 %!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
|
384 %!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
|
385 |
18747
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
386 ## Test millisecond format FFF |
20643
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
387 %!assert (datevec ("15:38:21.2", "HH:MM:SS.FFF"), [yr,1,1,15,38,21.2]) |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
388 %!assert (datevec ("15:38:21.25", "HH:MM:SS.FFF"), [yr,1,1,15,38,21.25]) |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
389 %!assert (datevec ("15:38:21.251", "HH:MM:SS.FFF"), [yr,1,1,15,38,21.251]) |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
390 |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
391 ## Test millisecond format FFF with AM/PM, and 1,2, or 3 FFF digits |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
392 %!assert (datevec ("06/01/2015 3:07:12.102 PM", "mm/dd/yyyy HH:MM:SS.FFF PM"), |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
393 %! [2015,6,1,15,7,12.102]) |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
394 %!assert (datevec ("06/01/2015 11:07:12.102 PM", "mm/dd/yyyy HH:MM:SS.FFF PM"), |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
395 %! [2015,6,1,23,7,12.102]) |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
396 %!assert (datevec ("06/01/2015 3:07:12.102 AM", "mm/dd/yyyy HH:MM:SS.FFF PM"), |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
397 %! [2015,6,1,3,7,12.102]) |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
398 %!assert (datevec ("06/01/2015 11:07:12.102 AM", "mm/dd/yyyy HH:MM:SS.FFF PM"), |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
399 %! [2015,6,1,11,7,12.102]) |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
400 %!assert (datevec ("06/01/2015 3:07:12.1 PM", "mm/dd/yyyy HH:MM:SS.FFF PM"), |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
401 %! [2015,6,1,15,7,12.1]) |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
402 %!assert (datevec ("06/01/2015 3:07:12.12 AM", "mm/dd/yyyy HH:MM:SS.FFF PM"), |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
403 %! [2015,6,1,3,7,12.12]) |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
404 %!assert (datevec ("06/01/2015 3:07:12.12 PM", "mm/dd/yyyy HH:MM:SS.FFF PM"), |
1a02e15a9f4c
datevec.m: Properly handle FFF millisecond code (bug #46171).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
20162
diff
changeset
|
405 %! [2015,6,1,15,7,12.12]) |
13848
40e32fe44aaa
Ugrade time functions to accept millisecond format string FFF (Bug #34586)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
406 |
22489
93ea313301f9
test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
407 ## Test structure of return value |
23573
1b4f4ec53b4a
use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents:
23572
diff
changeset
|
408 %!test <*42334> |
18747
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
409 %! [~, ~, 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
|
410 %! 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
|
411 |
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
412 ## Other tests |
21317
a4faec57f4c8
maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
413 %!assert (datenum (datevec ([-1e4:1e4])), [-1e4:1e4]') |
5687 | 414 %!test |
415 %! t = linspace (-2e5, 2e5, 10993); | |
5860 | 416 %! assert (all (abs (datenum (datevec (t)) - t') < 1e-5)); |
30330
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
30314
diff
changeset
|
417 %!assert (double (datevec (int64 (datenum ([2014 6 1])))), |
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
30314
diff
changeset
|
418 %! datevec (datenum ([2014 6 1]))) |
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
30314
diff
changeset
|
419 %!assert (double (datevec (int64 (datenum ([2014 6 18])))), |
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
30314
diff
changeset
|
420 %! 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
|
421 |
23967
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
422 ## Test parsing of date strings that fall within daylight saving transition |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
423 %!testif ; isunix () <*36954> |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
424 %! zones = { "UTC0" ... |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
425 %! "EST+5EDT,M3.2.0/2,M11.1.0/2" ... America/New_York |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
426 %! "CET-1CEST,M3.5.0/2,M10.5.0/2" ... Europe/Berlin |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
427 %! "CLT+4CLST,M8.2.0/0,M5.2.0/0" ... America/Santiago |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
428 %! "LHST-10:30LHDT-11,M10.1.0/2,M4.1.0/2" ... Australia/Lord_Howe |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
429 %! ":America/Caracas" ... |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
430 %! }; |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
431 %! TZ_orig = getenv ("TZ"); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
432 %! unwind_protect |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
433 %! for i = 1:numel (zones) |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
434 %! setenv ("TZ", zones{i}); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
435 %! ## These specific times were chosen to test conversion during the loss |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
436 %! ## of some amount of local time at the start of daylight saving time in |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
437 %! ## each of the zones listed above. We test all in each time zone to be |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
438 %! ## exhaustive, even though each is problematic for only one of the zones. |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
439 %! assert (datevec ("2017-03-12 02:15:00"), [2017 3 12 2 15 0]); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
440 %! assert (datevec ("2017-03-26 02:15:00"), [2017 3 26 2 15 0]); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
441 %! assert (datevec ("2017-08-13 00:15:00"), [2017 8 13 0 15 0]); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
442 %! assert (datevec ("2017-10-01 02:15:00"), [2017 10 1 2 15 0]); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
443 %! ## This tests a one-time loss of 30 minutes in Venezuela's local time |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
444 %! assert (datevec ("2016-05-01 02:40:00"), [2016 5 1 2 40 0]); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
445 %! endfor |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
446 %! unwind_protect_cleanup |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
447 %! if (isempty (TZ_orig)) |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
448 %! unsetenv ("TZ"); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
449 %! else |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
450 %! setenv ("TZ", TZ_orig); |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
451 %! endif |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
452 %! end_unwind_protect |
df726a12c757
datevec: fix parsing of date strings during daylight saving transition (bug #36954)
Mike Miller <mtmiller@octave.org>
parents:
23573
diff
changeset
|
453 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
454 ## Test input validation |
28896
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
455 %!error <Invalid call> datevec () |
18747
9eb72fa5f8b5
datevec.m: Return value with same shape as input (bug #42334).
Sergey Plotnikov <seryozha.plotnikov@gmail.com>
parents:
17744
diff
changeset
|
456 %!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
|
457 %!error <DATE not parsed correctly with given format> datevec ("foobar", "%d") |
30314
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
458 %!error <multiple year specifiers> datevec ("1/2/30", "mm/yy/yy") |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
459 %!error <multiple month specifiers> datevec ("1/2/30", "mm/mm/yy") |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
460 %!error <multiple day specifiers> datevec ("1/2/30", "mm/dd/dd") |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
461 %!error <multiple hour specifiers> datevec ("15:38:21.251", "HH:HH:SS") |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
462 %!error <multiple minute specifiers> datevec ("15:38:21.251", "MM:MM:SS") |
3e09b065779d
datevec.m: Check that no field (HH, MM etc) is repeated in format (bug #47627).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
29359
diff
changeset
|
463 %!error <multiple second specifiers> datevec ("15:38:21.251", "HH:SS:SS") |