]> Creatis software - CreaPhase.git/blob - octave_packages/m/time/eomday.m
update packages
[CreaPhase.git] / octave_packages / m / time / eomday.m
1 ## Copyright (C) 2004-2012 Paul Kienzle
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{e} =} eomday (@var{y}, @var{m})
21 ## Return the last day of the month @var{m} for the year @var{y}.
22 ## @seealso{weekday, datenum, datevec, is_leap_year, calendar}
23 ## @end deftypefn
24
25 ## Author: pkienzle <pkienzle@users.sf.net>
26 ## Created: 25 July 2004
27 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com>
28
29 function e = eomday (y, m)
30
31   if (nargin != 2)
32     print_usage ();
33   endif
34
35   eom = [31, 28, 31, 30 ,31, 30, 31, 31, 30, 31, 30, 31];
36   e = reshape (eom(m), size (m));
37   e += (m == 2) & (mod (y, 4) == 0 & (mod (y, 100) != 0 | mod (y, 400) == 0));
38
39 endfunction
40
41
42 %!demo
43 %! ## Find leap years in the 20th century
44 %! y = 1900:1999;
45 %! e = eomday (y, repmat (2, [1, 100]));
46 %! y(find (e == 29))
47
48 # tests
49 %!assert (eomday ([-4:4],2), [29,28,28,28,29,28,28,28,29])
50 %!assert (eomday ([-901,901],2), [28,28])
51 %!assert (eomday ([-100,100],2), [28,28])
52 %!assert (eomday ([-900,900],2), [28,28])
53 %!assert (eomday ([-400,400],2), [29,29])
54 %!assert (eomday ([-800,800],2), [29,29])
55 %!assert (eomday (2001,1:12), [31,28,31,30,31,30,31,31,30,31,30,31])
56 %!assert (eomday (1:3,1:3), [31,28,31])
57 %!assert (eomday (1:2000,2)', datevec(datenum(1:2000,3,0))(:,3))
58 %!assert ([1900:1999](find(eomday(1900:1999,2*ones(1,100))==29)), [1904,1908,1912,1916,1920,1924,1928,1932,1936,1940,1944,1948,1952,1956,1960,1964,1968,1972,1976,1980,1984,1988,1992,1996])
59 %!assert (eomday ([2004;2005], [2;2]), [29;28])
60
61 %% Test input validation
62 %!error eomday ()
63 %!error eomday (1)
64 %!error eomday (1,2,3)
65