]> Creatis software - CreaPhase.git/blob - octave_packages/m/time/is_leap_year.m
update packages
[CreaPhase.git] / octave_packages / m / time / is_leap_year.m
1 ## Copyright (C) 1996-2012 John W. Eaton
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} {} is_leap_year ()
21 ## @deftypefnx {Function File} {} is_leap_year (@var{year})
22 ## Return true if @var{year} is a leap year and false otherwise.  If no
23 ## year is specified, @code{is_leap_year} uses the current year.
24 ## For example:
25 ##
26 ## @example
27 ## @group
28 ## is_leap_year (2000)
29 ##    @result{} 1
30 ## @end group
31 ## @end example
32 ## @seealso{weekday, eomday, calendar}
33 ## @end deftypefn
34
35 ## Author: jwe
36
37 function retval = is_leap_year (year)
38
39   if (nargin > 1)
40     print_usage ();
41   endif
42
43   if (nargin == 0)
44     t = clock ();
45     year = t(1);
46   endif
47
48   retval = (rem (year, 4) == 0 & rem (year, 100) != 0) | (rem (year, 400) == 0);
49
50 endfunction
51
52
53 %!assert (is_leap_year (2000), true)
54 %!assert (is_leap_year (1976), true)
55 %!assert (is_leap_year (1000), false)
56 %!assert (is_leap_year (1800), false)
57 %!assert (is_leap_year (1600), true)
58
59 %!error is_leap_year (1, 2);
60