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 Dec 10, 2019
1 parent 6e961b5 commit b201bac
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
64 changes: 31 additions & 33 deletions Modelica/Utilities/Internal.mo
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,23 @@ partial package PartialModelicaServices
parameter Types.ShapeType shapeType="box"
"Type of shape (box, sphere, cylinder, pipecylinder, cone, pipe, beam, gearwheel, spring, <external shape>)";
input Frames.Orientation R=Frames.nullRotation()
"Orientation object to rotate the world frame into the object frame"
annotation(Dialog);
"Orientation object to rotate the world frame into the object frame" annotation(Dialog);
input SI.Position r[3]={0,0,0}
"Position vector from origin of world frame to origin of object frame, resolved in world frame"
annotation(Dialog);
"Position vector from origin of world frame to origin of object frame, resolved in world frame" annotation(Dialog);
input SI.Position r_shape[3]={0,0,0}
"Position vector from origin of object frame to shape origin, resolved in object frame"
annotation(Dialog);
"Position vector from origin of object frame to shape origin, resolved in object frame" annotation(Dialog);
input Real lengthDirection[3](each final unit="1")={1,0,0}
"Vector in length direction, resolved in object frame"
annotation(Dialog);
"Vector in length direction, resolved in object frame" annotation(Dialog);
input Real widthDirection[3](each final unit="1")={0,1,0}
"Vector in width direction, resolved in object frame"
annotation(Dialog);
"Vector in width direction, resolved in object frame" annotation(Dialog);
input SI.Length length=0 "Length of visual object" annotation(Dialog);
input SI.Length width=0 "Width of visual object" annotation(Dialog);
input SI.Length height=0 "Height of visual object" annotation(Dialog);
input Types.ShapeExtra extra=0.0
"Additional size data for some of the shape types" annotation(Dialog);
input Real color[3]={255,0,0} "Color of shape" annotation(Dialog(colorSelector=true));
input Types.SpecularCoefficient specularCoefficient = 0.7
"Reflection of ambient light (= 0: light is completely absorbed)"
annotation(Dialog);
"Reflection of ambient light (= 0: light is completely absorbed)" annotation(Dialog);
annotation (
Documentation(info="<html>
Expand Down Expand Up @@ -245,8 +239,7 @@ end FileSystem;
output Integer year "Year";
external "C" ModelicaInternal_getTime(ms,sec,min,hour,day,mon,year)
annotation(Library="ModelicaExternalC");
annotation (__ModelicaAssociation_Impure=true,
Documentation(info="<html>
annotation (__ModelicaAssociation_Impure=true, Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
(ms, sec, min, hour, day, mon, year) = Internal.Time.<strong>getTime</strong>();
Expand Down Expand Up @@ -314,21 +307,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 @@ -338,16 +335,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 @@ -359,6 +354,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 @@ -370,7 +368,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 @@ -32,7 +32,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 @@ -43,16 +43,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 @@ -64,6 +62,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 @@ -143,6 +144,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 @@ -384,8 +384,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 @@ -399,11 +398,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 b201bac

Please sign in to comment.