From 9575f160e5b8984e26fac12261126c82d03aa541 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 | 43 +++++++++++++++++++--------------- Modelica/Utilities/Time.mo | 11 +++++---- ModelicaTest/Utilities.mo | 9 ++++--- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/Modelica/Utilities/Internal.mo b/Modelica/Utilities/Internal.mo index a96fb2e78b5..c2313c8eef2 100644 --- a/Modelica/Utilities/Internal.mo +++ b/Modelica/Utilities/Internal.mo @@ -322,21 +322,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

@@ -346,7 +350,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:

@@ -354,8 +358,6 @@ The returned Integer number of dow has the following meaning: Day of week Number -Sunday 0 - Monday 1 Tuesday 2 @@ -367,6 +369,9 @@ The returned Integer number of dow has the following meaning: Friday 5 Saturday 6 + +Sunday 7 +
@@ -378,7 +383,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 a1032138fb9..905be685e77 100644 --- a/Modelica/Utilities/Time.mo +++ b/Modelica/Utilities/Time.mo @@ -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=" @@ -42,7 +42,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:

@@ -50,8 +50,6 @@ The returned Integer number of dow has the following meaning: Day of week Number -Sunday 0 - Monday 1 Tuesday 2 @@ -63,6 +61,9 @@ The returned Integer number of dow has the following meaning: Friday 5 Saturday 6 + +Sunday 7 +
@@ -142,6 +143,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 d10844cc92a..1204ee0cf8d 100644 --- a/ModelicaTest/Utilities.mo +++ b/ModelicaTest/Utilities.mo @@ -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); @@ -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");