[DISCUSSION NO SUPPORT NEEDED] Might have gone a bit overboard when dealing with DST... :)

I might have gone a bit overboard with this, but since i wanted to use an NTP-server, but also display the time in the HMI according to the the correct timezone and also adjust based on daylight savings time this was the outcome.
The code is specific for Sweden but could probably be adjusted pretty easily…

‘’'TodayDate := CONCAT_LDATE(RTC.YEAR,RTC.MONTH,RTC.DAY);
TodayDate2 := CONCAT_LDT(RTC.YEAR,RTC.MONTH,RTC.DAY,RTC.HOURS,RTC.MINUTES,RTC.SECONDS,0,0,0);
CurrentTime := CONCAT_LTOD(RTC.HOURS,RTC.MINUTES,RTC.SECONDS,0,0,0);

Year := TO_STRING(TodayDate,‘{0:yyyy}’);
Month := TO_STRING(TodayDate,‘{0:MM}’);
Day := TO_STRING(TodayDate,‘{0:dd}’);
Hours := TO_STRING(CurrentTime,‘{0:HH}’);
Minutes := TO_STRING(CurrentTime,‘{0:mm}’);
Seconds := TO_STRING(CurrentTime,‘{0:ss}’);

UtcHours := TO_INT(Hours);
UtcYear := TO_INT(Year);

LastDayMarch := CONCAT_LDT(UtcYear,3,31,23,59,59,00,00,00);
LastDayOctober := CONCAT_LDT(UtcYear,10,31,23,59,59,00,00,00);

DayOfWeekMarch := PBCL_DateTimeDayOfWeek_1(LastDayMarch);
DayOfWeekOctober := PBCL_DateTimeDayOfWeek_1(LastDayOctober);

DaysToSubtractMarch := DayOfWeekMarch MOD 7;
DaysToSubtractOctober := DayOfWeekOctober MOD 7;

DST_Start := CONCAT_LDT(UtcYear, 3, 31 - DaysToSubtractMarch, 23, 59, 59,00,00,00);
DST_End := CONCAT_LDT(UtcYear, 10, 31 - DaysToSubtractOctober, 23, 59, 59,00,00,00);

IF (TodayDate2 >= DST_Start) AND (TodayDate2 <= DST_End) THEN
IsDst := TRUE;
ELSE
IsDst := FALSE;
END_IF

IF IsDst THEN
LocalHours := UtcHours +2;
ELSE
LocalHours := UtcHours +1;
END_IF’‘’

That’s quite interesting. Thanks for sharing.
This might be a good topic for a Makers Blog post, for those who browse that section of the PLCnext Community website.
I wonder if someone might be interested in turning this into a function block that would work anywhere in the world, with the right parameters? It might be worth suggesting this to the developers of the PLCnextBase library via the “Contact Developer” button on their app page in the PLCnext Store.

As an alternative solution, would it be possible to issue the “date” command to an instance of the Shell function block from in the PLCnextBase library, maybe once a second, and process the resulting string? Then the operating system can do all the hard work of keeping track of the local time, including the transitions between standard and summer time.

Well, the developers of the PLCnextBase library are a step ahead of me.
Their “Date And Time” function blocks can already return the local time from the operating system. There is also a function indicating if the time has been adjusted for Summer time, or not.

Thanks for sharing, Mikael.

Another way to do it is to let the Operating System take care of it. We can let Linux check the real time clock for us.

What you need to do is change the time zone for the PLC/computer.
Default is UTC.
To change it in the OS you need to log in as admin in putty.
1. See the system’s current date and time
2. Set a new time, you must have root access to be able to change
3. Check the new time zone is set

1. > date
2. > sudo cp /usr/share/zoneinfo/Europe/Stockholm /etc/localtime
3. > date
Fri Jul 27 14:13:30 CEST 2018 (summer time)