annotate scripts/time/datevec.m @ 5859:ba0bea22167b

[project @ 2006-06-14 18:18:51 by jwe]
author jwe
date Wed, 14 Jun 2006 18:18:51 +0000
parents a2902024bc4e
children b645066d40ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5687
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 2000, 2001, 2004, 2005 Paul Kienzle
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
2 ##
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
4 ##
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
7 ## the Free Software Foundation; either version 2, or (at your option)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
8 ## any later version.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
9 ##
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
14 ##
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, write to the Free
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
18 ## 02110-1301, USA.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
19
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefn {Function File} {@var{v} =} datevec (@var{date})
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
22 ## @deftypefnx {Function File} {@var{v} =} datevec (@var{date}, @var{f})
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
23 ## @deftypefnx {Function File} {@var{v} =} datevec (@var{date}, @var{p})
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
24 ## @deftypefnx {Function File} {@var{v} =} datevec (@var{date}, @var{f}, @var{p})
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
25 ## @deftypefnx {Function File} {[@var{y}, @var{m}, @var{d}, @var{h}, @var{mi}, @var{s}] =} datevec (@dots{})
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
26 ## Convert a serial date number (see @code{datenum}) or date string (see
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
27 ## @code{datestr}) into a date vector.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
28 ##
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
29 ## A date vector is a row vector with six members, representing the year,
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
30 ## month, day, hour, minute, and seconds respectively.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
31 ##
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
32 ## @var{f} is the format string used to interpret date strings
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
33 ## (see @code{datestr}).
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
34 ##
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
35 ## @var{p} is the year at the start of the century in which two-digit years
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
36 ## are to be interpreted in. If not specified, it defaults to the current
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
37 ## year minus 50.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
38 ## @seealso{datenum, datestr, date, clock, now}
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
39 ## @end deftypefn
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
40
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
41 ## Algorithm: Peter Baum (http://vsg.cape.com/~pbaum/date/date0.htm)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
42
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
43 ## Author: pkienzle <pkienzle@users.sf.net>
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
44 ## Modified: bdenney <bill@givebillmoney.com>
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
45 ## Created: 10 October 2001 (CVS)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
46 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com>
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
47
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
48 ## The function __date_str2vec__ is based on datesplit by Bill Denney.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
49
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
50 function [y, m, d, h, mi, s] = datevec (date, varargin)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
51
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
52 persistent std_formats nfmt;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
53
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
54 if (isempty (std_formats))
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
55 std_formats = cell ();
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
56 nfmt = 0;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
57 std_formats{++nfmt} = "dd-mmm-yyyy HH:MM:SS"; # 0
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
58 std_formats{++nfmt} = "dd-mmm-yyyy"; # 1
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
59 std_formats{++nfmt} = "mm/dd/yy"; # 2
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
60 std_formats{++nfmt} = "mm/dd"; # 6
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
61 std_formats{++nfmt} = "HH:MM:SS"; # 13
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
62 std_formats{++nfmt} = "HH:MM:SS PM"; # 14
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
63 std_formats{++nfmt} = "HH:MM"; # 15
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
64 std_formats{++nfmt} = "HH:MM PM"; # 16
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
65 std_formats{++nfmt} = "mm/dd/yyyy"; # 23
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
66 # custom formats
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
67 std_formats{++nfmt} = "mmmyy"; # 12
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
68 std_formats{++nfmt} = "mm/dd/yyyy HH:MM";
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
69 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
70
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
71 if (nargin < 1 || nargin > 3)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
72 usage("v = datevec (...) or [y, m, d, h, mi, s] = datevec (...)");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
73 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
74
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
75 switch (nargin)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
76 case 1
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
77 f = [];
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
78 p = [];
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
79 case 2
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
80 if (ischar (varargin{1}))
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
81 f = varargin{1};
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
82 p = [];
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
83 else
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
84 f = [];
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
85 p = varargin{1};
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
86 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
87 case 3
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
88 f = varargin{1};
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
89 p = varargin{2};
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
90 endswitch
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
91
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
92 if (isempty (f))
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
93 f = -1;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
94 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
95
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
96 if (isempty (p))
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
97 p = (localtime (time)).year + 1900 - 50;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
98 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
99
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
100 if (ischar (date))
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
101 t = date;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
102 date = cell (1);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
103 date{1} = t;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
104 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
105
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
106 if (iscell (date))
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
107
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
108 nd = numel (date);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
109
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
110 y = m = d = h = mi = s = zeros (nd, 1);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
111
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
112 if (f == -1)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
113 for k = 1:nd
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
114 found = false;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
115 for l = 1:nfmt
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
116 [found y(k) m(k) d(k) h(k) mi(k) s(k)] = __date_str2vec__ (date{k}, std_formats{l}, p);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
117 if (found)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
118 break;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
119 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
120 endfor
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
121 if (! found)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
122 error ("datevec: none of the standard formats match the date string");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
123 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
124 endfor
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
125 else
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
126 for k = 1:nd
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
127 [found y(k) m(k) d(k) h(k) mi(k) s(k)] = __date_str2vec__ (date{k}, f, p);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
128 if (! found)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
129 error ("datevec: date not parsed correctly with given format");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
130 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
131 endfor
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
132 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
133
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
134 else
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
135
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
136 date = date(:);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
137
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
138 ## Move day 0 from midnight -0001-12-31 to midnight 0000-3-1
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
139 z = floor (date) - 60;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
140 ## Calculate number of centuries; K1 = 0.25 is to avoid rounding problems.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
141 a = floor ((z - 0.25) / 36524.25);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
142 ## Days within century; K2 = 0.25 is to avoid rounding problems.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
143 b = z - 0.25 + a - floor (a / 4);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
144 ## Calculate the year (year starts on March 1).
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
145 y = floor (b / 365.25);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
146 ## Calculate day in year.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
147 c = fix (b - floor (365.25 * y)) + 1;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
148 ## Calculate month in year.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
149 m = fix ((5 * c + 456) / 153);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
150 d = c - fix ((153 * m - 457) / 5);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
151 ## Move to Jan 1 as start of year.
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
152 ++y(m > 12);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
153 m(m > 12) -= 12;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
154
5859
ba0bea22167b [project @ 2006-06-14 18:18:51 by jwe]
jwe
parents: 5687
diff changeset
155 ## Convert hour-minute-seconds. Attempt to account for precision of
ba0bea22167b [project @ 2006-06-14 18:18:51 by jwe]
jwe
parents: 5687
diff changeset
156 ## datenum format.
ba0bea22167b [project @ 2006-06-14 18:18:51 by jwe]
jwe
parents: 5687
diff changeset
157
ba0bea22167b [project @ 2006-06-14 18:18:51 by jwe]
jwe
parents: 5687
diff changeset
158 fracd = date - floor (date);
ba0bea22167b [project @ 2006-06-14 18:18:51 by jwe]
jwe
parents: 5687
diff changeset
159 srnd = 10 .^ floor (log10 (365.25*y));
ba0bea22167b [project @ 2006-06-14 18:18:51 by jwe]
jwe
parents: 5687
diff changeset
160 s = round (86400*fracd.*srnd) ./ srnd;
5687
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
161 h = floor (s / 3600);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
162 s = s - 3600 * h;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
163 mi = floor (s / 60);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
164 s = s - 60 * mi;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
165
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
166 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
167
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
168 if (nargout <= 1)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
169 y = [y m d h mi s];
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
170 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
171
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
172 ### endfunction
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
173
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
174 function [found, y, m, d, h, mi, s] = __date_str2vec__ (ds, f, p)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
175
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
176 # Play safe with percent signs
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
177 f = strrep(f, "%", "%%");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
178
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
179 ## dates to lowercase (note: we cannot convert MM to mm)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
180 f = strrep (f, "YYYY", "yyyy");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
181 f = strrep (f, "YY", "yy");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
182 f = strrep (f, "QQ", "qq");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
183 f = strrep (f, "MMMM", "mmmm");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
184 f = strrep (f, "MMM", "mmm");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
185 f = strrep (f, "DDDD", "dddd");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
186 f = strrep (f, "DDD", "ddd");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
187 f = strrep (f, "DD", "dd");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
188 ## times to uppercase (also cannot convert mm to MM)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
189 f = strrep (f, "hh", "HH");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
190 f = strrep (f, "ss", "SS");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
191 f = strrep (f, "pm", "PM");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
192 f = strrep (f, "am", "AM");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
193
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
194 ## right now, the format string may only contain these tokens:
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
195 ## yyyy 4 digit year
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
196 ## yy 2 digit year
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
197 ## mmmm month name, full
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
198 ## mmm month name, abbreviated
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
199 ## mm month number
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
200 ## dddd weekday name, full
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
201 ## ddd weekday name, abbreviated
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
202 ## dd date
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
203 ## HH hour
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
204 ## MM minutes
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
205 ## SS seconds
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
206 ## PM AM/PM
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
207 ## AM AM/PM
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
208
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
209 if (! isempty (strfind (f, "PM")) || ! isempty (strfind (f, "AM")))
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
210 ampm = true;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
211 else
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
212 ampm = false;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
213 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
214
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
215 # date part
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
216 f = strrep (f, "yyyy", "%Y");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
217 f = strrep (f, "yy", "%y");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
218 f = strrep (f, "mmmm", "%B");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
219 f = strrep (f, "mmm", "%b");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
220 f = strrep (f, "mm", "%m");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
221 f = strrep (f, "dddd", "%A");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
222 f = strrep (f, "ddd", "%a");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
223 f = strrep (f, "dd", "%d");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
224
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
225 # time part
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
226 if (ampm)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
227 f = strrep (f, "HH", "%I");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
228 f = strrep (f, "PM", "%p");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
229 f = strrep (f, "AM", "%p");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
230 else
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
231 f = strrep (f, "HH", "%H");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
232 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
233 f = strrep (f, "MM", "%M");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
234 f = strrep (f, "SS", "%S");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
235
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
236 [tm, nc] = strptime (ds, f);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
237
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
238 if (nc == length (ds) + 1)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
239 y = tm.year + 1900; m = tm.mon + 1; d = tm.mday;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
240 h = tm.hour; mi = tm.min; s = tm.sec + tm.usec / 1e6;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
241 found = true;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
242 rY = rindex (f, "%Y");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
243 ry = rindex (f, "%y");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
244 if (rY < ry)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
245 if (y > 1999)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
246 y -= 2000;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
247 else
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
248 y -= 1900;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
249 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
250 y += p - mod (p, 100);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
251 if (y < p)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
252 y += 100;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
253 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
254 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
255 # check whether we need to give default values
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
256 # possible error when string contains "%%"
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
257 fy = rY || ry;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
258 fm = index (f, "%m") || index (f, "%b") || index (f, "%B");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
259 fd = index (f, "%d") || index (f, "%a") || index (f, "%A");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
260 fh = index (f, "%H") || index (f, "%I");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
261 fmi = index (f, "%M");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
262 fs = index (f, "%S");
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
263 if (! fy && ! fm && ! fd)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
264 tvm = localtime (time ()); ## tvm: this very moment
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
265 y = tvm.year + 1900;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
266 m = tvm.mon + 1;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
267 d = tvm.mday;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
268 elseif (! fy && fm && fd)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
269 tvm = localtime (time ()); ## tvm: this very moment
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
270 y = tvm.year + 1900;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
271 elseif (fy && fm && ! fd)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
272 tvm = localtime (time ()); ## tvm: this very moment
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
273 d = 1;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
274 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
275 if (! fh && ! fmi && ! fs)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
276 h = mi = s = 0;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
277 elseif (fh && fmi && ! fs)
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
278 s = 0;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
279 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
280 else
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
281 y = m = d = h = mi = s = 0;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
282 found = false;
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
283 endif
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
284
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
285 ### endfunction
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
286
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
287 %!shared nowvec
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
288 %! nowvec = datevec (now); # Some tests could fail around midnight!
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
289 # tests for standard formats: 0, 1, 2, 6, 13, 14, 15, 16, 23
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
290 %!assert(datevec("07-Sep-2000 15:38:09"),[2000,9,7,15,38,9]);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
291 %!assert(datevec("07-Sep-2000"),[2000,9,7,0,0,0]);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
292 %!assert(datevec("09/07/00"),[2000,9,7,0,0,0]);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
293 %!assert(datevec("09/13"),[nowvec(1),9,13,0,0,0]);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
294 %!assert(datevec("15:38:09"),[nowvec(1:3),15,38,9]);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
295 %!assert(datevec("3:38:09 PM"),[nowvec(1:3),15,38,9]);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
296 %!assert(datevec("15:38"),[nowvec(1:3),15,38,0]);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
297 %!assert(datevec("03:38 PM"),[nowvec(1:3),15,38,0]);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
298 %!assert(datevec("03/13/1962"),[1962,3,13,0,0,0]);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
299 # other tests
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
300 %!assert(all(datenum(datevec([-1e4:1e4]))==[-1e4:1e4]'))
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
301 %!test
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
302 %! t = linspace (-2e5, 2e5, 10993);
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
303 %! assert (all (datenum (datevec (t)) == t'));
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
304 # demos
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
305 %!demo
a2902024bc4e [project @ 2006-03-16 20:22:40 by jwe]
jwe
parents:
diff changeset
306 %! datevec (now ())