]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/m2xdate.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / m2xdate.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} {exceldatenums =} m2xdate (datenums)
18 ## @deftypefnx {Function File} {exceldatenums =} m2xdate (datenums, convention)
19 ## @deftypefnx {Function File} {exceldatenums =} m2xdate (datenums, convention, "ExcelBug")
20 ##
21 ## Convert @var{datenums} from the internal date format to the format
22 ## used by Microsoft Excel.  If set to 0 (default, Excel for Windows),
23 ## @var{convention} specifies to use the Excel 1900 convention where Jan
24 ## 1, 1900 corresponds to Excel serial date number 1.  If set to 1
25 ## (Excel for Mac), @var{convention} specifies to use the Excel 1904
26 ## convention where Jan 1, 1904 corresponds to Excel serial date number
27 ## 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 = m2xdate (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 convention == 0
52         adj = datenum(1900, 1, 1) - 2;
53   elseif convention == 1
54         adj = datenum(1904, 1, 1);
55   endif
56
57   if excelbug
58         datemask = (dates < datenum(1900, 3, 1));
59         dates(datemask) = dates(datemask) - 1;
60   endif
61   dates = dates - adj;
62   if any (dates < 0)
63         warning ("Negative date found, this will not work within MS Excel.")
64   endif
65
66 endfunction
67
68 ## Tests
69 %!assert(m2xdate(datenum(2008, 1, 1)), 39448)
70 %!assert(m2xdate(datenum(2007:2008, 1, 1)), [39083 39448])
71 %!assert(m2xdate(datenum(1900, 1, 1)), 2)
72 %!assert(m2xdate(datenum(1900, 1, 1), 0, "ExcelBug"), 1)
73 %!assert(m2xdate(datenum(1904, 1, 1), 1), 0)
74