This package contains models for time.
This blocks computes the unix time stamp, date and time
and the day of the week based on the Modelica
variable time
.
As for the weather data reader
Buildings.BoundaryConditions.WeatherData.ReaderTMY3,
time=0
corresponds to January 1st at midnight
in the local time zone.
The computed outputs are thus also for the local time zone.
The year for which time=0
is determined by
the parameter zerTim
.
The unix time stamp corresponding to the current time is computed.
From this variables the corresponding, year, date and time are computed using functions
such as floor()
and ceil()
.
The implementation only supports date computations from year 2010 up to and including 2020.
Daylight saving is not supported.
The model was implemented such that no events are being generated for computing the minute of the day.
The model also contains an implementation for setting time=0
for any day and month other than January first.
This is however not activated in the current model since these options may wrongly give the impression
that it changes the time based on which the solar position is computed and TMY3 data are read.
model CalendarTime
extends Modelica.Blocks.Icons.DiscreteBlock;
parameter Buildings.Utilities.Time.Types.ZeroTime zerTim
;
parameter Integer yearRef(min=firstYear, max=lastYear) = 2016
;
parameter Boolean outputUnixTimeStamp = false
;
parameter Modelica.Units.SI.Time timZon(displayUnit="h") = 0
;
parameter Modelica.Units.SI.Time offset(displayUnit="h") = 0
;
Modelica.Blocks.Interfaces.RealOutput unixTimeStampLocal(
final unit="s")
;
Modelica.Blocks.Interfaces.RealOutput unixTimeStamp(
final unit="s") = unixTimeStampLocal - timZon
if outputUnixTimeStamp
;
discrete Modelica.Blocks.Interfaces.IntegerOutput year ;
discrete Modelica.Blocks.Interfaces.IntegerOutput month ;
Modelica.Blocks.Interfaces.IntegerOutput day(fixed=false) ;
Modelica.Blocks.Interfaces.IntegerOutput hour(fixed=false) ;
Modelica.Blocks.Interfaces.RealOutput minute ;
Modelica.Blocks.Interfaces.IntegerOutput weekDay(fixed=false)
;
protected
final constant Real eps_time(
final unit="s") = 1 ;
final constant Integer firstYear = 2010
;
final constant Integer lastYear = firstYear +
size(timeStampsNewYear,1) - 1;
constant Modelica.Units.SI.Time timeStampsNewYear[42]={1262304000.0,
1293840000.0,1325376000.0,1356998400.0,1388534400.0,1420070400.0,
1451606400.0,1483228800.0,1514764800.0,1546300800.0,1577836800.0,
1609459200.0,1640995200.0,1672531200.0,1704067200.0,1735689600.0,
1767225600.0,1798761600.0,1830297600.0,1861920000.0,1893456000.0,
1924992000.0,1956528000.0,1988150400.0,2019686400.0,2051222400.0,
2082758400.0,2114380800.0,2145916800.0,2177452800.0,2208988800.0,
2240611200.0,2272147200.0,2303683200.0,2335219200.0,2366841600.0,
2398377600.0,2429913600.0,2461449600.0,2493072000.0,2524608000.0,
2556144000.0} ;
constant Boolean isLeapYear[41] = {
false, false, true, false,
false, false, true, false,
false, false, true, false,
false, false, true, false,
false, false, true, false,
false, false, true, false,
false, false, true, false,
false, false, true, false,
false, false, true, false,
false, false, true, false,
false}
;
final constant Integer dayInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
;
parameter Modelica.Units.SI.Time timOff(fixed=false) ;
final constant Integer monthRef(min=1, max=12) = 1 ;
final constant Integer dayRef(min=1, max=31) = 1 ;
Integer daysSinceEpoch(fixed=false) ;
discrete Integer yearIndex ;
discrete Real epochLastMonth
;
final parameter Modelica.Units.SI.Time hourSampleStart(fixed=false)
;
final parameter Modelica.Units.SI.Time daySampleStart(fixed=false)
;
Boolean hourSampleTrigger ;
Boolean daySampleTrigger ;
Boolean firstHourSampling(fixed=true, start=true)
;
Boolean firstDaySampling(fixed=true, start=true)
;
initial equation
hourSampleStart =
integer(time/3600)*3600 - offset;
daySampleStart =
integer(time/(3600*24))*3600*24 - offset;
hour =
integer(
floor(
rem(unixTimeStampLocal,3600*24)/3600));
daysSinceEpoch =
integer(
floor(unixTimeStampLocal/3600/24));
day =
integer(1+
floor((unixTimeStampLocal-epochLastMonth)/3600/24));
weekDay =
integer(
rem(4+daysSinceEpoch-1,7)+1);
initial algorithm
assert(
not zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
or yearRef>=firstYear
and yearRef<=lastYear,
"The value you chose for yearRef (=" +
String(yearRef) + ") is outside of
the validity range of " +
String(firstYear) + " to " +
String(lastYear) + ".");
assert(
not zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
or dayInMonth[monthRef] + (
if monthRef==2
and isLeapYear[yearRef-firstYear + 1]
then 1
else 0) >=dayRef,
"The day number you chose is larger than the number of days contained by the month you chose.");
if zerTim == Buildings.Utilities.Time.Types.ZeroTime.UnixTimeStamp
then
timOff :=0;
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.UnixTimeStampGMT
then
timOff :=timZon;
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2010
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2010
then
timOff :=timeStampsNewYear[1];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2011
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2011
then
timOff :=timeStampsNewYear[2];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2012
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2012
then
timOff :=timeStampsNewYear[3];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2013
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2013
then
timOff :=timeStampsNewYear[4];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2014
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2014
then
timOff :=timeStampsNewYear[5];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2015
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2015
then
timOff :=timeStampsNewYear[6];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2016
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2016
then
timOff :=timeStampsNewYear[7];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2017
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2017
then
timOff :=timeStampsNewYear[8];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2018
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2018
then
timOff :=timeStampsNewYear[9];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2019
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2019
then
timOff :=timeStampsNewYear[10];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2020
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2020
then
timOff :=timeStampsNewYear[11];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2021
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2021
then
timOff :=timeStampsNewYear[12];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2022
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2022
then
timOff :=timeStampsNewYear[13];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2023
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2023
then
timOff :=timeStampsNewYear[14];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2024
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2024
then
timOff := timeStampsNewYear[14];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2025
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2025
then
timOff := timeStampsNewYear[15];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2026
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2026
then
timOff := timeStampsNewYear[16];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2027
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2027
then
timOff := timeStampsNewYear[17];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2028
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2028
then
timOff := timeStampsNewYear[18];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2029
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2029
then
timOff := timeStampsNewYear[19];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2030
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2030
then
timOff := timeStampsNewYear[20];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2031
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2031
then
timOff := timeStampsNewYear[21];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2032
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2032
then
timOff := timeStampsNewYear[22];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2033
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2033
then
timOff := timeStampsNewYear[23];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2034
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2034
then
timOff := timeStampsNewYear[24];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2035
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2035
then
timOff := timeStampsNewYear[25];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2036
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2036
then
timOff := timeStampsNewYear[26];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2037
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2037
then
timOff := timeStampsNewYear[27];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2038
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2038
then
timOff := timeStampsNewYear[28];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2039
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2039
then
timOff := timeStampsNewYear[29];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2040
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2040
then
timOff := timeStampsNewYear[30];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2041
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2041
then
timOff := timeStampsNewYear[31];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2042
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2042
then
timOff := timeStampsNewYear[32];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2043
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2043
then
timOff := timeStampsNewYear[33];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2044
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2044
then
timOff := timeStampsNewYear[34];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2045
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2045
then
timOff := timeStampsNewYear[35];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2046
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2046
then
timOff := timeStampsNewYear[36];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2047
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2047
then
timOff := timeStampsNewYear[37];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2048
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2048
then
timOff := timeStampsNewYear[38];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2049
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2049
then
timOff := timeStampsNewYear[39];
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.NY2050
or
zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef == 2050
then
timOff := timeStampsNewYear[40];
else
timOff :=0;
assert(false, "No valid ZeroTime could be identified.
This is a bug, please submit a bug report.");
end if;
if zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
then
timOff :=timOff + ((dayRef - 1) +
sum({dayInMonth[i]
for i
in 1:(monthRef - 1)})
+ (
if monthRef > 2
and isLeapYear[yearRef - firstYear + 1]
then 1
else 0))*3600*24;
end if;
assert(time + offset + timOff >= timeStampsNewYear[1],
if zerTim == Buildings.Utilities.Time.Types.ZeroTime.UnixTimeStamp
then
"Could not initialize date in the CalendarTime block.
You selected 1970 as the time=0 reference.
Therefore the simulation startTime must be at least " +
String(timeStampsNewYear[1]) + "."
elseif zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
then
if yearRef <firstYear
then
"Could not initialize date in the CalendarTime block.
You selected a custom time=0 reference.
The minimum value for yearRef is then " +
String(firstYear) + " but your value is " +
String(yearRef) + "."
else
"Could not initialize date in the CalendarTime block.
You selected a custom time=0 reference.
Possibly your startTime is too small."
else
"Could not initialize date in the CalendarTime block.
Possibly your startTime is negative?");
assert(time + offset + timOff < timeStampsNewYear[
size(timeStampsNewYear,1)],
if zerTim == Buildings.Utilities.Time.Types.ZeroTime.Custom
and yearRef >= lastYear
then
"Could not initialize date in the CalendarTime block.
You selected a custom time=0 reference.
The maximum value for yearRef is then " +
String(lastYear) +
" but your value is " +
String(yearRef) + "."
else
"Could not initialize date in the CalendarTime block.
Possibly your startTime is too large.");
initial algorithm
year :=0;
for i
in 1:
size(timeStampsNewYear,1)
loop
if unixTimeStampLocal < timeStampsNewYear[i]
and (
if i == 1
then true
else unixTimeStampLocal >= timeStampsNewYear[i-1])
then
yearIndex :=i - 1;
year :=firstYear + i - 2;
end if;
end for;
epochLastMonth := timeStampsNewYear[yearIndex];
month:=13;
for i
in 1:12
loop
if (unixTimeStampLocal-epochLastMonth)/3600/24 <
(
if i==2
and isLeapYear[yearIndex]
then 1 + dayInMonth[i]
else dayInMonth[i])
then
month :=
min(i,month);
else
epochLastMonth :=epochLastMonth + (
if i == 2
and isLeapYear[yearIndex]
then 1 + dayInMonth[i]
else dayInMonth[i])*3600*24;
end if;
end for;
equation
unixTimeStampLocal = time + offset + timOff;
hourSampleTrigger =
sample(hourSampleStart, 3600);
when hourSampleTrigger
then
if pre(firstHourSampling)
then
hour =
integer(
floor(
rem(unixTimeStampLocal,3600*24)/3600));
else
hour =
if (
pre(hour) == 23)
then 0
else (
pre(hour) + 1);
end if;
firstHourSampling = false;
end when;
daySampleTrigger =
sample(daySampleStart, 86400);
when daySampleTrigger
then
if pre(firstDaySampling)
then
daysSinceEpoch =
integer(
floor(unixTimeStampLocal/3600/24));
weekDay=
integer(
rem(4+daysSinceEpoch-1,7)+1);
else
daysSinceEpoch =
pre(daysSinceEpoch) + 1;
weekDay =
if (
pre(weekDay) == 7)
then 1
else (
pre(weekDay) + 1);
end if;
if unixTimeStampLocal - timeStampsNewYear[
pre(yearIndex)+1] > -eps_time
then
yearIndex=
pre(yearIndex)+1;
year =
pre(year) + 1;
else
yearIndex =
pre(yearIndex);
year =
pre(year);
end if;
assert(yearIndex<=
size(timeStampsNewYear,1),
"Index out of range for epoch vector: timeStampsNewYear needs to be extended beyond the year "
+
String(firstYear+
size(timeStampsNewYear,1)));
if unixTimeStampLocal - (
pre(epochLastMonth) + (
if pre(month)==2
and isLeapYear[yearIndex]
then 1 + dayInMonth[
pre(month)]
else dayInMonth[
pre(month)])*3600*24) > -eps_time
then
month =
if pre(month) == 12
then 1
else pre(month) + 1;
epochLastMonth =
floor(0.1 +
pre(epochLastMonth) +
(
if pre(month)==2
and isLeapYear[yearIndex]
then 1 + dayInMonth[
pre(month)]
else dayInMonth[
pre(month)])*3600*24);
else
month =
pre(month);
epochLastMonth =
pre(epochLastMonth);
end if;
day =
integer(1+
floor((unixTimeStampLocal-epochLastMonth)/3600/24));
firstDaySampling = false;
end when;
minute = (unixTimeStampLocal/60-daysSinceEpoch*60*24-hour*60);
end CalendarTime;
This component outputs the model time, which starts at the value at which the simulation starts.
For example, if a simulation starts at t=-1, then this block outputs first t=-1,
and its output is advanced at the same rate as the simulation time.
The model is used to allow the simulation to start from any time without having to set
the parameters for the clock, as would be necessary for the model
Modelica.Blocks.Sources.ContinuousClock.