Mercurial > hg > octave-nkf
annotate scripts/time/datevec.m @ 11086:af03ff97df7b
datevec.m: use endfunction to mark end of primary function and subfunctions
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 07 Oct 2010 17:47:07 -0400 |
parents | be55736a0783 |
children | 277d891afae2 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007, 2008, 2009 Paul Kienzle |
5687 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5687 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5687 | 18 |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
20 ## @deftypefn {Function File} {@var{v} =} datevec (@var{date}) |
5687 | 21 ## @deftypefnx {Function File} {@var{v} =} datevec (@var{date}, @var{f}) |
22 ## @deftypefnx {Function File} {@var{v} =} datevec (@var{date}, @var{p}) | |
23 ## @deftypefnx {Function File} {@var{v} =} datevec (@var{date}, @var{f}, @var{p}) | |
24 ## @deftypefnx {Function File} {[@var{y}, @var{m}, @var{d}, @var{h}, @var{mi}, @var{s}] =} datevec (@dots{}) | |
25 ## Convert a serial date number (see @code{datenum}) or date string (see | |
26 ## @code{datestr}) into a date vector. | |
27 ## | |
28 ## A date vector is a row vector with six members, representing the year, | |
29 ## month, day, hour, minute, and seconds respectively. | |
30 ## | |
31 ## @var{f} is the format string used to interpret date strings | |
32 ## (see @code{datestr}). | |
33 ## | |
34 ## @var{p} is the year at the start of the century in which two-digit years | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
35 ## are to be interpreted in. If not specified, it defaults to the current |
5687 | 36 ## year minus 50. |
37 ## @seealso{datenum, datestr, date, clock, now} | |
38 ## @end deftypefn | |
39 | |
40 ## Algorithm: Peter Baum (http://vsg.cape.com/~pbaum/date/date0.htm) | |
41 | |
42 ## Author: pkienzle <pkienzle@users.sf.net> | |
43 ## Modified: bdenney <bill@givebillmoney.com> | |
44 ## Created: 10 October 2001 (CVS) | |
45 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com> | |
46 | |
47 ## The function __date_str2vec__ is based on datesplit by Bill Denney. | |
48 | |
49 function [y, m, d, h, mi, s] = datevec (date, varargin) | |
50 | |
51 persistent std_formats nfmt; | |
52 | |
53 if (isempty (std_formats)) | |
54 std_formats = cell (); | |
55 nfmt = 0; | |
56 std_formats{++nfmt} = "dd-mmm-yyyy HH:MM:SS"; # 0 | |
57 std_formats{++nfmt} = "dd-mmm-yyyy"; # 1 | |
58 std_formats{++nfmt} = "mm/dd/yy"; # 2 | |
59 std_formats{++nfmt} = "mm/dd"; # 6 | |
60 std_formats{++nfmt} = "HH:MM:SS"; # 13 | |
61 std_formats{++nfmt} = "HH:MM:SS PM"; # 14 | |
62 std_formats{++nfmt} = "HH:MM"; # 15 | |
63 std_formats{++nfmt} = "HH:MM PM"; # 16 | |
64 std_formats{++nfmt} = "mm/dd/yyyy"; # 23 | |
6044 | 65 std_formats{++nfmt} = "mmm-dd-yyyy HH:MM:SS"; |
66 std_formats{++nfmt} = "mmm-dd-yyyy"; | |
67 std_formats{++nfmt} = "dd mmm yyyy HH:MM:SS"; | |
68 std_formats{++nfmt} = "dd mmm yyyy"; | |
69 std_formats{++nfmt} = "mmm dd yyyy HH:MM:SS"; | |
70 std_formats{++nfmt} = "mmm dd yyyy"; | |
71 std_formats{++nfmt} = "dd.mmm.yyyy HH:MM:SS"; | |
72 std_formats{++nfmt} = "dd.mmm.yyyy"; | |
73 std_formats{++nfmt} = "mmm.dd.yyyy HH:MM:SS"; | |
74 std_formats{++nfmt} = "mmm.dd.yyyy"; | |
75 | |
8506 | 76 ## Custom formats. |
5687 | 77 std_formats{++nfmt} = "mmmyy"; # 12 |
78 std_formats{++nfmt} = "mm/dd/yyyy HH:MM"; | |
79 endif | |
80 | |
81 if (nargin < 1 || nargin > 3) | |
6046 | 82 print_usage (); |
5687 | 83 endif |
84 | |
85 switch (nargin) | |
86 case 1 | |
87 f = []; | |
88 p = []; | |
89 case 2 | |
90 if (ischar (varargin{1})) | |
91 f = varargin{1}; | |
92 p = []; | |
93 else | |
94 f = []; | |
95 p = varargin{1}; | |
96 endif | |
97 case 3 | |
98 f = varargin{1}; | |
99 p = varargin{2}; | |
100 endswitch | |
101 | |
102 if (isempty (f)) | |
103 f = -1; | |
104 endif | |
105 | |
106 if (isempty (p)) | |
107 p = (localtime (time)).year + 1900 - 50; | |
108 endif | |
109 | |
110 if (ischar (date)) | |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
111 date = cellstr (date); |
5687 | 112 endif |
113 | |
114 if (iscell (date)) | |
115 | |
116 nd = numel (date); | |
117 | |
118 y = m = d = h = mi = s = zeros (nd, 1); | |
119 | |
120 if (f == -1) | |
121 for k = 1:nd | |
122 found = false; | |
123 for l = 1:nfmt | |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
124 [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (std_formats{l}); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
125 [found y(k) m(k) d(k) h(k) mi(k) s(k)] = __date_str2vec__ (date{k}, p, f, rY, ry, fy, fm, fd, fh, fmi, fs); |
5687 | 126 if (found) |
127 break; | |
128 endif | |
129 endfor | |
130 if (! found) | |
131 error ("datevec: none of the standard formats match the date string"); | |
132 endif | |
133 endfor | |
134 else | |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
135 % Decipher the format string just once for sake of speed. |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
136 [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f); |
5687 | 137 for k = 1:nd |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
138 [found y(k) m(k) d(k) h(k) mi(k) s(k)] = __date_str2vec__ (date{k}, p, f, rY, ry, fy, fm, fd, fh, fmi, fs); |
5687 | 139 if (! found) |
140 error ("datevec: date not parsed correctly with given format"); | |
141 endif | |
142 endfor | |
143 endif | |
144 | |
145 else | |
146 | |
147 date = date(:); | |
148 | |
149 ## Move day 0 from midnight -0001-12-31 to midnight 0000-3-1 | |
150 z = floor (date) - 60; | |
151 ## Calculate number of centuries; K1 = 0.25 is to avoid rounding problems. | |
152 a = floor ((z - 0.25) / 36524.25); | |
153 ## Days within century; K2 = 0.25 is to avoid rounding problems. | |
154 b = z - 0.25 + a - floor (a / 4); | |
155 ## Calculate the year (year starts on March 1). | |
156 y = floor (b / 365.25); | |
157 ## Calculate day in year. | |
158 c = fix (b - floor (365.25 * y)) + 1; | |
159 ## Calculate month in year. | |
160 m = fix ((5 * c + 456) / 153); | |
161 d = c - fix ((153 * m - 457) / 5); | |
162 ## Move to Jan 1 as start of year. | |
163 ++y(m > 12); | |
164 m(m > 12) -= 12; | |
165 | |
5859 | 166 ## Convert hour-minute-seconds. Attempt to account for precision of |
167 ## datenum format. | |
168 | |
169 fracd = date - floor (date); | |
5873 | 170 tmps = abs (eps*86400*date); |
171 tmps(tmps == 0) = 1; | |
172 srnd = 2 .^ floor (- log2 (tmps)); | |
173 s = round (86400 * fracd .* srnd) ./ srnd; | |
5687 | 174 h = floor (s / 3600); |
175 s = s - 3600 * h; | |
176 mi = floor (s / 60); | |
177 s = s - 60 * mi; | |
178 | |
179 endif | |
180 | |
181 if (nargout <= 1) | |
5873 | 182 y = [y, m, d, h, mi, s]; |
5687 | 183 endif |
184 | |
11086
af03ff97df7b
datevec.m: use endfunction to mark end of primary function and subfunctions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
185 endfunction |
5687 | 186 |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
187 function [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f) |
5687 | 188 |
8506 | 189 ## Play safe with percent signs. |
5687 | 190 f = strrep(f, "%", "%%"); |
191 | |
8506 | 192 ## Dates to lowercase (note: we cannot convert MM to mm). |
5687 | 193 f = strrep (f, "YYYY", "yyyy"); |
194 f = strrep (f, "YY", "yy"); | |
195 f = strrep (f, "QQ", "qq"); | |
196 f = strrep (f, "MMMM", "mmmm"); | |
197 f = strrep (f, "MMM", "mmm"); | |
198 f = strrep (f, "DDDD", "dddd"); | |
199 f = strrep (f, "DDD", "ddd"); | |
200 f = strrep (f, "DD", "dd"); | |
8506 | 201 ## Times to uppercase (also cannot convert mm to MM). |
5687 | 202 f = strrep (f, "hh", "HH"); |
203 f = strrep (f, "ss", "SS"); | |
204 f = strrep (f, "pm", "PM"); | |
205 f = strrep (f, "am", "AM"); | |
206 | |
8506 | 207 ## Right now, the format string may only contain these tokens: |
208 ## | |
5687 | 209 ## yyyy 4 digit year |
210 ## yy 2 digit year | |
211 ## mmmm month name, full | |
212 ## mmm month name, abbreviated | |
213 ## mm month number | |
214 ## dddd weekday name, full | |
215 ## ddd weekday name, abbreviated | |
216 ## dd date | |
217 ## HH hour | |
218 ## MM minutes | |
219 ## SS seconds | |
220 ## PM AM/PM | |
221 ## AM AM/PM | |
222 | |
223 if (! isempty (strfind (f, "PM")) || ! isempty (strfind (f, "AM"))) | |
224 ampm = true; | |
225 else | |
226 ampm = false; | |
227 endif | |
228 | |
8506 | 229 ## Date part. |
5687 | 230 f = strrep (f, "yyyy", "%Y"); |
231 f = strrep (f, "yy", "%y"); | |
232 f = strrep (f, "mmmm", "%B"); | |
233 f = strrep (f, "mmm", "%b"); | |
234 f = strrep (f, "mm", "%m"); | |
235 f = strrep (f, "dddd", "%A"); | |
236 f = strrep (f, "ddd", "%a"); | |
237 f = strrep (f, "dd", "%d"); | |
238 | |
8506 | 239 ## Time part. |
5687 | 240 if (ampm) |
241 f = strrep (f, "HH", "%I"); | |
242 f = strrep (f, "PM", "%p"); | |
243 f = strrep (f, "AM", "%p"); | |
244 else | |
245 f = strrep (f, "HH", "%H"); | |
246 endif | |
247 f = strrep (f, "MM", "%M"); | |
248 f = strrep (f, "SS", "%S"); | |
249 | |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
250 rY = rindex (f, "%Y"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
251 ry = rindex (f, "%y"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
252 |
8506 | 253 ## Check whether we need to give default values. |
254 ## 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
|
255 fy = rY || ry; |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
256 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
|
257 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
|
258 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
|
259 fmi = index (f, "%M"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
260 fs = index (f, "%S"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
261 |
11086
af03ff97df7b
datevec.m: use endfunction to mark end of primary function and subfunctions
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
262 endfunction |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
263 |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
264 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
|
265 |
5687 | 266 [tm, nc] = strptime (ds, f); |
267 | |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
268 if (nc == size (ds, 2) + 1) |
5687 | 269 y = tm.year + 1900; m = tm.mon + 1; d = tm.mday; |
270 h = tm.hour; mi = tm.min; s = tm.sec + tm.usec / 1e6; | |
271 found = true; | |
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 ()); |
285 y = tmp.year + 1900; | |
286 m = tmp.mon + 1; | |
287 d = tmp.mday; | |
5687 | 288 elseif (! fy && fm && fd) |
8506 | 289 tmp = localtime (time ()); |
290 y = tmp.year + 1900; | |
5687 | 291 elseif (fy && fm && ! fd) |
8506 | 292 tmp = localtime (time ()); |
5687 | 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 |
307 %!shared nowvec | |
308 %! nowvec = datevec (now); # Some tests could fail around midnight! | |
309 # tests for standard formats: 0, 1, 2, 6, 13, 14, 15, 16, 23 | |
310 %!assert(datevec("07-Sep-2000 15:38:09"),[2000,9,7,15,38,9]); | |
311 %!assert(datevec("07-Sep-2000"),[2000,9,7,0,0,0]); | |
312 %!assert(datevec("09/07/00"),[2000,9,7,0,0,0]); | |
313 %!assert(datevec("09/13"),[nowvec(1),9,13,0,0,0]); | |
314 %!assert(datevec("15:38:09"),[nowvec(1:3),15,38,9]); | |
315 %!assert(datevec("3:38:09 PM"),[nowvec(1:3),15,38,9]); | |
316 %!assert(datevec("15:38"),[nowvec(1:3),15,38,0]); | |
317 %!assert(datevec("03:38 PM"),[nowvec(1:3),15,38,0]); | |
318 %!assert(datevec("03/13/1962"),[1962,3,13,0,0,0]); | |
319 # other tests | |
320 %!assert(all(datenum(datevec([-1e4:1e4]))==[-1e4:1e4]')) | |
321 %!test | |
322 %! t = linspace (-2e5, 2e5, 10993); | |
5860 | 323 %! assert (all (abs (datenum (datevec (t)) - t') < 1e-5)); |
5687 | 324 # demos |
325 %!demo | |
326 %! datevec (now ()) |