Modelica print current time

Viewed 383

How can I print the current date and/or time to a file (e.g. log file, or csv file) from Modelica? Do I need external code for this? I was not able to find any example code in the Modelica Standard Library.

2 Answers

https://build.openmodelica.org/Documentation/Modelica.Utilities.Streams.print.html

You would need to add this to your equation or algorithm section:

.Modelica.Utilities.Streams.print(String(time));

For local system time use: https://build.openmodelica.org/Documentation/Modelica.Utilities.System.getTime.html

model GetTime
  Integer ms;
  Integer sec;
  Integer min;
  Integer hour;
  Integer mday;
  Integer mon;
  Integer year;
algorithm
  (ms, sec, min, hour, mday, mon, year) := .Modelica.Utilities.System.getTime();
  .Modelica.Utilities.Streams.print("ms:" + String(ms) + "\n");
  .Modelica.Utilities.Streams.print("sec:" + String(sec) + "\n");
  .Modelica.Utilities.Streams.print("min:" + String(min) + "\n");
  .Modelica.Utilities.Streams.print("hour:" + String(hour) + "\n");
  .Modelica.Utilities.Streams.print("mday:" + String(mday) + "\n");
  .Modelica.Utilities.Streams.print("mon:" + String(mon) + "\n");
  .Modelica.Utilities.Streams.print("year:" + String(year) + "\n");
end GetTime;

Since v1.1 the Testing library (shipped with Dymola 2019) contains the operator record DateTime.

There are several constructors for the operator record. If no further arguments are given, the system time is used. Here is an example:

> dt =Testing.Utilities.Time.DateTime()   // use getTime() to create the operator record
> dt.a                                    // access one of the variables of the operator record
= 2019
> String(dt)                              // convert to string using default format
= "2019-10-14 12:31:50"
> String(dt, format="%Hh %MINmin %Ss")    // convert to string using custom format
= "12h 35min 12s"

Durations is another operator record in the Testing library, which can deal with time spans.

Related