comparison main/time/datenum.m @ 0:6b33357c7561 octave-forge

Initial revision
author pkienzle
date Wed, 10 Oct 2001 19:54:49 +0000
parents
children 8adaa495124c
comparison
equal deleted inserted replaced
-1:000000000000 0:6b33357c7561
1 ## Copyright (C) 2000 Paul Kienzle
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {} datenum(Y, M, D [, h , m [, s]])
19 ## @deftypefnx {Function File} {} datenum('date' [, P])
20 ## Returns the specified local time as a number of days since Jan 1, 0000.
21 ## By this reckoning, Jan 1, 1970 is day number 719529. The fractional
22 ## portion, corresponds to the portion of the specified day.
23 ##
24 ## Note: 32-bit architectures only handle times between Dec 14, 1901
25 ## and Jan 19, 2038, with special handling for 0000-01-01. datenum
26 ## returns -1 in case of a range error.
27 ##
28 ## @seealso{date,clock,now,datestr,datevec,calendar,weekday}
29 ## @end deftypefn
30
31 ## 2001-08-30 Paul Kienzle <pkienzle@users.sf.net>
32 ## * make it independent of time zone
33
34 function n = datenum(Y,M,D,h,m,s)
35 if nargin == 0 || (nargin > 2 && isstr(Y)) || nargin > 6
36 usage("n=datenum('date' [, P]) or n=datenum(Y, M, D [, h, m [, s]])");
37 endif
38 if isstr(Y)
39 if nargin < 2, M=[]; endif
40 [Y,M,D,h,m,s] = datevec(Y,M);
41 else
42 if nargin < 6, s = zeros(size(Y)); endif
43 if nargin < 5, m = s; endif
44 if nargin < 4, h = s; endif
45 endif
46
47 n = zeros(size(Y));
48 lt = localtime(0);
49 t0 = mktime(lt);
50 h = h+lt.hour-24;
51 for i=1:prod(size(Y))
52 tm.usec = 1e6*rem(s(i),1);
53 tm.sec = floor(s(i));
54 tm.min = m(i);
55 tm.hour = h(i);
56 tm.mday = D(i);
57 tm.mon = M(i)-1;
58 tm.year = Y(i)-1900;
59 tm.zone = "GMT";
60 tm.wday = 0;
61 tm.yday = 0;
62 tm.isdst = 0;
63 if (Y(i) == 0 && M(i) == 1 && d(i) == 1)
64 n(i) = (h(i)*3600 + m(i)*60 + s(i))/86400;
65 else
66 t = mktime(tm);
67 if (t==-1 && Y(i) != 1969)
68 n(i) = -1;
69 else
70 n(i) = (t-t0) / 86400 + 719529;
71 endif
72 endif
73 endfor
74 endfunction
75