UNITS
The UNITS
operator
converts an integer to an interval.
Syntax
expr UNITS qual
where qual can be one of:
YEAR
MONTH
DAY
HOUR
MINUTE
SECOND
FRACTION(1-6)
- expr is an integer expression.
Usage
The UNITS
operator converts an integer expression to an INTERVAL
value expressed in a single
unit of time that you specify after the UNITS
keyword.
For the qualifiers
YEAR
, MONTH
, DAY
,
HOUR
and SECOND
, if the left-hand expression evaluates to a
decimal number, any fractional part is discarded before the UNITS
operator is
applied. However, when using UNITS FRACTION
, the expression can be a decimal number
where the integer part is interpreted as a number of seconds, and the decimal part as the fraction
of a
second:MAIN
DEFINE iv INTERVAL SECOND(9) TO FRACTION(5)
LET iv = 76242.77999 UNITS FRACTION
DISPLAY iv -- Displays " 76242.77999"
END MAIN
The
UNITS
operator can be used to compare INTERVAL
values. For
example, to check if an INERVAL SECOND(9) TO SECOND
is
negative:FUNCTION is_negative( iv INTERVAL SECOND(9) TO SECOND )
RETURN (iv < 0 UNITS SECOND )
END FUNCTION
UNITS
has
a higher precedence than any arithmetic or boolean operator. As a
result, a left-hand arithmetic expression that uses a UNITS
operator
must be enclosed in parentheses. For example, 10 + 20 UNITS
MINUTES
will be evaluated as 10 + (20 UNITS MINUTES)
and
give a conversion error. It must be written (10 + 20) UNITS
MINUTES
to get the expected result.
Because the difference between two
DATE
values is an integer count of days rather than an
INTERVAL
data type, you might want to use the UNITS
operator to
convert such differences explicitly to INTERVAL
values:MAIN
DEFINE d DATE
LET d = TODAY + 200
DISPLAY (d - TODAY) UNITS DAY
END MAIN