File Exchange

image thumbnail

Isleap

version 2.0.0.0 (18.6 KB) by Giuseppe Cardillo
Calculate leap year

5 Downloads

Updated 31 May 2018

View Version History

GitHub view license on GitHub

ISLEAP True for leap year.
ISLEAP(Year) returns 1 if Year is a leap year and 0 otherwise.
ISLEAP is only set for gregorian calendar, so Year >= 1583
Syntax: ISLEAP(YEAR)

Inputs:
YEAR - Year of interest (default = current year).
You can input a vector of years.
Outputs:
Logical vector.

Example:

Calling on Matlab the function: isleap

Answer is: 0

Calling on Matlab the function: x=isleap([2007 2008])

Answer is:
x = 0 1

Created by Giuseppe Cardillo
giuseppe.cardillo-edta@poste.it
Modified after Simon Jan suggestions
To cite this file, this would be an appropriate format:
Cardillo G. (2007) Isleap: a simple routine to test if a year is leap.
http://www.mathworks.com/matlabcentral/fileexchange/14172

Cite As

Giuseppe Cardillo (2020). Isleap (https://github.com/dnafinder/isleap), GitHub. Retrieved .

Comments and Ratings (9)

Michael McCarthy

Giuseppe Cardillo

yes it is possible. I'll change to make it compatible

Michael McCarthy

Hi Giuseppe, do you have the Financial Toolbox installed? I get the error 'Undefined function 'year' for input arguments of type 'double'', if I run year(now). 'now' returns a date number, whereas 'year' (year number) requires a datetime value. 'year' (year of date), from the Financial Toolbox, accepts date numbers. Possibly this is the issue?

Giuseppe Cardillo

No, it is not an error.
help now
now Current date and time as date number.
T = now returns the current date and time as a serial date
number.

FLOOR(now) is the current date and REM(now,1) is the current time.
DATESTR(now) is the current date and time as a character vector.

See also date, datenum, datestr, clock.

Michael McCarthy

I think there's a typo in the March 2018 update. Line 35 should be: year(datetime('now')), rather than year(now). Otherwise, this is very useful. Cheers!

Jan

Descriptive help, reliable error checking, new version is really efficient, does exactly what the name says.

Jan

CLOCK -> DATE -> DATESTR -> STR2DOUBLE waste a lot of time: str2double(datestr(date,10))
Faster: c = clock; default = {c(1)};

Checking the number of inputs can be done easier and faster without VARARGIN, e.g.:
function x = isleap(Year)
switch nargin
case 1, % Your error checks...
case 0, c = clock; Year = c(1);
otherwise error(...);
end

The permanent disabling of the warning for logical conversion is not secure. You can omit this by simplifying the engine - instead of:
warning('OFF','MATLAB:conversionToLogical')
A=logical(mod(Year,4));
B=logical(mod(Year,100));
C=logical(mod(Year,400));
x=ismember(Year,Year(~C | (~A & B)));
call just:
x = ~mod(Year, 4) & (mod(Year, 100) | ~mod(Year, 400));
And suddenly the function is 150 times faster (400 times if called without argument).
Good help, H1-line, date, author and reference included. I'll rate this, if its efficiency is encreased.

N/A

Cool, just what I needed. Thanks! :o]

Marc FC

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!