initial commit of Gregorian/IFC date conversion tools and unit tests
This commit is contained in:
53
IFC.m
Normal file
53
IFC.m
Normal file
@@ -0,0 +1,53 @@
|
||||
function fixed = IFC(gregorian)
|
||||
arguments (Input)
|
||||
gregorian (:,1) datetime
|
||||
end
|
||||
arguments (Output)
|
||||
fixed (:, 4) table
|
||||
end
|
||||
% gregorian datetime 1 arg input?
|
||||
% y/m/d 3 args input?
|
||||
% TODO inputs validation
|
||||
|
||||
% prepare output struct
|
||||
fixed = struct('Year', [], 'Month', '', 'Day', [], 'DayOfWeek', '');
|
||||
fixed = repmat(fixed, length(gregorian), 1);
|
||||
|
||||
% eliminate time from input, only date matters
|
||||
gregorian.Hour = 0; gregorian.Minute = 0; gregorian.Second = 0;
|
||||
|
||||
% year remains constant
|
||||
Year = num2cell(uint16(year(gregorian')));
|
||||
[fixed.Year] = Year{:};
|
||||
|
||||
% find number of days into the current year
|
||||
Days = days(gregorian - datetime([fixed.Year] - 1, 12, 31)');
|
||||
|
||||
% Calculate IFC month, number of days into that month, and if leap day
|
||||
% and/or year day occured
|
||||
[nMonths, nDays, leaped, yeared] = IFCmonths(Days, [fixed.Year]');
|
||||
|
||||
% write month to output
|
||||
Month = num2cell(MONTHS(nMonths));
|
||||
[fixed.Month] = Month{:};
|
||||
|
||||
% write number of days in month to output
|
||||
nDays = num2cell(nDays);
|
||||
[fixed.Day] = nDays{:};
|
||||
|
||||
% write day of week to output
|
||||
DOW = num2cell(DAYSOFWEEK(mod([fixed.Day]', 7)));
|
||||
[fixed.DayOfWeek] = DOW{:};
|
||||
|
||||
% Write year days as special days of the week
|
||||
yearDays = num2cell(repmat(DAYSOFWEEK(7), 1, sum(yeared)));
|
||||
[fixed(yeared).DayOfWeek] = yearDays{:}; % try 2 year/leap days in a vector input
|
||||
|
||||
% Write leap days as special days of the week
|
||||
leapDayFlags = ~yeared & leaped & [fixed.Month]' == MONTHS(14) & [fixed.Day]' == 0;
|
||||
leapDays = num2cell(repmat(DAYSOFWEEK(8), 1, sum(leapDayFlags)));
|
||||
[fixed(leapDayFlags).DayOfWeek] = leapDays{:};
|
||||
|
||||
% convert output to table
|
||||
fixed = struct2table(fixed);
|
||||
end
|
||||
Reference in New Issue
Block a user