1 ## Copyright (C) 2000-2012 Paul Kienzle
3 ## This file is part of Octave.
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
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.
28 ## A date vector is a row vector with six members, representing the year,
29 ## month, day, hour, minute, and seconds respectively.
31 ## @var{f} is the format string used to interpret date strings
32 ## (see @code{datestr}).
34 ## @var{p} is the year at the start of the century to which two-digit years
35 ## will be referenced. If not specified, it defaults to the current year
37 ## @seealso{datenum, datestr, clock, now, date}
40 ## Algorithm: Peter Baum (http://vsg.cape.com/~pbaum/date/date0.htm)
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>
47 ## The function __date_str2vec__ is based on datesplit by Bill Denney.
49 function [y, m, d, h, mi, s] = datevec (date, f = [], p = [])
51 persistent std_formats nfmt;
53 if (isempty (std_formats))
54 std_formats = cell ();
56 ## These formats are specified by Matlab to be parsed
57 ## The '# XX' refers to the datestr numerical format code
58 std_formats{++nfmt} = "dd-mmm-yyyy HH:MM:SS"; # 0
59 std_formats{++nfmt} = "dd-mmm-yyyy"; # 1
60 std_formats{++nfmt} = "mm/dd/yy"; # 2
61 std_formats{++nfmt} = "mm/dd"; # 6
62 std_formats{++nfmt} = "HH:MM:SS"; # 13
63 std_formats{++nfmt} = "HH:MM:SS PM"; # 14
64 std_formats{++nfmt} = "HH:MM"; # 15
65 std_formats{++nfmt} = "HH:MM PM"; # 16
66 std_formats{++nfmt} = "mm/dd/yyyy"; # 23
68 ## These are other formats that Octave tries
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 std_formats{++nfmt} = "dd.mmm.yyyy HH:MM:SS";
76 std_formats{++nfmt} = "dd.mmm.yyyy";
77 std_formats{++nfmt} = "mmm.dd.yyyy HH:MM:SS";
78 std_formats{++nfmt} = "mmm.dd.yyyy";
79 std_formats{++nfmt} = "mmmyy"; # 12
80 std_formats{++nfmt} = "mm/dd/yyyy HH:MM";
83 if (nargin < 1 || nargin > 3)
88 date = cellstr (date);
101 p = (localtime (time ())).year + 1900 - 50;
108 y = m = d = h = mi = s = zeros (nd, 1);
114 [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (std_formats{l});
115 [found y(k) m(k) d(k) h(k) mi(k) s(k)] = __date_str2vec__ (date{k}, p, f, rY, ry, fy, fm, fd, fh, fmi, fs);
121 error ("datevec: none of the standard formats match the DATE string");
125 ## Decipher the format string just once for speed.
126 [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f);
128 [found y(k) m(k) d(k) h(k) mi(k) s(k)] = __date_str2vec__ (date{k}, p, f, rY, ry, fy, fm, fd, fh, fmi, fs);
130 error ("datevec: DATE not parsed correctly with given format");
139 ## Move day 0 from midnight -0001-12-31 to midnight 0000-3-1
140 z = floor (date) - 60;
141 ## Calculate number of centuries; K1 = 0.25 is to avoid rounding problems.
142 a = floor ((z - 0.25) / 36524.25);
143 ## Days within century; K2 = 0.25 is to avoid rounding problems.
144 b = z - 0.25 + a - floor (a / 4);
145 ## Calculate the year (year starts on March 1).
146 y = floor (b / 365.25);
147 ## Calculate day in year.
148 c = fix (b - floor (365.25 * y)) + 1;
149 ## Calculate month in year.
150 m = fix ((5 * c + 456) / 153);
151 d = c - fix ((153 * m - 457) / 5);
152 ## Move to Jan 1 as start of year.
156 ## Convert hour-minute-seconds. Attempt to account for precision of
159 fracd = date - floor (date);
160 tmps = abs (eps*86400*date);
162 srnd = 2 .^ floor (- log2 (tmps));
163 s = round (86400 * fracd .* srnd) ./ srnd;
164 h = floor (s / 3600);
172 y = [y, m, d, h, mi, s];
177 function [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f)
179 ## Play safe with percent signs.
180 f = strrep (f, "%", "%%");
182 if (! isempty (strfind (f, "PM")) || ! isempty (strfind (f, "AM")))
189 f = regexprep (f, '[Yy][Yy][Yy][Yy]', "%Y");
190 f = regexprep (f, '[Yy][Yy]', "%y");
191 f = strrep (f, "mmmm", "%B");
192 f = strrep (f, "mmm", "%b");
193 f = strrep (f, "mm", "%m");
194 f = regexprep (f, '[Dd][Dd][Dd][Dd]', "%A");
195 f = regexprep (f, '[Dd][Dd][Dd]', "%a");
196 f = regexprep (f, '[Dd][Dd]', "%d");
200 f = strrep (f, "HH", "%I");
201 f = strrep (f, "PM", "%p");
202 f = strrep (f, "AM", "%p");
204 f = strrep (f, "HH", "%H");
206 f = strrep (f, "MM", "%M");
207 f = regexprep (f, '[Ss][Ss]', "%S");
209 rY = rindex (f, "%Y");
210 ry = rindex (f, "%y");
212 ## Check whether we need to give default values.
213 ## Possible error when string contains "%%".
215 fm = index (f, "%m") || index (f, "%b") || index (f, "%B");
216 fd = index (f, "%d") || index (f, "%a") || index (f, "%A");
217 fh = index (f, "%H") || index (f, "%I");
218 fmi = index (f, "%M");
219 fs = index (f, "%S");
223 function [found, y, m, d, h, mi, s] = __date_str2vec__ (ds, p, f, rY, ry, fy, fm, fd, fh, fmi, fs)
225 idx = strfind (f, "FFF");
227 ## Kludge to handle FFF millisecond format since strptime does not
229 [~, nc] = strptime (ds, f);
231 msec = ds(nc:min(nc+2, end));
232 f = [f(1:idx-1) msec f(idx:end)];
233 [tm, nc] = strptime (ds, f);
234 tm.usec = 1000 * str2double (msec);
237 [tm, nc] = strptime (ds, f);
240 if (nc == columns (ds) + 1)
242 y = tm.year + 1900; m = tm.mon + 1; d = tm.mday;
243 h = tm.hour; mi = tm.min; s = tm.sec + tm.usec / 1e6;
250 y += p - mod (p, 100);
255 if (! fy && ! fm && ! fd)
256 tmp = localtime (time ());
260 elseif (! fy && fm && fd)
261 tmp = localtime (time ());
263 elseif (fy && fm && ! fd)
266 if (! fh && ! fmi && ! fs)
268 elseif (fh && fmi && ! fs)
272 y = m = d = h = mi = s = 0;
280 %! ## Current date and time
284 %! nowvec = datevec (now); # Some tests could fail around midnight!
285 %!# tests for standard formats: 0, 1, 2, 6, 13, 14, 15, 16, 23
286 %!assert (datevec ("07-Sep-2000 15:38:09"), [2000,9,7,15,38,9])
287 %!assert (datevec ("07-Sep-2000"), [2000,9,7,0,0,0])
288 %!assert (datevec ("09/07/00"), [2000,9,7,0,0,0])
289 %!assert (datevec ("09/13"), [nowvec(1),9,13,0,0,0])
290 %!assert (datevec ("15:38:09"), [nowvec(1:3),15,38,9])
291 %!assert (datevec ("3:38:09 PM"), [nowvec(1:3),15,38,9])
292 %!assert (datevec ("15:38"), [nowvec(1:3),15,38,0])
293 %!assert (datevec ("03:38 PM"), [nowvec(1:3),15,38,0])
294 %!assert (datevec ("03/13/1962"), [1962,3,13,0,0,0])
296 %% Test millisecond format FFF
297 %!assert (datevec ("15:38:21.25", "HH:MM:SS.FFF"), [nowvec(1:3),15,38,21.025])
300 %!assert (datenum (datevec ([-1e4:1e4])), [-1e4:1e4]')
302 %! t = linspace (-2e5, 2e5, 10993);
303 %! assert (all (abs (datenum (datevec (t)) - t') < 1e-5));