Skip to content

Add date and time utilities #3247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 66 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
ea27141
Add time utilities
beutlich Dec 2, 2019
ea7edc3
Add leap year utility functions
beutlich Dec 2, 2019
ca5c100
Rename TimeType components
beutlich Dec 5, 2019
c97323e
Add day of week computation
beutlich Dec 6, 2019
39c2930
Let dayOfWeek return Integer in range [1, 7]
beutlich Dec 10, 2019
f0a9871
Add function to round to nearest integer
m-kessler Apr 8, 2020
7495e30
Add arrays for month and day names (full and abbreviated)
m-kessler Apr 9, 2020
b5cd65d
Integrate DateTime record from Testing lib
m-kessler Apr 7, 2020
cf42eab
Integrate Duration record from Testing lib
m-kessler Apr 7, 2020
d33b461
Rename variables in DateTime (s -> second, ...)
m-kessler Apr 8, 2020
a7fdfb3
Rename seconds to totalSeconds in Duration
m-kessler Apr 8, 2020
0feec0e
Rename variables in Duration (s -> seconds, ...)
m-kessler Apr 8, 2020
2fe519e
Support '-' for DateTime
m-kessler Apr 8, 2020
c4fbfc0
Add DateTime.now()
m-kessler Apr 9, 2020
5388602
Add tests for Duration
m-kessler Apr 8, 2020
a314ccb
Add tests for DateTime
m-kessler Apr 8, 2020
1f0aa17
Format DateTime like strftime in C++ and Python
m-kessler Apr 9, 2020
d02886d
Align string formatting for Duration with DateTime
m-kessler Apr 9, 2020
d64accb
Add class comments to DateTime and all its operators
m-kessler Apr 9, 2020
3cd6124
Add class comments to Duration and all its operators
m-kessler Apr 9, 2020
346bd71
Use dt as variable name for DateTimes
m-kessler Apr 9, 2020
af2a09c
Use d as variable name for Durations
m-kessler Apr 9, 2020
3cc5052
Add basic doc for DateTime
m-kessler Apr 9, 2020
6cabef7
Add basic doc for Duration
m-kessler Apr 9, 2020
42c5237
Fix lookup of package constant
beutlich Apr 9, 2020
e379e43
Fix HTML tags and double quotes
beutlich Apr 9, 2020
0e18b43
Can use Modelica.Math.nearestInteger
beutlich Apr 10, 2020
de1f033
Remove TimeType in favor of DateTime
beutlich Apr 10, 2020
2616e2d
Fix icon inheritance
beutlich Apr 10, 2020
c8078e0
Rewrite using external functions from ModelicaTime.c
beutlich Apr 11, 2020
c725aad
Reintroduce handling of milliseconds
beutlich Apr 13, 2020
128907e
Utilize locale-independent string conversion
beutlich Apr 13, 2020
ceb93cc
Fix C++ compilation
beutlich Apr 13, 2020
ddf7c19
Update UsersGuide and package doc
beutlich Apr 13, 2020
6135677
Add wrappers for external functions
beutlich Apr 13, 2020
f99dc0d
Use horizontal ellipsis
beutlich Apr 13, 2020
b552cef
Avoid limitation of Duration.'String'.formatted
beutlich Apr 13, 2020
957cc39
Fix example for string formatting of Duration
m-kessler Apr 14, 2020
ac44252
Make Duration constructors unambiguous
m-kessler Apr 14, 2020
e55abd5
Fix more corner cases of ModelicaTime_difftime
beutlich Apr 14, 2020
f7fa01d
Fix consideration of DST
beutlich Apr 14, 2020
742dde0
Fix corner case
beutlich Apr 15, 2020
c4a8a44
Fix subtraction test of DateTime
m-kessler Apr 15, 2020
a9073f1
Add tests for nearestInteger
m-kessler Apr 15, 2020
1ce4579
Fix naming in nearestInteger tests
m-kessler Apr 15, 2020
1c9814c
Implement test for Duration.'0'
beutlich Apr 15, 2020
78283c9
Fix ModelicaTest.Utilities.Time
beutlich Apr 15, 2020
872188e
Fix Duration.'constructor'.fromDateTimes for negative time span
beutlich Apr 17, 2020
9c8090d
Add another test for negative durations
m-kessler Apr 20, 2020
e66052b
Support + and - for DateTime with Duration
m-kessler Apr 20, 2020
f1673fb
Optionally skip DateTime tests for years before 1970
m-kessler Apr 20, 2020
f88e295
Use class for operator record icon
m-kessler May 19, 2020
ef1c6e9
Use proper comments for operators
m-kessler May 20, 2020
5b05f8b
Update description
beutlich Oct 27, 2020
ee54ab3
Remove C99 standard version specifier
beutlich Oct 27, 2020
57ec62f
Remove documentation of border cases
beutlich Oct 29, 2020
99a7ca3
Improve return value handling of localtime
beutlich Oct 29, 2020
55cf27b
Exclude past tests
beutlich Oct 29, 2020
ec34d6e
Add some tests for CI
beutlich Oct 29, 2020
e928d7f
Migrate conversion to next major version
beutlich Jan 18, 2021
8edddc2
Move third-party license files
beutlich Oct 27, 2024
5a961ef
Update CMake configuration
beutlich Oct 27, 2024
040080f
Fix build errors
beutlich Oct 27, 2024
462fcf7
Bump copyright year
beutlich Oct 27, 2024
479e4a9
Add include guards
beutlich Oct 27, 2024
f7be3cc
Update include annotations
beutlich Oct 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .CI/Test/ModelicaTime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "../../Modelica/Resources/C-Sources/ModelicaTime.h"
#include "Common.c"

#include <assert.h>
#include <string.h>

int main(int argc, char **argv) {
int ms = 501;
int sec = 30;
int min = 44;
int hour = 20;
int mday = 29;
int mon = 10;
int year = 2020;
const char *format = "%Y-%m-%d %H:%M:%S.%L";
int maxSize = 128;
const char *dateStr = ModelicaTime_strftime(ms, sec, min, hour, mday, mon, year, format, maxSize);
ModelicaFormatMessage("Date: %s\n", dateStr);
assert(0 == strcmp("2020-10-29 20:44:30.501", dateStr));
ModelicaTime_strptime(&ms, &sec, &min, &hour, &mday, &mon, &year, "2000-09-30 06:45:01.123", format);
assert(ms == 123);
assert(sec == 1);
assert(min == 45);
assert(hour == 6);
assert(mday == 30);
assert(mon == 9);
assert(year == 2000);
return 0;
}
4 changes: 4 additions & 0 deletions .CI/Test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ if test ! "$1" = "nostatic"; then
./a.out || exit 1
"$CC" -L $LIBRARIES ModelicaStrings.c -Wl,-Bstatic -lModelicaExternalC -Wl,-Bdynamic || exit 1
./a.out || exit 1
"$CC" -L $LIBRARIES ModelicaTime.c -Wl,-Bstatic -lModelicaExternalC -Wl,-Bdynamic || exit 1
./a.out || exit 1
"$CC" -L $LIBRARIES Streams.c -Wl,-Bstatic -lModelicaExternalC -Wl,-Bdynamic || exit 1
./a.out || exit 1
"$CC" -L $LIBRARIES Tables.c -Wl,-Bstatic -lModelicaStandardTables -lModelicaIO -lModelicaMatIO -lzlib -Wl,-Bdynamic -lm || exit 1
Expand All @@ -31,6 +33,8 @@ if test ! "$1" = "onlystatic"; then
./a.out || exit 1
"$CC" -L $LIBRARIES -Wl,-rpath $LIBRARIES ModelicaStrings.c -lModelicaExternalC || exit 1
./a.out || exit 1
"$CC" -L $LIBRARIES -Wl,-rpath $LIBRARIES ModelicaTime.c -lModelicaExternalC || exit 1
./a.out || exit 1
"$CC" -L $LIBRARIES -Wl,-rpath $LIBRARIES Streams.c -lModelicaExternalC || exit 1
./a.out || exit 1
"$CC" -L $LIBRARIES -Wl,-rpath $LIBRARIES Tables.c -lModelicaStandardTables -lModelicaIO -lModelicaMatIO || exit 1
Expand Down
13 changes: 10 additions & 3 deletions Complex.mo
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ operator record Complex "Complex number with overloaded operators"
end fromReal;
annotation (Documentation(info="<html>
<p>Here the constructor operator(s) is/are defined.</p>
</html>"), Icon(graphics={Rectangle(
</html>"), Icon(
graphics={
Rectangle(
lineColor={200,200,200},
fillColor={248,248,248},
fillPattern=FillPattern.HorizontalCylinder,
extent={{-100,-100},{100,100}},
radius=25.0), Rectangle(
radius=25.0),
Rectangle(
lineColor={128,128,128},
extent={{-100,-100},{100,100}},
radius=25.0)}));
radius=25.0),
Text(
textColor={128,128,128},
extent={{-90,-90},{90,90}},
textString="f")}));
end 'constructor';

encapsulated operator function '0' "Zero-element of addition (= Complex(0))"
Expand Down
8 changes: 4 additions & 4 deletions Modelica/Blocks/Math.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,7 @@ Otherwise the input angle <code>u</code> is wrapped to the <a href=\"https://en.
Interfaces.IntegerOutput y "Connector of Integer output signal" annotation (
Placement(transformation(extent={{100,-10},{120,10}})));
equation
y = if (u > 0) then integer(floor(u + 0.5)) else integer(ceil(u - 0.5));
y = Modelica.Math.nearestInteger(u);
annotation (Icon(coordinateSystem(
preserveAspectRatio=true,
extent={{-100.0,-100.0},{100.0,100.0}}), graphics={
Expand All @@ -1959,11 +1959,11 @@ Otherwise the input angle <code>u</code> is wrapped to the <a href=\"https://en.
30.0,-10.0},{30.0,-20.0},{50.0,0.0}})}), Documentation(info="<html>
<p>
This block computes the output <strong>y</strong>
as <em>nearest integer value</em> of the input <strong>u</strong>:
as <em>nearest integer value</em> of the input <strong>u</strong> utilizing the
function <a href=\"modelica://Modelica.Math.nearestInteger\">nearestInteger</a>:
</p>
<blockquote><pre>
y = <strong>integer</strong>( <strong>floor</strong>( u + 0.5 ) ) for u &gt; 0;
y = <strong>integer</strong>( <strong>ceil </strong>( u - 0.5 ) ) for u &lt; 0;
y = Modelica.Math.<strong>nearestInteger</strong>(u);
</pre></blockquote>
</html>"));
end RealToInteger;
Expand Down
47 changes: 39 additions & 8 deletions Modelica/Icons.mo
Original file line number Diff line number Diff line change
Expand Up @@ -527,29 +527,60 @@ This icon is designed for a <strong>translational sensor</strong> model.
extent={{-150,60},{150,100}},
textString="%name"),
Rectangle(
origin={0.0,-25.0},
origin={0,-25},
lineColor={64,64,64},
fillColor={255,215,136},
fillPattern=FillPattern.Solid,
extent={{-100.0,-75.0},{100.0,75.0}},
radius=25.0),
extent={{-100,-75},{100,75}},
radius=25),
Line(
points={{-100.0,0.0},{100.0,0.0}},
points={{-100,0},{100,0}},
color={64,64,64}),
Line(
origin={0.0,-50.0},
points={{-100.0,0.0},{100.0,0.0}},
origin={0,-50},
points={{-100,0},{100,0}},
color={64,64,64}),
Line(
origin={0.0,-25.0},
points={{0.0,75.0},{0.0,-75.0}},
origin={0,-25},
points={{0,75},{0,-75}},
color={64,64,64})}), Documentation(info="<html>
<p>
This icon is indicates a record.
</p>
</html>"));
end Record;

partial class OperatorRecord "Icon for operator records"

annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{100,100}}), graphics={
Text(
textColor={0,0,255},
extent={{-150,60},{150,100}},
textString="%name"),
Rectangle(
origin={0,-25},
lineColor={64,64,64},
fillColor={255,215,136},
fillPattern=FillPattern.Solid,
extent={{-100,-75},{100,75}},
radius=25),
Line(
points={{-100,0},{100,0}},
color={64,64,64}),
Line(
origin={0,-50},
points={{-100,0},{100,0}},
color={64,64,64}),
Line(
origin={0,-25},
points={{0,75},{0,-75}},
color={64,64,64})}), Documentation(info="<html>
<p>
This icon is indicates an operator record.
</p>
</html>"));
end OperatorRecord;

type TypeReal "Icon for Real types"
extends Real;
annotation(Icon(coordinateSystem(preserveAspectRatio=false, extent={{-100,-100},{100,100}}), graphics={
Expand Down
44 changes: 44 additions & 0 deletions Modelica/Math/nearestInteger.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
within Modelica.Math;
function nearestInteger "Convert real number to nearest integer value"
extends Modelica.Icons.Function;

input Real r "Real number to convert to integer";
output Integer i "Integer value, which is closest to the given real number";

algorithm
i :=if (r > 0) then integer(floor(r + 0.5)) else integer(ceil(r - 0.5));

annotation (Documentation(info="<html>

<h4>Syntax</h4>
<blockquote><pre>
Math.<strong>nearestInteger</strong>(r);
</pre></blockquote>

<h4>Description</h4>
<p>
The input value \"r\" of type Real is converted to the closest Integer value \"i\",
using the <em>round half away from zero</em> rule with the equation:
</p>
<blockquote><pre>
i = <strong>integer</strong>( <strong>floor</strong>( r + 0.5 ) ) for r &gt; 0;
i = <strong>integer</strong>( <strong>ceil</strong>( r - 0.5 ) ) for r &lt; 0;
</pre></blockquote>

<h4>Example</h4>
<blockquote><pre>
import Modelica.Math;
Math.nearestInteger(0.4); // = 0
Math.nearestInteger(0.5); // = 1
Math.nearestInteger(-0.4); // = 0
Math.nearestInteger(-0.5); // = -1
</pre></blockquote>

<h4>Note</h4>

<p>
This function does the same conversion as the block
<a href=\"modelica://Modelica.Blocks.Math.RealToInteger\">RealToInteger</a>.
</p>
</html>"));
end nearestInteger;
1 change: 1 addition & 0 deletions Modelica/Math/package.order
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ FastFourierTransform
Icons
isEqual
isPowerOf2
nearestInteger
sin
cos
tan
Expand Down
4 changes: 4 additions & 0 deletions Modelica/Resources/BuildProjects/CMake/src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ set(EXTC_SOURCES
"${MODELICA_SOURCE_DIR}/ModelicaRandom.h"
"${MODELICA_SOURCE_DIR}/ModelicaStrings.c"
"${MODELICA_SOURCE_DIR}/ModelicaStrings.h"
"${MODELICA_SOURCE_DIR}/ModelicaTime.c"
"${MODELICA_SOURCE_DIR}/ModelicaTime.h"
"${MODELICA_SOURCE_DIR}/gconstructor.h"
"${MODELICA_SOURCE_DIR}/repl_str.h"
"${MODELICA_SOURCE_DIR}/stdint_msvc.h"
"${MODELICA_SOURCE_DIR}/stdint_wrap.h"
"${MODELICA_SOURCE_DIR}/strptime.h"
"${MODELICA_SOURCE_DIR}/uthash.h"
"${MODELICA_SOURCE_DIR}/win32_dirent.c"
"${MODELICA_SOURCE_DIR}/win32_dirent.h"
Expand Down
1 change: 1 addition & 0 deletions Modelica/Resources/BuildProjects/CMake/test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ if(MODELICA_BUILD_TESTING)
set(MODELICA_TESTS
FileSystem
ModelicaStrings
ModelicaTime
Streams
Tables
TablesFromCsvFile
Expand Down
2 changes: 1 addition & 1 deletion Modelica/Resources/BuildProjects/autotools/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lib_LTLIBRARIES = libzlib.la libModelicaExternalC.la libModelicaMatIO.la libModelicaIO.la libModelicaStandardTables.la
libModelicaExternalC_la_SOURCES = ../../C-Sources/ModelicaFFT.c ../../C-Sources/ModelicaInternal.c ../../C-Sources/ModelicaRandom.c ../../C-Sources/ModelicaStrings.c
libModelicaExternalC_la_SOURCES = ../../C-Sources/ModelicaFFT.c ../../C-Sources/ModelicaInternal.c ../../C-Sources/ModelicaRandom.c ../../C-Sources/ModelicaStrings.c ../../C-Sources/ModelicaTime.c
libModelicaExternalC_la_LIBADD = @LIBMATH@
libModelicaIO_la_SOURCES = ../../C-Sources/ModelicaIO.c
libModelicaIO_la_LIBADD = libModelicaMatIO.la
Expand Down
6 changes: 5 additions & 1 deletion Modelica/Resources/BuildProjects/gcc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ EXTC_OBJS = \
ModelicaFFT.o \
ModelicaInternal.o \
ModelicaRandom.o \
ModelicaStrings.o
ModelicaStrings.o \
ModelicaTime.o

TABLES_OBJS = \
ModelicaStandardTables.o \
Expand Down Expand Up @@ -93,6 +94,9 @@ ModelicaRandom.o: ../../C-Sources/ModelicaRandom.c
ModelicaStrings.o: ../../C-Sources/ModelicaStrings.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(INC) -c -o $@ $<

ModelicaTime.o: ../../C-Sources/ModelicaTime.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(INC) -c -o $@ $<

%.o: ../../C-Sources/zlib/%.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(INC) -c -o $@ $<

Expand Down
Loading