Skip to content

convert.q

Jaskirat Rajasansir edited this page Jan 29, 2021 · 6 revisions

Type Conversion Functions

This library provides conversions between different internal types and JSON types.

.convert.msToTimespan / .convert.timespanToMs

Converts between milliseconds and Timespan

.convert.epochMsToTimestamp / .convert.timestampToEpochMs

Converts between a kdb Timestamp and JSON-compatible date/time.

js) new Date().getTime()
js) 1489596941059
q) .convert.epochMsToTimestamp 1489596941059
2017.03.15D16:55:41.059000064
q) .convert.timestampToEpochMs 2017.03.15D16:55:41.059000064
1489596941059
js) new Date(1489596941059)
js) Wed Mar 15 2017 16:55:41 GMT+0000 (GMT Standard Time)

.convert.hsymToString / .convert.stringToHsym

Converts between a standard kdb file path type and a string. This can be useful for files you load with get (which requires a file path type) and system "l" which requires a string.

/ Equivalent to: hsym `$"/a/path"
q) .convert.stringToHsym "/a/path"
`:/a/path

q) .convert.stringToHsym .z.d
`:2021.01.08
q) .convert.hsymToString `:/a/hdb/path
"/a/hdb/path"

.convert.ipOctalToSymbol

Converts an IP address in octal form to the standard IPv4 address layout as a symbol.

q) .z.a
167837945i
q) .convert.ipOctalToSymbol .z.a
`10.1.0.249

.convert.listToString

Converts a list into a string, separated by commas, useful for logging to the console. This function has higher performance than .Q.s1 for printing lists.

q) .convert.listToString `a`b`c
"a, b, c"
q) .convert.listToString 1 2 3
"1, 2, 3"

.convert.genericListToString

A more general version of '.convert.listToString' to ensure all elements of the specified list are string-ed. Each element of the list is | separated

q) .convert.genericListToString `a`b`c
"a | b | c"

q) .convert.genericListToString (1 2 3; `a`b`c)
"1, 2, 3 | a, b, c"

.convert.bytesToLong

Converts bytes into it's equivalent as a long integer. Any byte lists shorter than 8 will be padded appropriately

q) .convert.bytesToLong 0x0a
10
q) .convert.bytesToLong 0x01ff0a
130826
q) .convert.bytesToLong 0x000000000000000001
'TooManyBytesException

.convert.tableToHtml

Converts a kdb table into a HTML representation (<table>) for display on a web-page or e-mail.

The column names are returned as a <th> element within a <thead> to allow for separate styling compared to the table contents. Each row is separated by a new line (\n).

q) tbl:flip `a`b`c`d!5?/:(`3; 13123; 5f;enlist "a string")

q) tbl
a   b     c         d
------------------------------
mil 12873 2.892601  "a string"
igf 11354 0.4194429 "a string"
kao 6234  0.9799536 "a string"
baf 11109 1.87819   "a string"
kfh 1492  3.068726  "a string"

q) -1 .convert.tableToHtml tbl

<table><thead><tr><th>a</th><th>b</th><th>c</th><th>d</th></tr></thead>
<tr><td>mil</td><td>12873</td><td>2.892601</td><td>a string</td></tr>
<tr><td>igf</td><td>11354</td><td>0.4194429</td><td>a string</td></tr>
<tr><td>kao</td><td>6234</td><td>0.9799536</td><td>a string</td></tr>
<tr><td>baf</td><td>11109</td><td>1.87819</td><td>a string</td></tr>
<tr><td>kfh</td><td>1492</td><td>3.068726</td><td>a string</td></tr></table>
Clone this wiki locally