Skip to content

Commit 6e961b5

Browse files
committed
Add day of week computation
1 parent eeeee81 commit 6e961b5

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

Modelica/Utilities/Internal.mo

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,65 @@ All returned values are of type Integer and have the following meaning:
313313
</table>
314314
</html>"));
315315
end getTime;
316+
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);
332+
annotation (Documentation(info="<html>
333+
<h4>Syntax</h4>
334+
<blockquote><pre>
335+
dow = Internal.Time.<strong>dayOfWeek</strong>(year, mon, day);
336+
</pre></blockquote>
337+
<h4>Description</h4>
338+
<p>
339+
<p>
340+
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:
342+
</p>
343+
344+
<blockquote>
345+
<table border=1 cellspacing=0 cellpadding=2>
346+
<tr><th>Day of week</th>
347+
<th>Number</th></tr>
348+
349+
<tr><td>Sunday</td> <td>0</td></tr>
350+
351+
<tr><td>Monday</td> <td>1</td></tr>
352+
353+
<tr><td>Tuesday</td> <td>2</td></tr>
354+
355+
<tr><td>Wednesday</td> <td>3</td></tr>
356+
357+
<tr><td>Thursday</td> <td>4</td></tr>
358+
359+
<tr><td>Friday</td> <td>5</td></tr>
360+
361+
<tr><td>Saturday</td> <td>6</td></tr>
362+
</table>
363+
</blockquote>
364+
365+
<h4>Example</h4>
366+
<blockquote><pre>
367+
dow = dayOfWeek(2019, 12, 6) // = 5
368+
// Dec. 06, 2019 (Saint Nicholas Day) is a Friday
369+
dow = dayOfWeek(2020) // = 3
370+
// Jan. 01, 2020 (New Year's Day) is a Wednesday
371+
</pre></blockquote>
372+
</html>"));
373+
end dayOfWeek;
374+
316375
annotation (
317376
Documentation(info="<html>
318377
<p>

Modelica/Utilities/Time.mo

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,54 @@ now = getTime() // = Modelica.Utilities.Types.TimeType(281, 30, 13, 10, 15, 2,
2929
</html>"));
3030
end getTime;
3131

32+
function dayOfWeek "Return day of week for given date"
33+
extends Modelica.Icons.Function;
34+
input Types.TimeType timeIn "Date";
35+
output Integer dow "Day of week: 0 = Sunday, ..., 6 = Saturday";
36+
algorithm
37+
dow := Internal.Time.dayOfWeek(timeIn.year, timeIn.month, timeIn.day);
38+
annotation (Documentation(info="<html>
39+
<h4>Syntax</h4>
40+
<blockquote><pre>
41+
dow = Time.<strong>dayOfWeek</strong>(timeIn);
42+
</pre></blockquote>
43+
<h4>Description</h4>
44+
<p>
45+
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:
47+
</p>
48+
49+
<blockquote>
50+
<table border=1 cellspacing=0 cellpadding=2>
51+
<tr><th>Day of week</th>
52+
<th>Number</th></tr>
53+
54+
<tr><td>Sunday</td> <td>0</td></tr>
55+
56+
<tr><td>Monday</td> <td>1</td></tr>
57+
58+
<tr><td>Tuesday</td> <td>2</td></tr>
59+
60+
<tr><td>Wednesday</td> <td>3</td></tr>
61+
62+
<tr><td>Thursday</td> <td>4</td></tr>
63+
64+
<tr><td>Friday</td> <td>5</td></tr>
65+
66+
<tr><td>Saturday</td> <td>6</td></tr>
67+
</table>
68+
</blockquote>
69+
70+
<h4>Example</h4>
71+
<blockquote><pre>
72+
now = getTime() // = Modelica.Utilities.Types.TimeType(281, 30, 13, 10, 6, 12, 2019)
73+
// Dec. 06, 2019 at 10:13 after 30.281 s
74+
dow = dayOfWeek(now) // = 5
75+
// Dec. 06, 2019 (Saint Nicholas Day) is a Friday
76+
</pre></blockquote>
77+
</html>"));
78+
end dayOfWeek;
79+
3280
function isLeapYear "Check if a year is a leap year"
3381
extends Modelica.Icons.Function;
3482
input Integer year "Year";

ModelicaTest/Utilities.mo

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ 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"};
387389
algorithm
388390
Streams.print("... Test of Modelica.Utilities.Time");
389391
Streams.print("... Test of Modelica.Utilities.Time", logFile);
@@ -396,6 +398,12 @@ extends Modelica.Icons.ExamplesPackage;
396398
Streams.print(" day = " + String(now.day));
397399
Streams.print(" mon = " + String(now.month));
398400
Streams.print(" year = " + String(now.year));
401+
dow := Modelica.Utilities.Time.dayOfWeek(now);
402+
Streams.print(" dow = " + weekDays[dow + 1]);
403+
404+
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");
399407

400408
assert(not Modelica.Utilities.Time.isLeapYear(1900), "Time.isLeapYear failed");
401409
assert(Modelica.Utilities.Time.isLeapYear(2000), "Time.isLeapYear failed");

0 commit comments

Comments
 (0)