8920
|
1 ## Copyright (C) 1996, 1997, 2006, 2007, 2008 John W. Eaton |
5596
|
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. |
5596
|
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/>. |
5596
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} is_leap_year (@var{year}) |
|
21 ## Return 1 if the given year is a leap year and 0 otherwise. If no |
|
22 ## arguments are provided, @code{is_leap_year} will use the current year. |
|
23 ## For example, |
|
24 ## |
|
25 ## @example |
|
26 ## @group |
|
27 ## is_leap_year (2000) |
|
28 ## @result{} 1 |
|
29 ## @end group |
|
30 ## @end example |
|
31 ## @end deftypefn |
|
32 |
|
33 ## Author: jwe |
|
34 |
|
35 function retval = is_leap_year (year) |
|
36 |
|
37 if (nargin > 1) |
6046
|
38 print_usage (); |
5596
|
39 endif |
|
40 |
|
41 if (nargin == 0) |
|
42 t = clock (); |
|
43 year = t (1); |
|
44 endif |
|
45 |
|
46 retval = ((rem (year, 4) == 0 & rem (year, 100) != 0) ... |
|
47 | rem (year, 400) == 0); |
|
48 |
|
49 endfunction |
7411
|
50 |
|
51 %!assert((is_leap_year (2000) == 1 && is_leap_year (1976) == 1 |
|
52 %! && is_leap_year (1000) == 0 && is_leap_year (1800) == 0 |
|
53 %! && is_leap_year (1600) == 1)); |
|
54 |
|
55 %!error is_leap_year (1, 2); |
|
56 |