]> Creatis software - CreaPhase.git/blob - octave_packages/m/time/datevec.m
update packages
[CreaPhase.git] / octave_packages / m / time / datevec.m
1 ## Copyright (C) 2000-2012 Paul Kienzle
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
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
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
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 to which two-digit years
35 ## will be referenced.  If not specified, it defaults to the current year
36 ## minus 50.
37 ## @seealso{datenum, datestr, clock, now, date}
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, f = [], p = [])
50
51   persistent std_formats nfmt;
52
53   if (isempty (std_formats))
54     std_formats = cell ();
55     nfmt = 0;
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
67
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";
81   endif
82
83   if (nargin < 1 || nargin > 3)
84     print_usage ();
85   endif
86
87   if (ischar (date))
88     date = cellstr (date);
89   endif
90
91   if (isnumeric (f))
92     p = f;
93     f = [];
94   endif
95
96   if (isempty (f))
97     f = -1;
98   endif
99
100   if (isempty (p))
101     p = (localtime (time ())).year + 1900 - 50;
102   endif
103
104   if (iscell (date))
105
106     nd = numel (date);
107
108     y = m = d = h = mi = s = zeros (nd, 1);
109
110     if (f == -1)
111       for k = 1:nd
112         found = false;
113         for l = 1:nfmt
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);
116           if (found)
117             break;
118           endif
119         endfor
120         if (! found)
121           error ("datevec: none of the standard formats match the DATE string");
122         endif
123       endfor
124     else
125       ## Decipher the format string just once for speed.
126       [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f);
127       for k = 1:nd
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);
129         if (! found)
130           error ("datevec: DATE not parsed correctly with given format");
131         endif
132       endfor
133     endif
134
135   else   # datenum input
136
137     date = date(:);
138
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.
153     ++y(m > 12);
154     m(m > 12) -= 12;
155
156     ## Convert hour-minute-seconds.  Attempt to account for precision of
157     ## datenum format.
158
159     fracd = date - floor (date);
160     tmps = abs (eps*86400*date);
161     tmps(tmps == 0) = 1;
162     srnd = 2 .^ floor (- log2 (tmps));
163     s = round (86400 * fracd .* srnd) ./ srnd;
164     h = floor (s / 3600);
165     s = s - 3600 * h;
166     mi = floor (s / 60);
167     s = s - 60 * mi;
168
169   endif
170
171   if (nargout <= 1)
172     y = [y, m, d, h, mi, s];
173   endif
174
175 endfunction
176
177 function [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f)
178
179   ## Play safe with percent signs.
180   f = strrep (f, "%", "%%");
181
182   if (! isempty (strfind (f, "PM")) || ! isempty (strfind (f, "AM")))
183     ampm = true;
184   else
185     ampm = false;
186   endif
187
188   ## Date part.
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");
197
198   ## Time part.
199   if (ampm)
200     f = strrep (f, "HH", "%I");
201     f = strrep (f, "PM", "%p");
202     f = strrep (f, "AM", "%p");
203   else
204     f = strrep (f, "HH", "%H");
205   endif
206   f = strrep (f, "MM", "%M");
207   f = regexprep (f, '[Ss][Ss]', "%S");
208
209   rY = rindex (f, "%Y");
210   ry = rindex (f, "%y");
211
212   ## Check whether we need to give default values.
213   ## Possible error when string contains "%%".
214   fy = rY || ry;
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");
220
221 endfunction
222
223 function [found, y, m, d, h, mi, s] = __date_str2vec__ (ds, p, f, rY, ry, fy, fm, fd, fh, fmi, fs)
224
225   idx = strfind (f, "FFF");
226   if (! isempty (idx)) 
227     ## Kludge to handle FFF millisecond format since strptime does not
228     f(idx:idx+2) = []; 
229     [~, nc] = strptime (ds, f);
230     if (nc > 0)
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);
235     endif
236   else
237     [tm, nc] = strptime (ds, f);
238   endif
239   
240   if (nc == columns (ds) + 1)
241     found = true;
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;
244     if (rY < ry)
245       if (y > 1999)
246         y -= 2000;
247       else
248         y -= 1900;
249       endif
250       y += p - mod (p, 100);
251       if (y < p)
252         y += 100;
253       endif
254     endif
255     if (! fy && ! fm && ! fd)
256       tmp = localtime (time ());
257       y = tmp.year + 1900;
258       m = tmp.mon + 1;
259       d = tmp.mday;
260     elseif (! fy && fm && fd)
261       tmp = localtime (time ());
262       y = tmp.year + 1900;
263     elseif (fy && fm && ! fd)
264       d = 1;
265     endif
266     if (! fh && ! fmi && ! fs)
267       h = mi = s = 0;
268     elseif (fh && fmi && ! fs)
269       s = 0;
270     endif
271   else
272     y = m = d = h = mi = s = 0;
273     found = false;
274   endif
275
276 endfunction
277
278
279 %!demo
280 %! ## Current date and time
281 %! datevec (now ())
282
283 %!shared nowvec
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])
295
296 %% Test millisecond format FFF
297 %!assert (datevec ("15:38:21.25", "HH:MM:SS.FFF"), [nowvec(1:3),15,38,21.025])
298
299 # Other tests
300 %!assert (datenum (datevec ([-1e4:1e4])), [-1e4:1e4]')
301 %!test
302 %! t = linspace (-2e5, 2e5, 10993);
303 %! assert (all (abs (datenum (datevec (t)) - t') < 1e-5));
304