]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/x2mdate.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / x2mdate.m
1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {datenums =} x2mdate (exceldatenums)
18 ## @deftypefnx {Function File} {datenums =} x2mdate (exceldatenums, convention)
19 ## @deftypefnx {Function File} {datenums =} x2mdate (exceldatenums, convention, "ExcelBug")
20 ##
21 ## Convert @var{datenums} from the Microsoft Excel date format to the
22 ## format used by @code{datenum}.  If set to 0 (default, Excel for
23 ## Windows), @var{convention} specifies to use the Excel 1900 convention
24 ## where Jan 1, 1900 corresponds to Excel serial date number 1.  If set
25 ## to 1 (Excel for Mac), @var{convention} specifies to use the Excel
26 ## 1904 convention where Jan 1, 1904 corresponds to Excel serial date
27 ## number 0.
28 ##
29 ## Note that this does not take into account the Excel bug where 1900 is
30 ## considered to be a leap year unless you give the "ExcelBug" option.
31 ##
32 ## Excel does not represent dates prior to 1 January 1900 using this
33 ## format, so a warning will be issued if any dates preceed this date.
34 ##
35 ## @seealso{datenum, x2mdate}
36 ## @end deftypefn
37
38 function dates = x2mdate (dates, convention, excelbug)
39
40   if nargin == 1
41         convention = 0;
42         excelbug = false();
43   elseif nargin == 2
44         excelbug = false();
45   elseif nargin == 3
46         excelbug = strcmpi(excelbug, "ExcelBug");
47   else
48         print_usage ();
49   endif
50
51   if any (dates < 0)
52         warning ("Negative date found, this will not work within MS Excel.")
53   endif
54
55   if convention == 0
56         adj = datenum(1900, 1, 1) - 2;
57   elseif convention == 1
58         adj = datenum(1904, 1, 1);
59   endif
60
61   if excelbug
62         datemask = (dates < 61);
63         dates(datemask) = dates(datemask) + 1;
64   endif
65   dates = dates + adj;
66
67 endfunction
68
69 ## Tests
70 %!assert(x2mdate(39448), datenum(2008, 1, 1))
71 %!assert(x2mdate([39083 39448]), datenum(2007:2008, 1, 1))
72 %!assert(x2mdate(2), datenum(1900, 1, 1))
73 %!assert(x2mdate(1, 0, "ExcelBug"), datenum(1900, 1, 1))
74 %!assert(x2mdate(0, 1), datenum(1904, 1, 1))
75