Skip to content

Commit b201bac

Browse files
committed
Let dayOfWeek return Integer in range [1, 7]
1 parent 6e961b5 commit b201bac

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

Modelica/Utilities/Internal.mo

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,23 @@ partial package PartialModelicaServices
1616
parameter Types.ShapeType shapeType="box"
1717
"Type of shape (box, sphere, cylinder, pipecylinder, cone, pipe, beam, gearwheel, spring, <external shape>)";
1818
input Frames.Orientation R=Frames.nullRotation()
19-
"Orientation object to rotate the world frame into the object frame"
20-
annotation(Dialog);
19+
"Orientation object to rotate the world frame into the object frame" annotation(Dialog);
2120
input SI.Position r[3]={0,0,0}
22-
"Position vector from origin of world frame to origin of object frame, resolved in world frame"
23-
annotation(Dialog);
21+
"Position vector from origin of world frame to origin of object frame, resolved in world frame" annotation(Dialog);
2422
input SI.Position r_shape[3]={0,0,0}
25-
"Position vector from origin of object frame to shape origin, resolved in object frame"
26-
annotation(Dialog);
23+
"Position vector from origin of object frame to shape origin, resolved in object frame" annotation(Dialog);
2724
input Real lengthDirection[3](each final unit="1")={1,0,0}
28-
"Vector in length direction, resolved in object frame"
29-
annotation(Dialog);
25+
"Vector in length direction, resolved in object frame" annotation(Dialog);
3026
input Real widthDirection[3](each final unit="1")={0,1,0}
31-
"Vector in width direction, resolved in object frame"
32-
annotation(Dialog);
27+
"Vector in width direction, resolved in object frame" annotation(Dialog);
3328
input SI.Length length=0 "Length of visual object" annotation(Dialog);
3429
input SI.Length width=0 "Width of visual object" annotation(Dialog);
3530
input SI.Length height=0 "Height of visual object" annotation(Dialog);
3631
input Types.ShapeExtra extra=0.0
3732
"Additional size data for some of the shape types" annotation(Dialog);
3833
input Real color[3]={255,0,0} "Color of shape" annotation(Dialog(colorSelector=true));
3934
input Types.SpecularCoefficient specularCoefficient = 0.7
40-
"Reflection of ambient light (= 0: light is completely absorbed)"
41-
annotation(Dialog);
35+
"Reflection of ambient light (= 0: light is completely absorbed)" annotation(Dialog);
4236
annotation (
4337
Documentation(info="<html>
4438
@@ -245,8 +239,7 @@ end FileSystem;
245239
output Integer year "Year";
246240
external "C" ModelicaInternal_getTime(ms,sec,min,hour,day,mon,year)
247241
annotation(Library="ModelicaExternalC");
248-
annotation (__ModelicaAssociation_Impure=true,
249-
Documentation(info="<html>
242+
annotation (__ModelicaAssociation_Impure=true, Documentation(info="<html>
250243
<h4>Syntax</h4>
251244
<blockquote><pre>
252245
(ms, sec, min, hour, day, mon, year) = Internal.Time.<strong>getTime</strong>();
@@ -314,21 +307,25 @@ All returned values are of type Integer and have the following meaning:
314307
</html>"));
315308
end getTime;
316309

317-
function dayOfWeek "Return day of week for given date"
318-
extends Modelica.Icons.Function;
319-
input Integer year "Year";
320-
input Integer mon=1 "Month";
321-
input Integer day=1 "Day of month";
322-
output Integer dow "Day of week: 0 = Sunday, ..., 6 = Saturday";
323-
protected
324-
constant Integer t[:] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
325-
Integer y = year;
326-
algorithm
327-
assert(mon >= 1 and mon <= 12, "Month is out of range.");
328-
if mon < 3 then
329-
y := y - 1;
330-
end if;
331-
dow := mod(y + div(y, 4) - div(y, 100) + div(y, 400) + t[mon] + day, 7);
310+
function dayOfWeek "Return day of week for given date"
311+
extends Modelica.Icons.Function;
312+
input Integer year "Year";
313+
input Integer mon=1 "Month";
314+
input Integer day=1 "Day of month";
315+
output Integer dow(min=1, max=7) "Day of week: 1 = Monday, ..., 6 = Saturday, 7 = Sunday";
316+
protected
317+
constant Integer t[:] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
318+
Integer y = year;
319+
algorithm
320+
assert(mon >= 1 and mon <= 12, "Month is out of range.");
321+
if mon < 3 then
322+
y := y - 1;
323+
end if;
324+
dow := mod(y + div(y, 4) - div(y, 100) + div(y, 400) + t[mon] + day, 7);
325+
// One-based indexing: Sunday is 7
326+
if dow == 0 then
327+
dow := 7;
328+
end if;
332329
annotation (Documentation(info="<html>
333330
<h4>Syntax</h4>
334331
<blockquote><pre>
@@ -338,16 +335,14 @@ dow = Internal.Time.<strong>dayOfWeek</strong>(year, mon, day);
338335
<p>
339336
<p>
340337
Returns the day of the week for a given date using Tomohiko Sakamoto's algorithm.
341-
The returned Integer number of <code>dow</dow> has the following meaning:
338+
The returned Integer number of <code>dow</code> has the following meaning:
342339
</p>
343340
344341
<blockquote>
345342
<table border=1 cellspacing=0 cellpadding=2>
346343
<tr><th>Day of week</th>
347344
<th>Number</th></tr>
348345
349-
<tr><td>Sunday</td> <td>0</td></tr>
350-
351346
<tr><td>Monday</td> <td>1</td></tr>
352347
353348
<tr><td>Tuesday</td> <td>2</td></tr>
@@ -359,6 +354,9 @@ The returned Integer number of <code>dow</dow> has the following meaning:
359354
<tr><td>Friday</td> <td>5</td></tr>
360355
361356
<tr><td>Saturday</td> <td>6</td></tr>
357+
358+
<tr><td>Sunday</td> <td>7</td></tr>
359+
362360
</table>
363361
</blockquote>
364362
@@ -370,7 +368,7 @@ dow = dayOfWeek(2020) // = 3
370368
// Jan. 01, 2020 (New Year's Day) is a Wednesday
371369
</pre></blockquote>
372370
</html>"));
373-
end dayOfWeek;
371+
end dayOfWeek;
374372

375373
annotation (
376374
Documentation(info="<html>

Modelica/Utilities/Time.mo

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

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

ModelicaTest/Utilities.mo

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,7 @@ extends Modelica.Icons.ExamplesPackage;
384384
output Boolean ok;
385385
protected
386386
Modelica.Utilities.Types.TimeType now;
387-
Integer dow "Day of week";
388-
constant String weekDays[:] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
387+
Integer dow(min=1, max=7) "Day of week";
389388
algorithm
390389
Streams.print("... Test of Modelica.Utilities.Time");
391390
Streams.print("... Test of Modelica.Utilities.Time", logFile);
@@ -399,11 +398,11 @@ extends Modelica.Icons.ExamplesPackage;
399398
Streams.print(" mon = " + String(now.month));
400399
Streams.print(" year = " + String(now.year));
401400
dow := Modelica.Utilities.Time.dayOfWeek(now);
402-
Streams.print(" dow = " + weekDays[dow + 1]);
401+
Streams.print(" dow = " + Modelica.Utilities.Time.weekDays[dow]);
403402

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

408407
assert(not Modelica.Utilities.Time.isLeapYear(1900), "Time.isLeapYear failed");
409408
assert(Modelica.Utilities.Time.isLeapYear(2000), "Time.isLeapYear failed");

0 commit comments

Comments
 (0)