Is there a more direct method to extract the current PC's system date and time in Structured Language for a CoDeSys application?
I have tried the two methods implemented in the DateTimeUtilities example but I honestly believe that there should be an easier way to interrogate the system to obtain the present date and time without the need to have the program to invest time in calculating dates and times from 1970 and expecting a parameter for the timezone. The System's date and time is already available in the PC.
I have also attempted the RTC function, but this method requires the timezone parameter also.
In contrast, in the visualization the direct exhibition of a text like %t[ddd MMM.dd.yyyy HH:mm:ss] in a text field will display Thu Jun.25.2015 18:22:14 at running time - easy, clear and direct.
Luis Mendez
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Perhaps I shall add that I am writing a CoDeSys application to run through Win SL that is expected to go in a product that could be in any timezone. (It actually might travel through several timezones)
This is the reason why I would like to understand if there is a method to obtain the computer's system time by just by requesting it.
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
you are right i agree ... we will change either this Date and Time example (add an simple fb)
or we need to add an FB to a library which bring a simple (visu like) result.
BR
Edwin
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
// 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);
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Jzhvymetal,
In my case, using the PLC_TIME function, my time is delayed by 2 hours to the system time. Why is this happening? Everything is ok when the offset is set to 120. What are the input variables for in this function of time and year?
Last edit: zawadzki666 2021-09-23
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Is there a more direct method to extract the current PC's system date and time in Structured Language for a CoDeSys application?
I have tried the two methods implemented in the DateTimeUtilities example but I honestly believe that there should be an easier way to interrogate the system to obtain the present date and time without the need to have the program to invest time in calculating dates and times from 1970 and expecting a parameter for the timezone. The System's date and time is already available in the PC.
I have also attempted the RTC function, but this method requires the timezone parameter also.
In contrast, in the visualization the direct exhibition of a text like %t[ddd MMM.dd.yyyy HH:mm:ss] in a text field will display Thu Jun.25.2015 18:22:14 at running time - easy, clear and direct.
Luis Mendez
more posts ...
Perhaps I shall add that I am writing a CoDeSys application to run through Win SL that is expected to go in a product that could be in any timezone. (It actually might travel through several timezones)
This is the reason why I would like to understand if there is a method to obtain the computer's system time by just by requesting it.
Thanks
Hi,
you are right i agree ... we will change either this Date and Time example (add an simple fb)
or we need to add an FB to a library which bring a simple (visu like) result.
BR
Edwin
Hi Edwin,
In which library can I find this FB? I am user of Codesys 3.5.17
Try the following POU
PLC_Time_V2.zip [3.26 KiB]
Thanks Edwin and jzhvymetal for your prompt feedback,
I have decided to go with the following set of instructions,
VAR
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;
END_VAR
// 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);
Hi Jzhvymetal,
In my case, using the PLC_TIME function, my time is delayed by 2 hours to the system time. Why is this happening? Everything is ok when the offset is set to 120. What are the input variables for in this function of time and year?
Last edit: zawadzki666 2021-09-23
hi, I'm new user of Codesys and when I import the POU for time, this POU doesnt appear to be a fonction block, does someone can help me?
SysTimeRtcConvertUtcToLocal is marked as obsolete, but there's not hint on the actual implementation. Which function should I use instead?
Edit: even the new online help is still using SysTimeRtcConvertUtcToLocal as example...
Last edit: atone 2024-01-05