From b201bac155a3e2887246a60a16c4fb9110877ff9 Mon Sep 17 00:00:00 2001 From: Thomas Beutlich Date: Tue, 10 Dec 2019 22:07:28 +0100 Subject: [PATCH] Let dayOfWeek return Integer in range [1, 7] --- Modelica/Utilities/Internal.mo | 64 ++++++++++++++++------------------ Modelica/Utilities/Time.mo | 11 +++--- ModelicaTest/Utilities.mo | 9 +++-- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/Modelica/Utilities/Internal.mo b/Modelica/Utilities/Internal.mo index 97b8698770..2ffdbb61e2 100644 --- a/Modelica/Utilities/Internal.mo +++ b/Modelica/Utilities/Internal.mo @@ -16,20 +16,15 @@ partial package PartialModelicaServices parameter Types.ShapeType shapeType="box" "Type of shape (box, sphere, cylinder, pipecylinder, cone, pipe, beam, gearwheel, spring, )"; 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); @@ -37,8 +32,7 @@ partial package PartialModelicaServices "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=" @@ -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=" + annotation (__ModelicaAssociation_Impure=true, Documentation(info="

Syntax

 (ms, sec, min, hour, day, mon, year) = Internal.Time.getTime();
@@ -314,21 +307,25 @@ All returned values are of type Integer and have the following meaning:
 "));
     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="
 

Syntax

@@ -338,7 +335,7 @@ dow = Internal.Time.dayOfWeek(year, mon, day);
 

Returns the day of the week for a given date using Tomohiko Sakamoto's algorithm. -The returned Integer number of dow has the following meaning: +The returned Integer number of dow has the following meaning:

@@ -346,8 +343,6 @@ The returned Integer number of dow has the following meaning: Day of week Number -Sunday 0 - Monday 1 Tuesday 2 @@ -359,6 +354,9 @@ The returned Integer number of dow has the following meaning: Friday 5 Saturday 6 + +Sunday 7 +
@@ -370,7 +368,7 @@ dow = dayOfWeek(2020) // = 3 // Jan. 01, 2020 (New Year's Day) is a Wednesday
")); - end dayOfWeek; + end dayOfWeek; annotation ( Documentation(info=" diff --git a/Modelica/Utilities/Time.mo b/Modelica/Utilities/Time.mo index de11920041..776d128657 100644 --- a/Modelica/Utilities/Time.mo +++ b/Modelica/Utilities/Time.mo @@ -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=" @@ -43,7 +43,7 @@ dow = Time.dayOfWeek(timeIn);

Description

Returns the day of the week for a given date using Tomohiko Sakamoto's algorithm. -The returned Integer number of dow has the following meaning: +The returned Integer number of dow has the following meaning:

@@ -51,8 +51,6 @@ The returned Integer number of dow has the following meaning: Day of week Number -Sunday 0 - Monday 1 Tuesday 2 @@ -64,6 +62,9 @@ The returned Integer number of dow has the following meaning: Friday 5 Saturday 6 + +Sunday 7 +
@@ -143,6 +144,8 @@ days = leapDays(2000, 2020) // = 5 leap days in range [2000, 2019] ")); end leapDays; + final constant String weekDays[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} "Array of week days"; + annotation ( Documentation(info="

diff --git a/ModelicaTest/Utilities.mo b/ModelicaTest/Utilities.mo index 2df001bde8..df314e194d 100644 --- a/ModelicaTest/Utilities.mo +++ b/ModelicaTest/Utilities.mo @@ -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); @@ -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");