Skip to content

Commit

Permalink
Let dayOfWeek return Integer in range [1, 7]
Browse files Browse the repository at this point in the history
  • Loading branch information
beutlich committed Apr 11, 2020
1 parent 78aff17 commit 2223e91
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
43 changes: 24 additions & 19 deletions Modelica/Utilities/Internal.mo
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,25 @@ All returned values are of type Integer and have the following meaning:
</html>"));
end getTime;

function dayOfWeek "Return day of week for given date"
extends Modelica.Icons.Function;
input Integer year "Year";
input Integer mon=1 "Month";
input Integer day=1 "Day of month";
output Integer dow "Day of week: 0 = Sunday, ..., 6 = Saturday";
protected
constant Integer t[:] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
Integer y = year;
algorithm
assert(mon >= 1 and mon <= 12, "Month is out of range.");
if mon < 3 then
y := y - 1;
end if;
dow := mod(y + div(y, 4) - div(y, 100) + div(y, 400) + t[mon] + day, 7);
function dayOfWeek "Return day of week for given date"
extends Modelica.Icons.Function;
input Integer year "Year";
input Integer mon=1 "Month";
input Integer day=1 "Day of month";
output Integer dow(min=1, max=7) "Day of week: 1 = Monday, ..., 6 = Saturday, 7 = Sunday";
protected
constant Integer t[:] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
Integer y = year;
algorithm
assert(mon >= 1 and mon <= 12, "Month is out of range.");
if mon < 3 then
y := y - 1;
end if;
dow := mod(y + div(y, 4) - div(y, 100) + div(y, 400) + t[mon] + day, 7);
// One-based indexing: Sunday is 7
if dow == 0 then
dow := 7;
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Expand All @@ -346,16 +350,14 @@ dow = Internal.Time.<strong>dayOfWeek</strong>(year, mon, day);
<p>
<p>
Returns the day of the week for a given date using Tomohiko Sakamoto's algorithm.
The returned Integer number of <code>dow</dow> has the following meaning:
The returned Integer number of <code>dow</code> has the following meaning:
</p>

<blockquote>
<table border=1 cellspacing=0 cellpadding=2>
<tr><th>Day of week</th>
<th>Number</th></tr>

<tr><td>Sunday</td> <td>0</td></tr>

<tr><td>Monday</td> <td>1</td></tr>

<tr><td>Tuesday</td> <td>2</td></tr>
Expand All @@ -367,6 +369,9 @@ The returned Integer number of <code>dow</dow> has the following meaning:
<tr><td>Friday</td> <td>5</td></tr>

<tr><td>Saturday</td> <td>6</td></tr>

<tr><td>Sunday</td> <td>7</td></tr>

</table>
</blockquote>

Expand All @@ -378,7 +383,7 @@ dow = dayOfWeek(2020) // = 3
// Jan. 01, 2020 (New Year's Day) is a Wednesday
</pre></blockquote>
</html>"));
end dayOfWeek;
end dayOfWeek;

annotation (
Documentation(info="<html>
Expand Down
11 changes: 7 additions & 4 deletions Modelica/Utilities/Time.mo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ now = getTime() // = Modelica.Utilities.Types.TimeType(281, 30, 13, 10, 15, 2,
function dayOfWeek "Return day of week for given date"
extends Modelica.Icons.Function;
input Types.TimeType timeIn "Date";
output Integer dow "Day of week: 0 = Sunday, ..., 6 = Saturday";
output Integer dow(min=1, max=7) "Day of week: 1 = Monday, ..., 6 = Saturday, 7 = Sunday";
algorithm
dow := Internal.Time.dayOfWeek(timeIn.year, timeIn.month, timeIn.day);
annotation (Documentation(info="<html>
Expand All @@ -42,16 +42,14 @@ dow = Time.<strong>dayOfWeek</strong>(timeIn);
<h4>Description</h4>
<p>
Returns the day of the week for a given date using Tomohiko Sakamoto's algorithm.
The returned Integer number of <code>dow</dow> has the following meaning:
The returned Integer number of <code>dow</code> has the following meaning:
</p>

<blockquote>
<table border=1 cellspacing=0 cellpadding=2>
<tr><th>Day of week</th>
<th>Number</th></tr>

<tr><td>Sunday</td> <td>0</td></tr>

<tr><td>Monday</td> <td>1</td></tr>

<tr><td>Tuesday</td> <td>2</td></tr>
Expand All @@ -63,6 +61,9 @@ The returned Integer number of <code>dow</dow> has the following meaning:
<tr><td>Friday</td> <td>5</td></tr>

<tr><td>Saturday</td> <td>6</td></tr>

<tr><td>Sunday</td> <td>7</td></tr>

</table>
</blockquote>

Expand Down Expand Up @@ -142,6 +143,8 @@ days = leapDays(2000, 2020) // = 5 leap days in range [2000, 2019]
</html>"));
end leapDays;

final constant String weekDays[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} "Array of week days";

annotation (
Documentation(info="<html>
<p>
Expand Down
9 changes: 4 additions & 5 deletions ModelicaTest/Utilities.mo
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,7 @@ extends Modelica.Icons.ExamplesPackage;
output Boolean ok;
protected
Modelica.Utilities.Types.TimeType now;
Integer dow "Day of week";
constant String weekDays[:] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Integer dow(min=1, max=7) "Day of week";
algorithm
Streams.print("... Test of Modelica.Utilities.Time");
Streams.print("... Test of Modelica.Utilities.Time", logFile);
Expand All @@ -403,11 +402,11 @@ extends Modelica.Icons.ExamplesPackage;
Streams.print(" mon = " + String(now.month));
Streams.print(" year = " + String(now.year));
dow := Modelica.Utilities.Time.dayOfWeek(now);
Streams.print(" dow = " + weekDays[dow + 1]);
Streams.print(" dow = " + Modelica.Utilities.Time.weekDays[dow]);

dow := Modelica.Utilities.Time.dayOfWeek(
Modelica.Utilities.Types.TimeType(year=2019, month=12, day=6, hour=12, minute=0, second=0, millisecond=0));
assert(5 == dow, "Time.dayOfWeek failed");
Modelica.Utilities.Types.TimeType(year=2019, month=12, day=8, hour=12, minute=0, second=0, millisecond=0));
assert(7 == dow, "Time.dayOfWeek failed");

assert(not Modelica.Utilities.Time.isLeapYear(1900), "Time.isLeapYear failed");
assert(Modelica.Utilities.Time.isLeapYear(2000), "Time.isLeapYear failed");
Expand Down

0 comments on commit 2223e91

Please sign in to comment.