.

时间转换

.
.

提示!

如果类型转换运算符的操作数值超出目标数据类型的值范围,则结果输出取决于处理器类型,因此未定义。例如,当将负操作数值从LREAL 转换为目标数据类型UINT时,就是这种情况。

从较大的数据类型转换为较小的数据类型时,可能会丢失信息。

运算符将时间值(TIMELIME)转换为指定的数据类型,并返回该类型转换后的值。

.
调用语法

<变量名> := <时间转换操作符> ( <操作数> );

<操作数> = <变量名> | <文字>
 

.运算符


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
 
.
转换为布尔值

当且仅当操作数值可以解释为“0”时,运算符才返回FALSE

.

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

.
转换为字符串

提示!

转换为STRINGWSTRING的字符串操作

将类型转换为STRINGWSTRING时,键入的值将作为字符串左对齐,并且如果过长则被截断。因此,为类型转换运算符<>_TO_STRING<>_TO_WSTRING声明返回变量的长度应足够长,以便在不进行任何操作的情况下字符串有足够的空间。

.

sTime := TIME_TO_STRING(T#0MS);

sTime = 'T#0MS'

wsLongTime := LTIME_TO_WSTRING(T#0US);

wsLongTime = "T#0US"

.
例子
.

.ST实现语言


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实现语言