Skip to content

Commit 2e8f7ec

Browse files
committed
Add day of week computation
1 parent c3f7610 commit 2e8f7ec

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
@@ -321,6 +321,65 @@ All returned values are of type Integer and have the following meaning:
321321
</table>
322322
</html>"));
323323
end getTime;
324+
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);
340+
annotation (Documentation(info="<html>
341+
<h4>Syntax</h4>
342+
<blockquote><pre>
343+
dow = Internal.Time.<strong>dayOfWeek</strong>(year, mon, day);
344+
</pre></blockquote>
345+
<h4>Description</h4>
346+
<p>
347+
<p>
348+
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:
350+
</p>
351+
352+
<blockquote>
353+
<table border=1 cellspacing=0 cellpadding=2>
354+
<tr><th>Day of week</th>
355+
<th>Number</th></tr>
356+
357+
<tr><td>Sunday</td> <td>0</td></tr>
358+
359+
<tr><td>Monday</td> <td>1</td></tr>
360+
361+
<tr><td>Tuesday</td> <td>2</td></tr>
362+
363+
<tr><td>Wednesday</td> <td>3</td></tr>
364+
365+
<tr><td>Thursday</td> <td>4</td></tr>
366+
367+
<tr><td>Friday</td> <td>5</td></tr>
368+
369+
<tr><td>Saturday</td> <td>6</td></tr>
370+
</table>
371+
</blockquote>
372+
373+
<h4>Example</h4>
374+
<blockquote><pre>
375+
dow = dayOfWeek(2019, 12, 6) // = 5
376+
// Dec. 06, 2019 (Saint Nicholas Day) is a Friday
377+
dow = dayOfWeek(2020) // = 3
378+
// Jan. 01, 2020 (New Year's Day) is a Wednesday
379+
</pre></blockquote>
380+
</html>"));
381+
end dayOfWeek;
382+
324383
annotation (
325384
Documentation(info="<html>
326385
<p>

Modelica/Utilities/Time.mo

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

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

ModelicaTest/Utilities.mo

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ 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"};
391393
algorithm
392394
Streams.print("... Test of Modelica.Utilities.Time");
393395
Streams.print("... Test of Modelica.Utilities.Time", logFile);
@@ -400,6 +402,12 @@ extends Modelica.Icons.ExamplesPackage;
400402
Streams.print(" day = " + String(now.day));
401403
Streams.print(" mon = " + String(now.month));
402404
Streams.print(" year = " + String(now.year));
405+
dow := Modelica.Utilities.Time.dayOfWeek(now);
406+
Streams.print(" dow = " + weekDays[dow + 1]);
407+
408+
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");
403411

404412
assert(not Modelica.Utilities.Time.isLeapYear(1900), "Time.isLeapYear failed");
405413
assert(Modelica.Utilities.Time.isLeapYear(2000), "Time.isLeapYear failed");

0 commit comments

Comments
 (0)