1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
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
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
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/>.
17 ## @deftypefn {Function File} {exceldatenums =} m2xdate (datenums)
18 ## @deftypefnx {Function File} {exceldatenums =} m2xdate (datenums, convention)
19 ## @deftypefnx {Function File} {exceldatenums =} m2xdate (datenums, convention, "ExcelBug")
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
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.
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.
35 ## @seealso{datenum, x2mdate}
38 function dates = m2xdate (dates, convention, excelbug)
46 excelbug = strcmpi(excelbug, "ExcelBug");
52 adj = datenum(1900, 1, 1) - 2;
53 elseif convention == 1
54 adj = datenum(1904, 1, 1);
58 datemask = (dates < datenum(1900, 3, 1));
59 dates(datemask) = dates(datemask) - 1;
63 warning ("Negative date found, this will not work within MS Excel.")
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)