Skip to content

Commit

Permalink
Add wrappers for external functions
Browse files Browse the repository at this point in the history
Force consistency and make SimulationX happy
  • Loading branch information
beutlich committed Apr 13, 2020
1 parent 755a31b commit 6a63a1d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
42 changes: 40 additions & 2 deletions Modelica/Utilities/Internal.mo
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,25 @@ end FileSystem;

package Time "Internal package with external functions as interface to date and time"
extends Modelica.Icons.InternalPackage;

pure function diffTime "Convert local time to elapsed seconds since custom epoch year"
extends Modelica.Icons.Function;
input Integer ms "Millisecond";
input Integer sec "Second";
input Integer min "Minute";
input Integer hour "Hour";
input Integer day "Day";
input Integer mon "Month";
input Integer year "Year";
input Integer epoch_year = 1970 "Reference year";
output Real seconds "Elapsed seconds since epoch_year in the current time zone";
external "C" seconds = ModelicaTime_difftime(ms, sec, min, hour, day, mon, year, epoch_year)
annotation (IncludeDirectory="modelica://Modelica/Resources/C-Sources", Include="#include \"ModelicaTime.c\"");
annotation (Documentation(info="<html>
TODO
</html>"));
end diffTime;

impure function getTime "Retrieve the local time (in the local time zone)"
extends Modelica.Icons.Function;
output Integer ms "Millisecond";
Expand Down Expand Up @@ -342,7 +361,7 @@ TODO

pure function stringToTime "Retrieve the local time (in the local time zone) from formatted string"
extends Modelica.Icons.Function;
input String str "Formatted data and time string";
input String str "Formatted date and time string";
input String format = "%Y-%m-%d %H:%M:%S" "Format string passed to strptime";
output Integer ms "Millisecond";
output Integer sec "Second";
Expand All @@ -358,12 +377,31 @@ TODO
</html>"));
end stringToTime;

pure function timeToString "Convert the local time (in the local time zone) to string"
extends Modelica.Icons.Function;
input Integer ms "Millisecond";
input Integer sec "Second";
input Integer min "Minute";
input Integer hour "Hour";
input Integer day "Day";
input Integer mon "Month";
input Integer year "Year";
input String format = "%Y-%m-%d %H:%M:%S" "Format string passed to strftime";
input Integer maxSize = 128 "Maximal length of formatted string";
output String str "Formatted date and time string";
external "C99" str = ModelicaTime_strftime(ms, sec, min, hour, day, mon, year, format, maxSize)
annotation (IncludeDirectory="modelica://Modelica/Resources/C-Sources", Include="#include \"ModelicaTime.c\"");
annotation (Documentation(info="<html>
TODO
</html>"));
end timeToString;

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";
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;
Expand Down
25 changes: 15 additions & 10 deletions Modelica/Utilities/Time.mo
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ days = leapDays(2000, 2020) // = 5 leap days in range [2000, 2019]
import Modelica.Utilities.Internal.Time.stringToTime;
extends Function;

input String str "Formatted data and time string";
input String str "Formatted date and time string";
input String format = "%Y-%m-%d %H:%M:%S" "Format string passed to strptime";
output DateTime dt "Date and time";

Expand Down Expand Up @@ -290,16 +290,18 @@ days = leapDays(2000, 2020) // = 5 leap days in range [2000, 2019]
import Modelica.Utilities.Time.DateTime;
import Modelica.Icons.Function;

pure function formatted "Use strftime conversion to format a DateTime record as string"
function formatted "Use strftime conversion to format a DateTime record as string"
import Modelica.Utilities.Internal.Time.timeToString;
extends Function;

input DateTime dt "Date and time";
input String format = "%Y-%m-%d %H:%M:%S" "Format string passed to strftime";
input Integer maxSize = 128 "Maximal length of formatted string";
output String str "Formatted data and time string";
external "C99" str = ModelicaTime_strftime(dt.millisecond, dt.second, dt.minute, dt.hour, dt.day, dt.month, dt.year, format, maxSize)
annotation (IncludeDirectory="modelica://Modelica/Resources/C-Sources", Include="#include \"ModelicaTime.c\"");
annotation (Documentation(info="<html>
output String str "Formatted date and time string";

algorithm
str := timeToString(dt.millisecond, dt.second, dt.minute, dt.hour, dt.day, dt.month, dt.year, format, maxSize);
annotation (Inline=true, Documentation(info="<html>
<h4>Syntax</h4>
<blockquote>
<pre>
Expand Down Expand Up @@ -584,17 +586,19 @@ String(dt, format=\"%A, %d. %B %y, %H:%M:%S.%L\") // = \"Thursday, 24. December
points={{-50,0},{50,0}})}));
end '-';

encapsulated pure function epoch "Convert DateTime to elapsed seconds since custom epoch year"
encapsulated function epoch "Convert DateTime to elapsed seconds since custom epoch year"
import Modelica.Utilities.Time.DateTime;
import Modelica.Utilities.Internal.Time.diffTime;
import Modelica.Icons.Function;
extends Function;

input DateTime dt "Date and time";
input Integer epoch_year = 1970 "Reference year";
output Real seconds "Elapsed seconds since epoch_year in the current time zone";

external "C" seconds = ModelicaTime_difftime(dt.millisecond, dt.second, dt.minute, dt.hour, dt.day, dt.month, dt.year, epoch_year)
annotation (IncludeDirectory="modelica://Modelica/Resources/C-Sources", Include="#include \"ModelicaTime.c\"");
algorithm
seconds := diffTime(dt.millisecond, dt.second, dt.minute, dt.hour, dt.day, dt.month, dt.year, epoch_year);
annotation (Inline=true);
end epoch;

encapsulated function now "Get current system date and time as DateTime"
Expand All @@ -604,7 +608,8 @@ String(dt, format=\"%A, %d. %B %y, %H:%M:%S.%L\") // = \"Thursday, 24. December

output DateTime now "Current date and time";
algorithm
now := DateTime.'constructor'.fromSystemTime();
now := DateTime.'constructor'.fromSystemTime();
annotation (Inline=true);
end now;

annotation (Documentation(info="<html>
Expand Down

0 comments on commit 6a63a1d

Please sign in to comment.