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} {datenums =} x2mdate (exceldatenums)
18 ## @deftypefnx {Function File} {datenums =} x2mdate (exceldatenums, convention)
19 ## @deftypefnx {Function File} {datenums =} x2mdate (exceldatenums, convention, "ExcelBug")
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
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 = x2mdate (dates, convention, excelbug)
46 excelbug = strcmpi(excelbug, "ExcelBug");
52 warning ("Negative date found, this will not work within MS Excel.")
56 adj = datenum(1900, 1, 1) - 2;
57 elseif convention == 1
58 adj = datenum(1904, 1, 1);
62 datemask = (dates < 61);
63 dates(datemask) = dates(datemask) + 1;
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))