cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Reading out system time / clock in PLC

Reading out system time / clock in PLC

alink
New Contributor

Hello,

I'd like to read out system time in PLC.

Later he time should be split in hour, minute, second. There are several different libraries but I didn't get it to work.

Any hint?

With WebVisu it's easy with %t[HH:mm.ss] so the system time has to be available with standard library but which to use?

Thanks in advance

4 REPLIES 4

Open
Occasional Contributor

Hello Alink,

I happened to have done it in my recent project.

I used SysTimeRtc (System) library.

Declaration:

Date_and_time_in_seconds : UDINT;
Local_date_time_seconds : UDINT;
Date_and_Time_result : UDINT;
Local_date_time_from_secs : SYSTIMEDATE;
Date_and_time_format : DATE_AND_TIME;
Date_and_time_string : STRING;

 Implementation: 

// Get time in seconds since 1970 :
Date_and_time_in_seconds := SysTimeRtcGet(Date_and_Time_result);
IF Date_and_Time_result <> 0 THEN
RETURN;
END_IF

// Convert UTC seconds to local time seconds, regarding Timezone and Summer time :
Date_and_Time_result := SysTimeRtcConvertUtcToLocal(Date_and_time_in_seconds, Local_date_time_seconds);
IF Date_and_Time_result <> 0 THEN
RETURN;
END_IF

// Convert the UTC seconds to a DATE_AND_TIME variable dt#yyyy-mm-dd-hh:mm:ss :
Date_and_time_format := UDINT_TO_DT(Local_date_time_seconds);

// Convert DATE_AND_TIME to a String :
Date_and_time_string := DT_TO_STRING(Date_and_time_format);

 

alink
New Contributor

Thanks a lot @Open 

This works great.

Now I can build my own analog clock and assign a timestamp to recorded data.

PADE
Established Member

Hi, @Open ,

would you pls give me a hint where SysTimeRtcConvertUtcToLocal function takes timezone and summer time from?

I only found IL_LocalTime fb where you can set different timezones and switchting time to summer/winter time but SysTimeRtc function somehow works without those information.

Thanks a lot.

HmiGuide
Community Moderator
Community Moderator

You have to define the timezone settings, like offset to UTC and start & end of daylight saving time in a structure. See https://help.codesys.com/webapp/nuLcg6pcETbhW5dXsYWvvHsQUp0%2FTimeZone;product=Util;version=3.5.17.0

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist