1## Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007, 2008, 2009 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 in which two-digit years
35## are to be interpreted in. If not specified, it defaults to the current
37## @seealso{datenum, datestr, date, clock, now}
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.
49function [y, m, d, h, mi, s] = datevec (date, varargin)
51 persistent std_formats nfmt;
53 if (isempty (std_formats))
54 std_formats = cell ();
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
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";
77 std_formats{++nfmt} = "mmmyy"; # 12
78 std_formats{++nfmt} = "mm/dd/yyyy HH:MM";
81 if (nargin < 1 || nargin > 3)
90 if (ischar (varargin{1}))
107 p = (localtime (time)).year + 1900 - 50;
111 date = cellstr (date);
118 y = m = d = h = mi = s = zeros (nd, 1);
124 [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (std_formats{l});
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);
131 error ("datevec: none of the standard formats match the date string");
135 % Decipher the format string just once for sake of speed.
136 [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f);
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);
140 error ("datevec: date not parsed correctly with given format");
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.
166 ## Convert hour-minute-seconds. Attempt to account for precision of
169 fracd = date - floor (date);
170 tmps = abs (eps*86400*date);
172 srnd = 2 .^ floor (- log2 (tmps));
173 s = round (86400 * fracd .* srnd) ./ srnd;
174 h = floor (s / 3600);
182 y = [y, m, d, h, mi, s];
187function [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f)
189 ## Play safe with percent signs.
190 f = strrep(f, "%", "%%");
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");
207 ## Right now, the format string may only contain these tokens:
211 ## mmmm month name, full
212 ## mmm month name, abbreviated
214 ## dddd weekday name, full
215 ## ddd weekday name, abbreviated
223 if (! isempty (strfind (f, "PM")) || ! isempty (strfind (f, "AM")))
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");
241 f = strrep (f, "HH", "%I");
242 f = strrep (f, "PM", "%p");
243 f = strrep (f, "AM", "%p");
245 f = strrep (f, "HH", "%H");
247 f = strrep (f, "MM", "%M");
248 f = strrep (f, "SS", "%S");
250 rY = rindex (f, "%Y");
251 ry = rindex (f, "%y");
253 ## Check whether we need to give default values.
254 ## Possible error when string contains "%%".
256 fm = index (f, "%m") || index (f, "%b") || index (f, "%B");
257 fd = index (f, "%d") || index (f, "%a") || index (f, "%A");
258 fh = index (f, "%H") || index (f, "%I");
259 fmi = index (f, "%M");
260 fs = index (f, "%S");
264function [found, y, m, d, h, mi, s] = __date_str2vec__ (ds, p, f, rY, ry, fy, fm, fd, fh, fmi, fs)
266 [tm, nc] = strptime (ds, f);
268 if (nc == size (ds, 2) + 1)
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;
278 y += p - mod (p, 100);
283 if (! fy && ! fm && ! fd)
284 tmp = localtime (time ());
288 elseif (! fy && fm && fd)
289 tmp = localtime (time ());
291 elseif (fy && fm && ! fd)
292 tmp = localtime (time ());
295 if (! fh && ! fmi && ! fs)
297 elseif (fh && fmi && ! fs)
301 y = m = d = h = mi = s = 0;
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]);
320%!assert(all(datenum(datevec([-1e4:1e4]))==[-1e4:1e4]'))
322%! t = linspace (-2e5, 2e5, 10993);
323%! assert (all (abs (datenum (datevec (t)) - t') < 1e-5));