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:


LDATE and LTOD, 64-bit specified date and time datatypes, are not supported within the Util Library.

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:

How to: Setting the time zone in Runtime/OS 

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
VAR
    fbDateTimeProvider : Util.DateTimeProvider;
    uliDateTimeUTC     : ULINT;
END_VAR

Implementation
// Get the time in UTC
uliDateTimeUTC :=  fbDateTimeProvider.GetDateTime();  

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

    gc_tzTimeZoneCET : Util.TimeZone :=
    (/// Central Europe Time
        iBias := 60 // T#1h => minutes,
        asgPeriod := [
        (
            // (CEST -> CET) - Last Sunday in Oktober at 03:00:00.000
            sName  := 'CET',
            dtDate := (uiMonth := 10, eWeekday := WEEKDAY.SUNDAY, uiDay := 5, uiHour := 3),
            iBias  := 0
        ),(
            // (CET -> CEST) - Last Sunday in March at 02:00:00.000
            sName  := 'CEST',
            dtDate := (uiMonth := 3, eWeekday := WEEKDAY.SUNDAY, uiDay := 5, uiHour := 2),
            iBias  := 60 // T#1h => minutes
        )]
    );

END_VAR

Calculation of the local time

The calculation of the local time.

Declaration
VAR
    uliDateTimeCET : ULINT;
    ePeriodCET     : Util.Period;
    eError         : Util.Error;
END_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.


Declaration
VAR
    dtCET : Util.DateTime;
END_VAR

Implementation
Util.SplitDateTime(uliDateTime :=   uliDateTimeCET,
                   uiYear=>         dtCET.uiYear,
                   uiMonth=>        dtCET.uiMonth,
                   uiDay=>          dtCET.uiDay,
                   uiHour=>         dtCET.uiHour,
                   uiMinute=>       dtCET.uiMinute,
                   uiSecond=>       dtCET.uiSecond,
                   uiMilliseconds=> dtCET.uiMilliseconds,
                   eWeekday=>       dtCET.eWeekday);

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.

Declaration
VAR
    dateCET : DATE;
    todCET  : TOD;
END_VAR

Implementation
Error := Util.SeparateDateTime(uliDateTime:=uliDateTimeCET, eWeekDay=>, datDate=>dateCET, todTime=>todCET);



See also....





  • No labels