Date and time conversion
Undefined result if the value range is exceeded If the input value of a type conversion operator is outside the value range of the output data type, the result of the operation is not defined and depends on the platform. This is the case, for example, when a negative operand value is converted from LREAL to the target data type UINT. An exception error is also possible! |
Loss of information possible If a larger data type is converted to a smaller data type, information may be lost. |
String manipulation when converting to STRING or WSTRING With a type conversion to STRING or WSTRING, the typed value is stored as a left aligned string and truncated if it is overlong. Therefore, declare the return variables for the type conversion operators <type>_TO_STRING and <type>_TO_WSTRING long enough to accommodate the string without manipulation. |
TIME/TOD_TO_<type>
The operators are used to convert a date and time specification to another data type.
You can specify a variable or a literal as the operand.
Syntax:
DATE_TO_<Datentyp> (<Variablenname> | <Literal>)
DT_TO_<Datentyp> (<Variablenname> | <Literal>)
TOD_TO_<Datentyp> (<Variablenname> | <Literal>)
LDATE_TO_<Datentyp> (<Variablenname> | <Literal>)
LDT_TO_<Datentyp> (<Variablenname> | <Literal>)
LTOD_TO_<Datentyp> (<Variablenname> | <Literal>)
The DATE and DT data types use the same internal storage format and are stored as DWORD. The resolution for DATE is 1 day. The resolution for DT is 1 second. Both begin on January 1, 1970. TOD is stored as a DWORD with a resolution of 1 millisecond.
Examples:
ST code | Result |
---|---|
| 'T#12ms' |
| 300000 |
| 12 |
Converting date and time to BOOL
Operators:
DATE_TO_BOOL (<Variablenname> | <Literal>)
DT_TO_BOOL (<Variablenname> | <Literal>)
TOD_TO_BOOL (<Variablenname> | <Literal>)
LDATE_TO_BOOL (<Variablenname> | <Literal>)
LDT_TO_BOOL (<Variablenname> | <Literal>)
LTOD_TO_BOOL (<Variablenname> | <Literal>)
The operator returns a FALSE exactly at the time when the operand value can be interpreted as “0”.
Examples:
ST code | Result |
---|---|
| FALSE |
| FALSE |
| FALSE |
| TRUE |
| TRUE |
| TRUE |
Converting date and time to integer
Operators:
DATE_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
DT_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
TOD_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
LDATE_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
LDT_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
LTOD_TO_<INT-Datentyp> (<Variablenname> | <Literal>)
The same internal storage format, a DWORD, is used for the DATE and DT data types. The resolution for DATE is 1 day. The resolution for DT is 1 second. Both begin on January 1, 1970.
TOD is stored as a DWORD with a resolution of 1 millisecond.
Examples:
ST code | Result |
---|---|
| 0 |
| 0 |
| 0 |
| 1 |
| 86400 |
| 1567339200 |
| 1567339200 |
| 43200000 |
Converting date and time to STRING/WSTRING
Operators:
DATE_TO_<STRING/WSTRING> (<Variablenname> | <Literal>)
DT_TO_<STRING/WSTRING> (<Variablenname> | <Literal>)
TOD_TO_<STRING/WSTRING> (<Variablenname> | <Literal>)
LDATE_TO_<STRING/WSTRING> (<Variablenname> | <Literal>)
LDT_TO_<STRING/WSTRING> (<Variablenname> | <Literal>)
LTOD_TO_<STRING/WSTRING> (<Variablenname> | <Literal>)
The operands of type DATE, DATE_AND_TIME, TIME_OF_DAY, DT or TOD, which are passed to an operator for a date and time conversion, are converted in their constant notation (literal notation). The generated string contains the keywords D#, DT# or TOD# and then the size with its date and time unit, as specified in the IEC 61131-3 standard.
Examples:
ST code | Result |
---|---|
| 'T#12ms' |
| 'DT#1998-02-13-14:20:00' |
See also:
- Data types > Date and time data types