5687
|
1 ## Copyright (C) 2004 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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5687
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5687
|
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{datenum, datevec, weekday} |
|
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) |
6046
|
32 print_usage (); |
5687
|
33 endif |
|
34 |
|
35 eom = [31, 28, 31, 30 ,31, 30, 31, 31, 30, 31, 30, 31]; |
|
36 e = eom(m); |
|
37 e += (m == 2) & (mod (y, 4) == 0 & (mod (y, 100) != 0 | mod (y, 400) == 0)); |
|
38 |
|
39 endfunction |
|
40 |
|
41 # tests |
|
42 %!assert(eomday([-4:4],2),[29,28,28,28,29,28,28,28,29]) |
|
43 %!assert(eomday([-901,901],2),[28,28]) |
|
44 %!assert(eomday([-100,100],2),[28,28]) |
|
45 %!assert(eomday([-900,900],2),[28,28]) |
|
46 %!assert(eomday([-400,400],2),[29,29]) |
|
47 %!assert(eomday([-800,800],2),[29,29]) |
|
48 %!assert(eomday(2001,1:12),[31,28,31,30,31,30,31,31,30,31,30,31]) |
|
49 %!assert(eomday(1:3,1:3),[31,28,31]) |
|
50 %!assert(eomday(1:2000,2)',datevec(datenum(1:2000,3,0))(:,3)) |
|
51 %!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]) |
|
52 # demos |
|
53 %!demo |
|
54 %! y = 1900:1999; |
|
55 %! e = eomday (y, 2 * ones (1, 100)); |
|
56 %! y (find (e == 29)) |