Time Conversion

Hint

If the operand value for a type conversion operator is outside of the value range of the target data type, then the result output depends on the processor type and is therefore undefined. This is the case, for example, when a negative operand value is converted from LREAL to the target data type UINT.

Information can be lost when converting from larger data types to smaller data types.

The operators convert time values (TIME or LIME) into the specified data types and return this type-converted value.

Call syntax

<variable name> := <time conversion operator> ( <operand> );

<operand> = <variable name> | <literal>

Operators

TIME_TO___UXINT
TIME_TO___XINT
TIME_TO___XWORD
TIME_TO_BIT
TIME_TO_BOOL
TIME_TO_BYTE
TIME_TO_DATE
TIME_TO_DINT
TIME_TO_DT
TIME_TO_DWORD
TIME_TO_INT
TIME_TO_LDATE
TIME_TO_LDINT
TIME_TO_LINT
TIME_TO_LREAL
TIME_TO_LTIME
TIME_TO_LTOD
TIME_TO_LWORD
TIME_TO_REAL
TIME_TO_SINT
TIME_TO_STRING
TIME_TO_TOD
TIME_TO_UDINT
TIME_TO_UINT
TIME_TO_ULINT
TIME_TO_USINT
TIME_TO_WORD
TIME_TO_WSTRING

LTIME_TO___UXINT
LTIME_TO___XINT
LTIME_TO___XWORD
LTIME_TO_BIT
LTIME_TO_BOOL
LTIME_TO_BYTE
LTIME_TO_DATE
LTIME_TO_DINT
LTIME_TO_DT
LTIME_TO_DWORD
LTIME_TO_INT
LTIME_TO_LDATE
LTIME_TO_LDINT
LTIME_TO_LINT
LTIME_TO_LREAL
LTIME_TO_LTOD
LTIME_TO_LWORD
LTIME_TO_REAL
LTIME_TO_SINT
LTIME_TO_STRING
LTIME_TO_TIME
LTIME_TO_TOD
LTIME_TO_UDINT
LTIME_TO_UINT
LTIME_TO_ULINT
LTIME_TO_USINT
LTIME_TO_WORD
LTIME_TO_WSTRING

Converting to Boolean values

The operator returns FALSE if and only if the operand value can be interpreted as “0”.

xTime := TIME_TO_BOOL(T#0MS); xTime = FALSE
xLongTime := TIME_TO_BOOL(T#0NS); xLongTime = FALSE
xTime := TIME_TO_BOOL(T#1MS); xDate = TRUE
xLongTime := TIME_TO_BOOL(T#1NS); xLongTime = TRUE

Converting to a string

Hint

String manipulation when converting to STRING or WSTRING

When converting the type to STRING or WSTRING, the typed value is left-aligned as a character string and truncated if it is too long. Therefore, declare the return variable for the type conversion operators <>_TO_STRING and <>_TO_WSTRING long enough that the character string has enough space without any manipulation.

sTime := TIME_TO_STRING(T#0MS); sTime = 'T#0MS'
wsLongTime := LTIME_TO_WSTRING(T#0US); wsLongTime = "T#0US"

Examples

ST implementation language

FUNCTION_BLOCK FB_ConvertTimeAndDate
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
        ltReturn_1: LTIME;
        lwReturn_2: LWORD;
        rReturn_3: REAL;
        strReturn_4: STRING;
        timReturn_5: TIME;
        todReturn_6: TIME_OF_DAY;
        uliReurn_7: ULINT;
        wstrReturn_8: WSTRING;
        wstrReturn_80: WSTRING;
        uliReurn_70: ULINT;
        todReturn_60: TIME_OF_DAY;
        timReturn_50: TIME;
        strReturn_40: STRING;
        rReturn_30: REAL;
        lwReturn_20: LWORD;
        ltReturn_10: LTIME;
        ltReturn_11: LTIME;
        lwReturn_21: LWORD;
        rReturn_31: REAL;
        strReturn_41: STRING;
        timReturn_51: TIME;
        todRedurn_61: TIME_OF_DAY;
        uliReurn_71: ULINT;
        wstrReturn_81: WSTRING;
        ltReturn_12: LTIME;
        xReturn_9: BOOL;
        xReturn_90: BOOL;
        xReturn_91: BOOL;
        xReturn_92: BOOL;
        dateReturn_6: DATE;
        timReturn_60: TIME;
        wReturn_61: WORD;
        todReturn_61: TIME_OF_DAY;
END_VAR

ltReturn_1 := DT_TO_LTIME(DT#2019-9-9-23:59:59);
ltReturn_10 := DT_TO_LTIME(DT#1970-1-1-0:0:0);
ltReturn_11 := DT_TO_LTIME(DT#1970-1-2-0:0:1);
ltReturn_12 := DT_TO_LTIME(DT#1970-1-3-12:30:30);

lwReturn_2 := TIME_TO_LWORD(T#5D4H2M3S2MS);
lwReturn_20 := TIME_TO_LWORD(T#0D0H0M0S0MS);

rReturn_3 := TIME_TO_REAL(T#5D4H2M3S2MS);
rReturn_30 := TIME_TO_REAL(T#0D0H0M0S0MS);

strREturn_4 := TIME_TO_STRING(T#5D4H2M3S2MS);
strREturn_40 := TIME_TO_STRING(T#0D0H0M0S0MS);

timReturn_5 := TOD_TO_TIME(TOD#23:59:59.999);
timReturn_50 := TOD_TO_TIME(TOD#0:0:0.000);
timReturn_51 := TOD_TO_TIME(TOD#0:0:0.001);

dateReturn_6 := TOD_TO_DATE(TOD#23:59:59.999);
timReturn_60 := TOD_TO_TIME(TOD#0:0:0.000);
wReturn_61 := TOD_TO_WORD(TOD#0:0:0.001);

uliReurn_7 := DATE_TO_ULINT(D#2019-9-9);
uliReurn_70 := DATE_TO_ULINT(D#1970-1-1);
uliReurn_71 := DATE_TO_ULINT(D#1970-1-2);

wstrReturn_8 := DATE_TO_WSTRING(D#2019-9-9);
wstrReturn_80 := DATE_TO_WSTRING(D#1970-1-1);
wstrReturn_81 := DATE_TO_WSTRING(D#1970-1-2);

xReturn_9 := DATE_TO_BOOL(D#2019-9-9);
xReturn_90 := DATE_TO_BOOL(D#1970-1-1);
xReturn_91 := DATE_TO_BOOL(D#1970-1-2);
xReturn_92 := DATE_TO_BOOL(D#1970-1-3);

FBD implementation language

See also