In an application, it is often necessary that there is access to the local time of a certain time zone.
The time zone does not necessarily have to correspond to the time zone of the control.
Possibly even different time zones are needed, e.g. for a mobile control.
In this article, the following application examples are described:
Setting the time and time zone of a controller
The CODESYS runtime provides a variety of different functions for setting the time and time zone.
However, it is not always advisable to implement this via the CODESYS runtime.
This will be considered in more detail in the following article:
Our recommendation is to define the time zone(s) in the application itself and to manage them there, in place of the CODESYS runtime!
The Util-Lib provides functions for this, with which you can define your own time zones as IEC structure based on the time in the Coordinated Universal Time (UTC).
Reading the time (UTC)
The DateTimeProvider of the Util-Lib returns the time (UTC) in milliseconds since 01/01/1970, 00:00.
Declaration |
---|
MAIN_PRG
|
Implementation |
---|
// Get the time in UTC
|
Time zone configuration
The time zone for which the controller is currently configured is not always the time zone suitable for displaying the current time. Therefore, the possibility to select the "correct" time zone suitable for the particular output option (WebVisu, Log Files, Email, ...) should be provided in the particular application.
The Util.TimeZone structure can be used to specify the bias, the switchover times for summer and winter time and the time offset.
The bias represents the deviation from UTC. This value is given in minutes.
- In eastern direction, starting from the zero meridian, the offset is positive.
- In western direction, starting from the prime meridian, the offset is negative.
Declaration |
---|
VAR_GLOBAL CONSTANT
|
Calculation of the local time
The calculation of the local time.
Declaration |
---|
VAR
|
Implementation |
---|
uliDateTimeCET := Util.LocalDateTime(tzTimeZone:=tziCET, uliDateTime:=uliDateTimeUTC, eErrorID=>eError, ePeriod=>ePeriodCET);
|
Splitting the local time
The Util.SplitDateTime function can be used to split the time specification (milliseconds since 1970 00:00) into the components: year, month, day, hours, minutes, seconds, milliseconds and the day of the week.
The function Util.SeperateDateTime can be used to split the time specification (milliseconds since 1970 00:00) into the data types DATE and TIME_OF_DAY.
See also....
- How to: Setting the time zone in Runtime/OS
- Our Codesys Online Help (OLH) Website
- OLH: TimerSwitch Example
- OLH: Advanced Topic’s- Duration, Date and Time
- Basic information to the Codesys Development System (Codesys Homepage)