Daylight Saving Time in Codesys

Daylight Saving Time in Codesys
codesys Time Zones daylight

Many PLCs uses a central time or NTP server for keeping the clocks updated and in sync. Time servers deliver the time in UTC format, which does not account for daylight saving time, known as winter or summer time. Which can also vary greatly between countries and can be changed over time by politicians.

As alarm time stamps or schedules in a PLC is oriented around humans, we need to keep the time available in the PLC up to date with daylight saving time or DST for short. The local time of the PLCs operating system is not always a universal solution, if you use your Codesys programs across different manufacturers PLC hardware.

First we need some information about the time and time zone. Make these declarations.

Codesys date, time and timezone variables.

The Codesys Util library contains a global constant with time zone information for CET, Central European Time, which I use in Denmark. We need to write the content of the global variable into our own TimeZone variable that is used as in/out data type on the LocalDateTime function.

The content of the global constant gc_tzTimeZoneCET is just a range of dates for switching between winter and summer time, the complete documentation of the STRUCT can be found at: https://help.codesys.com/webapp/nuLcg6pcETbhW5dXsYWvvHsQUp0%2FTimeZone;product=Util;version=3.5.17.0

Codesys TimeZone STRUCT

We get the time from a DateTimeProvider.GetDateTime which outputs a 64-bit ULINT of milliseconds since 1970. The SplitDateTime is just there to show the datetime in a human readable format.

LocalDateTime takes a TimeZone and DateTime and we are interested in the ePeriod output. Its a enumeration that will give us a 1 for winter time (STANDARD) and 2 for summer time (DAYLIGHT). A small evaluation on the value is used to write a 0 or 1 into my own DST_OFFSET variable that takes a hour input.

Daylight saving time code for Codesys

From the Online data flow we can conclude that it works and its summer!

Daylight saving time code for Codesys

Discover more from SCADA, PLC and Automation Engineering

Subscribe to get the latest posts sent to your email.

Leave a Reply

Your email address will not be published. Required fields are marked *