Mercurial > hg > octave-nkf
annotate scripts/time/datevec.m @ 8411:69d45a4c7d94
avoid repeated parsing of format string in datevec
author | Daniel J Sebald <daniel.sebald@ieee.org> |
---|---|
date | Thu, 18 Dec 2008 20:22:59 +0100 |
parents | 0064f7d6a451 |
children | bc982528de11 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007 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 -*- | |
20 ## @deftypefn {Function File} {@var{v} =} datevec (@var{date}) | |
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 | |
35 ## are to be interpreted in. If not specified, it defaults to the current | |
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 | |
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 | |
185 ### endfunction | |
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 |
189 # Play safe with percent signs | |
190 f = strrep(f, "%", "%%"); | |
191 | |
192 ## dates to lowercase (note: we cannot convert MM to mm) | |
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"); | |
201 ## times to uppercase (also cannot convert mm to MM) | |
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 | |
207 ## right now, the format string may only contain these tokens: | |
208 ## yyyy 4 digit year | |
209 ## yy 2 digit year | |
210 ## mmmm month name, full | |
211 ## mmm month name, abbreviated | |
212 ## mm month number | |
213 ## dddd weekday name, full | |
214 ## ddd weekday name, abbreviated | |
215 ## dd date | |
216 ## HH hour | |
217 ## MM minutes | |
218 ## SS seconds | |
219 ## PM AM/PM | |
220 ## AM AM/PM | |
221 | |
222 if (! isempty (strfind (f, "PM")) || ! isempty (strfind (f, "AM"))) | |
223 ampm = true; | |
224 else | |
225 ampm = false; | |
226 endif | |
227 | |
228 # date part | |
229 f = strrep (f, "yyyy", "%Y"); | |
230 f = strrep (f, "yy", "%y"); | |
231 f = strrep (f, "mmmm", "%B"); | |
232 f = strrep (f, "mmm", "%b"); | |
233 f = strrep (f, "mm", "%m"); | |
234 f = strrep (f, "dddd", "%A"); | |
235 f = strrep (f, "ddd", "%a"); | |
236 f = strrep (f, "dd", "%d"); | |
237 | |
238 # time part | |
239 if (ampm) | |
240 f = strrep (f, "HH", "%I"); | |
241 f = strrep (f, "PM", "%p"); | |
242 f = strrep (f, "AM", "%p"); | |
243 else | |
244 f = strrep (f, "HH", "%H"); | |
245 endif | |
246 f = strrep (f, "MM", "%M"); | |
247 f = strrep (f, "SS", "%S"); | |
248 | |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
249 rY = rindex (f, "%Y"); |
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 |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
252 # check whether we need to give default values |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
253 # possible error when string contains "%%" |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
254 fy = rY || ry; |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
255 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
|
256 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
|
257 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
|
258 fmi = index (f, "%M"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
259 fs = index (f, "%S"); |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
260 |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
261 ### endfunction |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
262 |
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
263 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
|
264 |
5687 | 265 [tm, nc] = strptime (ds, f); |
266 | |
8411
69d45a4c7d94
avoid repeated parsing of format string in datevec
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7044
diff
changeset
|
267 if (nc == size (ds, 2) + 1) |
5687 | 268 y = tm.year + 1900; m = tm.mon + 1; d = tm.mday; |
269 h = tm.hour; mi = tm.min; s = tm.sec + tm.usec / 1e6; | |
270 found = true; | |
271 if (rY < ry) | |
272 if (y > 1999) | |
273 y -= 2000; | |
274 else | |
275 y -= 1900; | |
276 endif | |
277 y += p - mod (p, 100); | |
278 if (y < p) | |
279 y += 100; | |
280 endif | |
281 endif | |
282 if (! fy && ! fm && ! fd) | |
283 tvm = localtime (time ()); ## tvm: this very moment | |
284 y = tvm.year + 1900; | |
285 m = tvm.mon + 1; | |
286 d = tvm.mday; | |
287 elseif (! fy && fm && fd) | |
288 tvm = localtime (time ()); ## tvm: this very moment | |
289 y = tvm.year + 1900; | |
290 elseif (fy && fm && ! fd) | |
291 tvm = localtime (time ()); ## tvm: this very moment | |
292 d = 1; | |
293 endif | |
294 if (! fh && ! fmi && ! fs) | |
295 h = mi = s = 0; | |
296 elseif (fh && fmi && ! fs) | |
297 s = 0; | |
298 endif | |
299 else | |
300 y = m = d = h = mi = s = 0; | |
301 found = false; | |
302 endif | |
303 | |
304 ### endfunction | |
305 | |
306 %!shared nowvec | |
307 %! nowvec = datevec (now); # Some tests could fail around midnight! | |
308 # tests for standard formats: 0, 1, 2, 6, 13, 14, 15, 16, 23 | |
309 %!assert(datevec("07-Sep-2000 15:38:09"),[2000,9,7,15,38,9]); | |
310 %!assert(datevec("07-Sep-2000"),[2000,9,7,0,0,0]); | |
311 %!assert(datevec("09/07/00"),[2000,9,7,0,0,0]); | |
312 %!assert(datevec("09/13"),[nowvec(1),9,13,0,0,0]); | |
313 %!assert(datevec("15:38:09"),[nowvec(1:3),15,38,9]); | |
314 %!assert(datevec("3:38:09 PM"),[nowvec(1:3),15,38,9]); | |
315 %!assert(datevec("15:38"),[nowvec(1:3),15,38,0]); | |
316 %!assert(datevec("03:38 PM"),[nowvec(1:3),15,38,0]); | |
317 %!assert(datevec("03/13/1962"),[1962,3,13,0,0,0]); | |
318 # other tests | |
319 %!assert(all(datenum(datevec([-1e4:1e4]))==[-1e4:1e4]')) | |
320 %!test | |
321 %! t = linspace (-2e5, 2e5, 10993); | |
5860 | 322 %! assert (all (abs (datenum (datevec (t)) - t') < 1e-5)); |
5687 | 323 # demos |
324 %!demo | |
325 %! datevec (now ()) |