CoDeSys Time Manipulation

Hi,

I’m looking to manipulate time vars a bit in a CoDeSys project. (Like add 24 hours to a DT then compare it to the current time.)
I saw this thread:

and tried using SysTime for getting the current time. That seems to work fine, but I would like to do some date manipulation and the SysTime doesn’t have that. Whats the easiest way to convert the results from SysTime to a standard DT object? Or is there a better way to do this?

Thanks,
-Caleb

I would recommend checking out the OSCAT library functions regarding Date and Time. The OSCAT Library is an open source catalog of functions, and are included in the Opto 22 Codesys Runtime.

Yes. OSCAT looks good for manipulation, but I don’t see a way to retrieve from the clock with OSCAT, which is understandable in an opensource library used by multiple manufactures…

SysTime gives the current time, but doesn’t give it in the built-in types. (Is there a reason for this? To me using the built-in types would be a no-brainer.)

So I’ll write a function block to convert the SysTime output into DT and TOD with OSCAT functions but I’d prefer to use something that’s already written if it exists.

-Caleb

Here is my Code for the next guy…

Not tested extensively, but did have correct time when NTP is turned on.

Needs OSCAT_BASIC, and SysTime Libraries.

FUNCTION_BLOCK OptoTime
VAR_INPUT
END_VAR
VAR_OUTPUT
	todNow: TOD;
	dtNow: DT;
END_VAR
VAR
	stNow: SYSTIME; 
	stdNow: SYSTIMEDATE;
	sdtNow: OSCAT_BASIC.SDT;
END_VAR


(* Gets Current Time from SysTime and converts to BUILT_IN TYPES! *) 

SysTimeRtcHighResGet(stNow);
SysTimeRtcConvertHighResToLocal(stNow,stdNow);

sdtNow.YEAR 	:= stdNow.wYear;
sdtNow.MONTH 	:= stdNow.wMonth;
sdtNow.DAY 		:= stdNow.wDay;
sdtNow.HOUR 	:= stdNow.wHour;
sdtNow.MINUTE 	:= stdNow.wMinute;
sdtNow.SECOND 	:= stdNow.wSecond;
sdtNow.MS 		:= stdNow.wMilliseconds;

todNow := OSCAT_BASIC.SDT_TO_TOD(sdtNow);
dtNow := OSCAT_BASIC.SDT_TO_DT(sdtNow);