PROGRAM SystemTime
VAR
iState : INT;
CurrentDateAndTime : DATE_AND_TIME;
GetCurrentDateAndTime : DTU.GetDateAndTime;
SetCurrentTimeZoneInformation: DTU.SetTimeZoneInformation;
uiYear : UINT;
uiMonth : UINT;
uiDay : UINT;
uiHour : UINT;
uiMinute : UINT;
uiSecond : UINT;
uimillSecond : UINT;
SetCurrentDateAndTime : DTU.SetDateAndTime;
secpulsFB : R_TRIG;
secpuls : BOOL;
olds : UINT;
news : UINT;
change : BOOL;
scount : DINT;
END_VAR
// This example visualises an analog clock. Timezone informatione are used to get the correct local time.
//
CASE iState OF
0: iState:= 1;
// Set the current timezone. The times to set the clock
SetCurrentTimeZoneInformation(
xExecute:= TRUE,
tziInfo:= DTU.GlobalConstants.gc_tziTimeZoneCET);
IF SetCurrentTimeZoneInformation.xDone THEN
SetCurrentTimeZoneInformation(xExecute:= FALSE);
iState:= 1;
END_IF
IF SetCurrentTimeZoneInformation.xError THEN
SetCurrentTimeZoneInformation(xExecute:= FALSE);
iState:= 32767;
END_IF
1: // Fetch the current date and time.
GetCurrentDateAndTime(
xExecute:= TRUE);
IF GetCurrentDateAndTime.xDone THEN
CurrentDateAndTime:= GetCurrentDateAndTime.dtDateAndTime;
GetCurrentDateAndTime(xExecute:= FALSE);
iState:= 2;
END_IF
IF GetCurrentDateAndTime.xError THEN
GetCurrentDateAndTime(xExecute:= FALSE);
iState:= 32767;
END_IF
2: // Split the date and time in its parts.
IF DTU.DTSplit(
CurrentDateAndTime,
ADR(uiYear),
ADR(uiMonth),
ADR(uiDay),
ADR(uiHour),
ADR(uiMinute),
ADR(uiSecond)) = DTU.ERROR.NO_ERROR
THEN
iState:= 1;
ELSE
iState:= 32767;
END_IF
32767: (*error*) ;
iState:= 1;
END_CASE
Year := UINT_TO_STRING ( uiYear );
Month := '';
CASE UINT_TO_INT (uiMonth) OF
0..9 : Month := CONCAT ( Month , '0' );
Month := CONCAT ( Month , UINT_TO_STRING ( uiMonth )) ;
ELSE
Month := UINT_TO_STRING ( uiMonth );
END_CASE;
Day := '';
CASE UINT_TO_INT (uiDay) OF
0..9 : Day := CONCAT ( Day , '0' );
Day := CONCAT ( Day , UINT_TO_STRING ( uiDay ));
ELSE
Day := UINT_TO_STRING ( uiDay ) ;
END_CASE;
Hour := '';
CASE UINT_TO_INT (uiHour) OF
0..9 : Hour := CONCAT ( Hour , '0' );
Hour := CONCAT ( Hour , UINT_TO_STRING ( uiHour ) );
ELSE
Hour := UINT_TO_STRING ( uiHour ) ;
END_CASE;
Minute :='';
CASE UINT_TO_INT (uiMinute) OF
0..9 : Minute := CONCAT ( Minute , '0' );
Minute := CONCAT ( Minute , UINT_TO_STRING ( uiMinute ) );
ELSE
Minute := UINT_TO_STRING ( uiMinute );
END_CASE;
Second := '';
CASE UINT_TO_INT (uiSecond) OF
0..9 : Second := CONCAT ( Second , '0' );
Second := CONCAT ( Second , UINT_TO_STRING ( uiSecond ) );
ELSE
Second := UINT_TO_STRING ( uiSecond ) ;
END_CASE;
(* für das File Object *)
(* ----------------------------------- Datestring ------------------------------ *)
Date_ := '';
Date_ := CONCAT (Date_, Year);
Date_ := CONCAT (Date_, '_');
Date_ := CONCAT (Date_, Month);
Date_ := CONCAT (Date_, '_');
Date_ := CONCAT (Date_, Day);
(* ----------------------------------- Timestring ------------------------------ *)
Time_ := '';
Time_ := CONCAT (Time_, ' ');
Time_ := CONCAT (Time_, Hour);
Time_ := CONCAT (Time_, ':');
Time_ := CONCAT (Time_, Minute);
Time_ := CONCAT (Time_, ':');
Time_ := CONCAT (Time_, Second);
news := uiSecond;
IF ( olds <> news )
THEN
olds := news;
change := TRUE;
ELSE
change := FALSE;
END_IF
secpulsFB(CLK:= change , Q=> secpuls );
IF secpuls
THEN
scount := scount + 1;
ELSE
;
END_IF