53 lines
1.5 KiB
Matlab
53 lines
1.5 KiB
Matlab
function [nMonths, Days, leaped, yeared] = IFCmonths(Days, Year)
|
|
arguments (Input)
|
|
Days (:, 1) uint16
|
|
Year (:, 1) uint16
|
|
end
|
|
arguments (Output)
|
|
nMonths (:, 1) uint8
|
|
Days (:, 1) uint8
|
|
leaped (:, 1) logical
|
|
yeared (:, 1) logical
|
|
end
|
|
|
|
% intialize output to first month
|
|
nMonths = ones(size(Days));
|
|
|
|
% initialize output saying a leap day happened to false
|
|
leaped = false(size(Days));
|
|
|
|
% initialize output saying a year day happened to false
|
|
yeared = false(size(Days));
|
|
|
|
% check if it's a leap year
|
|
leap = isLeap(Year);
|
|
|
|
% while more days than 1 month, add months to nMonths
|
|
excess = Days > 28;
|
|
while any(excess)
|
|
% Subtract 28 days and add one month
|
|
Days(excess) = Days(excess) - 28;
|
|
nMonths(excess) = nMonths(excess) + 1;
|
|
|
|
% Find dates which are reaching the leap day (if it exists)
|
|
leaping = (leap & nMonths == 7);
|
|
% subtract leap day before proceeding to add another month
|
|
Days(leaping) = Days(leaping) - 1;
|
|
% Set leap day output to true
|
|
leaped(leaping) = true;
|
|
% Set the month to 14 (no month) if there are no days left
|
|
% meaning the last day is the leap day
|
|
% (which is not in any month)
|
|
noneLeft = ~Days;
|
|
nMonths(noneLeft) = 14;
|
|
% continue subtracting days and adding months as needed
|
|
|
|
% find excess days remaining for next iteration
|
|
excess = Days > 28;
|
|
end
|
|
|
|
% check for if a year day occured
|
|
yearing = (nMonths == 14 & Days == 1);
|
|
yeared(yearing) = true; % Year day achieved, set flag
|
|
Days(yearing) = 0; % decrement Days since year day was taken
|
|
end |