Utility Functions

Utility Functions

Utility functions can be commonly used as parameters of any functions.

Context

context()

Syntax: context()

Returns context object of the script runtime.

key()

Syntax: key()

Returns the key of the current record.

value()

Syntax: value( [index] )

  • index int index of the value array

Returns the whole value of the current records in array. If the index is given, it returns the element of the values.

For example, If the current value is [0, true, "hello", "world"]

  • value() returns the whole value array [0, true, "hello", "world"]
  • value(0) returns the first element of the value 0
  • value(3) returns the last element of the value "world"

payload()

Syntax: payload()

Returns the current input stream that sent from caller of the tql script. If the tql script is called via HTTP, the result of payload() is the stream of the body content of POST request. If the tql script is called via MQTT, the payload() returns the payload of the PUBLISH message.

param()

Syntax: param( name )

  • name string name of the query parameter

When the tql script is called via HTTP, the requested query parameters can be accessed by param() function.

Time

time()

Syntax: time( number|string [, duration] )

Example)

  • time('now') return current time.
  • time('now', '-10s50ms') return the time 10.05 seconds before from now.
  • time('now -10s50ms') return the time 10.05 seconds before from now.
  • time(1672531200*1000000000) returns the time of Jan-1-2023 AM 12:00:00

roundTime()

Syntax: roundTime( time, duration )

Returns rounded time.

Example)

  • roundTime(time('now'), '1h')
  • roundTime(key(), '1s')

parseTime()

Syntax: parseTime( time, format, timezone )

  • time string time expression
  • format string time format expression
  • timezone tz timezone value typically use tz() function to get the demand location

Example)

  • parseTime("2023-03-01 14:01:02", "DEFAULT", tz("Asia/Tokyo"))
  • parseTime("2023-03-01 14:01:02", "DEFAULT", tz("local"))

tz()

Syntax: tz( name )

Returns time zone that matched with the given name

Example)

  • tz('local')
  • tz('UTC')
  • tz('EST')
  • tz("Europe/Paris")

timeformat()

Syntax: timeformat( format )

  • format string
SQL(`select time, value from example limit 10`)
CSV(timeformat("DEFAULT"), tz("Asia/Seoul"))
ROWNUM	column0                column1
1       22 Aug 23 09:58 KST    0.8906962016352916
2       22 Aug 23 09:58 KST    0.8439627151061103
formatresult of timeformatting
DEFAULT2006-01-02 15:04:05.999
NUMERIC01/02 03:04:05PM ‘06 -0700
ANSICMon Jan _2 15:04:05 2006
UNIXMon Jan _2 15:04:05 MST 2006
RUBYMon Jan 02 15:04:05 -0700 2006
RFC82202 Jan 06 15:04 MST
RFC822Z02 Jan 06 15:04 -0700
RFC850Monday, 02-Jan-06 15:04:05 MST
RFC1123Mon, 02 Jan 2006 15:04:05 MST
RFC1123ZMon, 02 Jan 2006 15:04:05 -0700
RFC33392006-01-02T15:04:05Z07:00
RFC3339NANO2006-01-02T15:04:05.999999999Z07:00
KITCHEN3:04:05PM
STAMPJan _2 15:04:05
STAMPMILLIJan _2 15:04:05.000
STAMPMICROJan _2 15:04:05.000000
STAMPNANOJan _2 15:04:05.000000000
sunix epoch time in seconds
msunix epoch time in milliseconds
usunix epoch time in microseconds
nsunix epoch time in nanoseconds
s_msseconds and millisec (05.999)
s_usseconds and microsec (05.999999)
s_nsseconds and nanosec (05.999999999)
s.msseconds and millisec, zero padding (05.000)
s.usseconds and microsec, zero padding (05.000000)
s.nsseconds and nanosec, zero padding (05.000000000)

sqlTimeformat()

Syntax: sqlTimeformat( format )

  • format string
SQL(`select time, value from example limit 10`)
CSV(sqlTimeformat("YYYY-MM-DD HH24:MI:SS.nnnnnn"), tz("Asia/Seoul"))
ROWNUM	column0	                      column1
1       2023-08-22 09:58:58.216739    0.8906962016352916
2       2023-08-22 09:58:58.226739    0.8439627151061103
3       2023-08-22 09:58:58.236739    0.7897361125403046
formatresult of timeformatting
YYYYfour-digit year value
YYtwo-digit year value
MMtwo-digit month value between 01 to 12
MMMday of week
DDtwo-digit day of month between 01 to 31
HH24two-digit hour value between 00 to 23
HH12two-digit hour value between 0 to 12
HHtwo-digit hour value between 0 to 12
MItwo-digit minute value between 00 to 59
SStwo-digit seconds value between 0 and 59
AMAM/PM
nnn…1 to 9 digits fractions of a second

ansiTimeformat()

Syntax: ansiTimeformat( format )

  • format string
SQL(`select time, value from example limit 10`)
CSV( ansiTimeformat("yyyy-mm-dd hh:nn:ss.ffffff"))
ROWNUM	column0	                      column1
1       2023-08-22 09:58:58.216739    0.8906962016352916
2       2023-08-22 09:58:58.226739    0.8439627151061103
3       2023-08-22 09:58:58.236739    0.7897361125403046
formatresult of timeformatting
yyyyfour-digit year value
mmtwo-digit month value between 01 to 12
ddtwo-digit day value between 01 to 31
hhtwo-digit hour value between 00 to 23
nntwo-digit minute value between 00 to 59
sstwo-digit seconds value between 0 and 59
fff…1 to 9 digits fractions of a second

Others

count()

Syntax: count( array|tuple )

Returns the number of the elements.

Math

Mathematical functions. Since v8.0.6

This functions does not guarantee bit-identical results across system architectures.

functiondescription
abs(x)the absolute value of x.
acos(x)the arccosine, in radians, of x.
acosh(x)the inverse hyperbolic cosine of x.
asin(x)the arcsine, in radians, of x.
asinh(x)the inverse hyperbolic sine of x.
atan(x)the arctangent, in radians, of x.
atanh(x)the inverse hyperbolic tangent of x.
ceil(x)the least integer value greater than or equal to x.
cos(x)the cosine of the radian argument x.
cosh(x)the hyperbolic cosine of x.
exp(x)e**x, the base-e exponential of x.
exp2(x)2**x, the base-2 exponential of x.
floor(x)the greatest integer value less than or equal to x.
log(x)the natural logarithm of x.
log2(x)the binary logarithm of x. The special cases are the same as for log.
log10(x)the decimal logarithm of x. The special cases are the same as for log.
max(x,y)the larger of x or y.
min(x,y)the smaller of x or y.
mod(x,y)the floating-point remainder of x/y.
The magnitude of the result is less than y and its sign agrees with that of x.
pow(x, y)x**y, the base-x exponential of y.
pow10(x)10**x, the base-10 exponential of n.
remainder(x,y)the IEEE 754 floating-point remainder of x/y.
round(x)the nearest integer, rounding half away from zero.
sin(x)the sine of the radian argument x.
sinh(x)the hyperbolic sine of x.
sqrt(x)the square root of x.
tan(x)the tangent of the radian argument x.
tanh(x)the hyperbolic tangent of x.
trunc(x)the integer value of x.

An example usage of math function sqrt with MAPVALUE. map-math

Last updated on