Skip to content

Commit 9575f16

Browse files
committed
Let dayOfWeek return Integer in range [1, 7]
1 parent 2e8f7ec commit 9575f16

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

Modelica/Utilities/Internal.mo

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -322,21 +322,25 @@ All returned values are of type Integer and have the following meaning:
322322
</html>"));
323323
end getTime;
324324

325-
function dayOfWeek "Return day of week for given date"
326-
extends Modelica.Icons.Function;
327-
input Integer year "Year";
328-
input Integer mon=1 "Month";
329-
input Integer day=1 "Day of month";
330-
output Integer dow "Day of week: 0 = Sunday, ..., 6 = Saturday";
331-
protected
332-
constant Integer t[:] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
333-
Integer y = year;
334-
algorithm
335-
assert(mon >= 1 and mon <= 12, "Month is out of range.");
336-
if mon < 3 then
337-
y := y - 1;
338-
end if;
339-
dow := mod(y + div(y, 4) - div(y, 100) + div(y, 400) + t[mon] + day, 7);
325+
function dayOfWeek "Return day of week for given date"
326+
extends Modelica.Icons.Function;
327+
input Integer year "Year";
328+
input Integer mon=1 "Month";
329+
input Integer day=1 "Day of month";
330+
output Integer dow(min=1, max=7) "Day of week: 1 = Monday, ..., 6 = Saturday, 7 = Sunday";
331+
protected
332+
constant Integer t[:] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
333+
Integer y = year;
334+
algorithm
335+
assert(mon >= 1 and mon <= 12, "Month is out of range.");
336+
if mon < 3 then
337+
y := y - 1;
338+
end if;
339+
dow := mod(y + div(y, 4) - div(y, 100) + div(y, 400) + t[mon] + day, 7);
340+
// One-based indexing: Sunday is 7
341+
if dow == 0 then
342+
dow := 7;
343+
end if;
340344
annotation (Documentation(info="<html>
341345
<h4>Syntax</h4>
342346
<blockquote><pre>
@@ -346,16 +350,14 @@ dow = Internal.Time.<strong>dayOfWeek</strong>(year, mon, day);
346350
<p>
347351
<p>
348352
Returns the day of the week for a given date using Tomohiko Sakamoto's algorithm.
349-
The returned Integer number of <code>dow</dow> has the following meaning:
353+
The returned Integer number of <code>dow</code> has the following meaning:
350354
</p>
351355
352356
<blockquote>
353357
<table border=1 cellspacing=0 cellpadding=2>
354358
<tr><th>Day of week</th>
355359
<th>Number</th></tr>
356360
357-
<tr><td>Sunday</td> <td>0</td></tr>
358-
359361
<tr><td>Monday</td> <td>1</td></tr>
360362
361363
<tr><td>Tuesday</td> <td>2</td></tr>
@@ -367,6 +369,9 @@ The returned Integer number of <code>dow</dow> has the following meaning:
367369
<tr><td>Friday</td> <td>5</td></tr>
368370
369371
<tr><td>Saturday</td> <td>6</td></tr>
372+
373+
<tr><td>Sunday</td> <td>7</td></tr>
374+
370375
</table>
371376
</blockquote>
372377
@@ -378,7 +383,7 @@ dow = dayOfWeek(2020) // = 3
378383
// Jan. 01, 2020 (New Year's Day) is a Wednesday
379384
</pre></blockquote>
380385
</html>"));
381-
end dayOfWeek;
386+
end dayOfWeek;
382387

383388
annotation (
384389
Documentation(info="<html>

Modelica/Utilities/Time.mo

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ now = getTime() // = Modelica.Utilities.Types.TimeType(281, 30, 13, 10, 15, 2,
3131
function dayOfWeek "Return day of week for given date"
3232
extends Modelica.Icons.Function;
3333
input Types.TimeType timeIn "Date";
34-
output Integer dow "Day of week: 0 = Sunday, ..., 6 = Saturday";
34+
output Integer dow(min=1, max=7) "Day of week: 1 = Monday, ..., 6 = Saturday, 7 = Sunday";
3535
algorithm
3636
dow := Internal.Time.dayOfWeek(timeIn.year, timeIn.month, timeIn.day);
3737
annotation (Documentation(info="<html>
@@ -42,16 +42,14 @@ dow = Time.<strong>dayOfWeek</strong>(timeIn);
4242
<h4>Description</h4>
4343
<p>
4444
Returns the day of the week for a given date using Tomohiko Sakamoto's algorithm.
45-
The returned Integer number of <code>dow</dow> has the following meaning:
45+
The returned Integer number of <code>dow</code> has the following meaning:
4646
</p>
4747
4848
<blockquote>
4949
<table border=1 cellspacing=0 cellpadding=2>
5050
<tr><th>Day of week</th>
5151
<th>Number</th></tr>
5252
53-
<tr><td>Sunday</td> <td>0</td></tr>
54-
5553
<tr><td>Monday</td> <td>1</td></tr>
5654
5755
<tr><td>Tuesday</td> <td>2</td></tr>
@@ -63,6 +61,9 @@ The returned Integer number of <code>dow</dow> has the following meaning:
6361
<tr><td>Friday</td> <td>5</td></tr>
6462
6563
<tr><td>Saturday</td> <td>6</td></tr>
64+
65+
<tr><td>Sunday</td> <td>7</td></tr>
66+
6667
</table>
6768
</blockquote>
6869
@@ -142,6 +143,8 @@ days = leapDays(2000, 2020) // = 5 leap days in range [2000, 2019]
142143
</html>"));
143144
end leapDays;
144145

146+
final constant String weekDays[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} "Array of week days";
147+
145148
annotation (
146149
Documentation(info="<html>
147150
<p>

ModelicaTest/Utilities.mo

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,7 @@ extends Modelica.Icons.ExamplesPackage;
388388
output Boolean ok;
389389
protected
390390
Modelica.Utilities.Types.TimeType now;
391-
Integer dow "Day of week";
392-
constant String weekDays[:] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
391+
Integer dow(min=1, max=7) "Day of week";
393392
algorithm
394393
Streams.print("... Test of Modelica.Utilities.Time");
395394
Streams.print("... Test of Modelica.Utilities.Time", logFile);
@@ -403,11 +402,11 @@ extends Modelica.Icons.ExamplesPackage;
403402
Streams.print(" mon = " + String(now.month));
404403
Streams.print(" year = " + String(now.year));
405404
dow := Modelica.Utilities.Time.dayOfWeek(now);
406-
Streams.print(" dow = " + weekDays[dow + 1]);
405+
Streams.print(" dow = " + Modelica.Utilities.Time.weekDays[dow]);
407406

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

412411
assert(not Modelica.Utilities.Time.isLeapYear(1900), "Time.isLeapYear failed");
413412
assert(Modelica.Utilities.Time.isLeapYear(2000), "Time.isLeapYear failed");

0 commit comments

Comments
 (0)