-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIFC.m
More file actions
53 lines (42 loc) · 1.57 KB
/
IFC.m
File metadata and controls
53 lines (42 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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