Skip to content

Commit

Permalink
Added additional comments to dayofyear.m, and updated copyrights.
Browse files Browse the repository at this point in the history
  • Loading branch information
asifouna committed May 4, 2022
1 parent 4e9d677 commit 6ad3085
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion License.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2021, The MathWorks, Inc.
Copyright (c) 2022, The MathWorks, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Expand Down
32 changes: 22 additions & 10 deletions code/dayofyear.m
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
function doy = dayofyear(ddmmyyyy,dateFormat)
%DAYOFYEAR Converts a date string ("dd/mm/yyyy") to the day number of the
function doy = dayofyear(mmddyy,dateFormat)
%DAYOFYEAR Converts a date string ("mm/dd/yyyy") to the day number of the
%year.

% NOTE: MATLAB already does easily this using:
% doy = day(d,"dayofyear")
% where d is a datetime object

% Copyright 2021 The MathWorks, Inc.
% Copyright 2022 The MathWorks, Inc.

arguments
ddmmyyyy string;
mmddyy string;
dateFormat (1,1) string {mustBeMember(dateFormat,["mm/dd/yyyy","dd/mm/yyyy"])} = "mm/dd/yyyy";
end

% Check that ddmmyyyy was provided in the appropriate format
if numel(split(ddmmyyyy,"/")) ~= 3
% Check that mmddyy was provided in the appropriate format
if numel(split(mmddyy,"/")) ~= 3
error("dayofyear:InvalidDateFormat","Invalid date string. Expected date formatted as dd/mm/yyyy.")
end

% Create a datetime object depending on the dateFormat provided
if dateFormat == "mm/dd/yyyy"
d = datetime(ddmmyyyy,"Format","MM/dd/uuuu");
d = datetime(mmddyy,"Format","MM/dd/uuuu");
else
d = datetime(ddmmyyyy,"Format","dd/MM/uuuu");
d = datetime(mmddyy,"Format","dd/MM/uuuu");
end

% Initialize the days per month
daysPerMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
daysPerMonth = [ ...
31; % January
28; % February
31; % March
30; % April
31; % May
30; % June
31; % July
31; % August
30; % September
31; % October
30; % November
31]; % December

% Check for leap year
if mod(d.Year,4) == 0
% This is a leap year, so February to 29 days
% This is a leap year, so change February to 29 days
daysPerMonth(2) = 29;
end

Expand Down
4 changes: 2 additions & 2 deletions tests/ParameterizedTestExample.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
classdef ParameterizedTestExample < matlab.unittest.TestCase
% Creates 12 test points, one for the 15th day of every month of 2021
% Creates 12 test points, one test point for the 15th day of every month of 2021

% Copyright 2021 The MathWorks, Inc.
% Copyright 2022 The MathWorks, Inc.

properties (TestParameter)
monthNum = num2cell(1:12);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestExamples.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
% out to illustrate missing code coverage in continous
% integration (CI) systems

% Copyright 2021 The MathWorks, Inc.
% Copyright 2022 The MathWorks, Inc.

methods (Test)

Expand Down

0 comments on commit 6ad3085

Please sign in to comment.