dolphindb 2.0.512 → 2.0.515

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -12,6 +12,10 @@ export declare enum DdbForm {
12
12
  chart = 7,
13
13
  chunk = 8
14
14
  }
15
+ /** DolphinDB DataType
16
+ 对应的 array vector 类型为 64 + 基本类型
17
+ 对应的 extended 类型为 128 + 基本类型
18
+ */
15
19
  export declare enum DdbType {
16
20
  void = 0,
17
21
  bool = 1,
@@ -93,7 +97,7 @@ export interface DdbArrayVectorBlock {
93
97
  unit: 1 | 2 | 4;
94
98
  rows: number;
95
99
  lengths: Uint8Array | Uint16Array | Uint32Array;
96
- data: DdbVectorValue;
100
+ data: Int8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array;
97
101
  }
98
102
  export interface DdbMatrixValue {
99
103
  rows: DdbObj<DdbVectorValue>;
@@ -179,6 +183,7 @@ export declare class DdbObj<T extends DdbValue = DdbValue> {
179
183
  to_rows<T extends Record<string, any> = Record<string, any>>(): T[];
180
184
  to_dict<T = Record<string, any>>(): T;
181
185
  }
186
+ export declare function format(type: DdbType, value: DdbValue, le: boolean, options: InspectOptions): string;
182
187
  export declare class DdbVoid extends DdbObj<undefined> {
183
188
  constructor();
184
189
  }
@@ -243,6 +248,10 @@ export declare class DdbTable extends DdbObj<DdbObj[]> {
243
248
  constructor(columns: DdbObj[], name?: string);
244
249
  }
245
250
  export declare function date2str(date: number, format?: string): string;
251
+ export declare function month2str(month: number | null): string;
252
+ export declare function time2str(time: number, format?: string): string;
253
+ export declare function minute2str(minute: number | null, format?: string): string;
254
+ export declare function second2str(second: number | null, format?: string): string;
246
255
  export declare function datetime2str(datetime: number, format?: string): string;
247
256
  /** format timestamp (bigint) to string
248
257
  - timestamp: bigint value
@@ -251,13 +260,15 @@ export declare function datetime2str(datetime: number, format?: string): string;
251
260
  https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
252
261
  */
253
262
  export declare function timestamp2str(timestamp: bigint, format?: string): string;
263
+ export declare function datehour2str(datehour: number, format?: string): string;
254
264
  /** parse timestamp string to bigint value
255
- - str: timestamp string, If it is an empty string, it will return the corresponding empty value (nulls.int64)
265
+ - str: timestamp string, If it is an empty string or 'null', it will return the corresponding empty value (nulls.int64)
256
266
  - format?:
257
267
  The format string corresponding to the incoming string, the default is `YYYY.MM.DD HH:mm:ss.SSS`
258
268
  https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
259
269
  */
260
270
  export declare function str2timestamp(str: string, format?: string): bigint;
271
+ export declare function nanotime2str(nanotime: bigint, format?: string): string;
261
272
  /** format nanotimestamp value (bigint) to string
262
273
  - nanotimestamp: bigint value
263
274
  - format?:
@@ -267,13 +278,16 @@ export declare function str2timestamp(str: string, format?: string): bigint;
267
278
  */
268
279
  export declare function nanotimestamp2str(nanotimestamp: bigint, format?: string): string;
269
280
  /** parse nano timestamp string to bigint value
270
- - str: nano timestamp string, If it is an empty string, it will return the corresponding empty value (nulls.int64)
281
+ - str: nano timestamp string, If it is an empty string or 'null', it will return the corresponding empty value (nulls.int64)
271
282
  - format?:
272
283
  The format string corresponding to the incoming string, the default is `YYYY.MM.DD HH:mm:ss.SSSSSSSSS`
273
284
  Seconds are in the format ss (must be included); nanoseconds are in the format SSSSSSSSS (must be included)
274
285
  https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
275
286
  */
276
287
  export declare function str2nanotimestamp(str: string, format?: string): bigint;
288
+ export declare function ipaddr2str(buffer: Uint8Array, le?: boolean): string;
289
+ export declare function uuid2str(buffer: Uint8Array, le?: boolean): string;
290
+ export declare function int1282str(buffer: Uint8Array, le?: boolean): string;
277
291
  export declare class DDB {
278
292
  /** 当前的 session id (http 或 tcp) */
279
293
  sid: string;
@@ -377,9 +391,9 @@ export declare class DDB {
377
391
  - options:
378
392
  - urgent?: 决定 `行为标识` 那一行字符串的取值(只适用于 script 和 function)
379
393
  - vars?: type === 'variable' 时必传,variable 指令中待上传的变量名
380
- - handler?: Process messages during this rpc (DdbMessage)
381
- - parse_object?: 在该次 rpc 期间设置 parse_object, 结束后恢复原有,为 false 时返回的 DdbObj 仅含有 buffer 和 le,
382
- 不做解析,以便后续转发、序列化
394
+ - listener?: 处理本次 rpc 期间的消息 (DdbMessage)
395
+ - parse_object?: 在本次 rpc 期间设置 parse_object, 结束后恢复原有
396
+ 为 false 时返回的 DdbObj 仅含有 buffer 和 le,不做解析,以便后续转发、序列化
383
397
  */
384
398
  rpc<T extends DdbObj = DdbObj>(type: 'script' | 'function' | 'variable' | 'connect', { script, func, args, vars, urgent, listener, parse_object, }: {
385
399
  script?: string;
@@ -459,4 +473,3 @@ export interface DdbErrorMessage {
459
473
  data: Error;
460
474
  }
461
475
  export declare type DdbMessage = DdbPrintMessage | DdbObjectMessage | DdbErrorMessage;
462
- export default DDB;
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DDB = exports.str2nanotimestamp = exports.nanotimestamp2str = exports.str2timestamp = exports.timestamp2str = exports.datetime2str = exports.date2str = exports.DdbTable = exports.DdbSetString = exports.DdbSetDouble = exports.DdbSetInt = exports.DdbVectorSymbol = exports.DdbVectorAny = exports.DdbVectorString = exports.DdbVectorDouble = exports.DdbVectorInt = exports.DdbFunction = exports.DdbPair = exports.DdbNanoTimeStamp = exports.DdbTimeStamp = exports.DdbDateTime = exports.DdbDouble = exports.DdbLong = exports.DdbString = exports.DdbInt = exports.DdbChar = exports.DdbBool = exports.DdbVoid = exports.DdbObj = exports.timezone_offset = exports.nulls = exports.DdbDurationUnit = exports.DdbFunctionType = exports.DdbType = exports.DdbForm = void 0;
3
+ exports.DDB = exports.int1282str = exports.uuid2str = exports.ipaddr2str = exports.str2nanotimestamp = exports.nanotimestamp2str = exports.nanotime2str = exports.str2timestamp = exports.datehour2str = exports.timestamp2str = exports.datetime2str = exports.second2str = exports.minute2str = exports.time2str = exports.month2str = exports.date2str = exports.DdbTable = exports.DdbSetString = exports.DdbSetDouble = exports.DdbSetInt = exports.DdbVectorSymbol = exports.DdbVectorAny = exports.DdbVectorString = exports.DdbVectorDouble = exports.DdbVectorInt = exports.DdbFunction = exports.DdbPair = exports.DdbNanoTimeStamp = exports.DdbTimeStamp = exports.DdbDateTime = exports.DdbDouble = exports.DdbLong = exports.DdbString = exports.DdbInt = exports.DdbChar = exports.DdbBool = exports.DdbVoid = exports.format = exports.DdbObj = exports.timezone_offset = exports.nulls = exports.DdbDurationUnit = exports.DdbFunctionType = exports.DdbType = exports.DdbForm = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const ws_1 = require("ws");
6
6
  const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
@@ -20,6 +20,10 @@ var DdbForm;
20
20
  DdbForm[DdbForm["chart"] = 7] = "chart";
21
21
  DdbForm[DdbForm["chunk"] = 8] = "chunk";
22
22
  })(DdbForm = exports.DdbForm || (exports.DdbForm = {}));
23
+ /** DolphinDB DataType
24
+ 对应的 array vector 类型为 64 + 基本类型
25
+ 对应的 extended 类型为 128 + 基本类型
26
+ */
23
27
  var DdbType;
24
28
  (function (DdbType) {
25
29
  DdbType[DdbType["void"] = 0] = "void";
@@ -471,7 +475,7 @@ class DdbObj {
471
475
  unit,
472
476
  rows,
473
477
  lengths,
474
- data
478
+ data: data
475
479
  });
476
480
  i_block_start = i_data_start + len_items;
477
481
  i_row += rows;
@@ -905,30 +909,97 @@ class DdbObj {
905
909
  case DdbForm.set:
906
910
  return `set<${tname}>[${this.rows}]`;
907
911
  case DdbForm.table:
908
- return `table[${this.rows} rows][${this.cols} cols]`;
912
+ return `table[${this.rows}r][${this.cols}c]`;
909
913
  case DdbForm.dict:
910
914
  return `dict<${DdbType[this.value[0].type]}, ${DdbType[this.value[1].type]}>`;
911
915
  case DdbForm.matrix:
912
- return `matrix<${DdbType[this.type]}>[${this.rows} rows][${this.cols} cols]`;
916
+ return `matrix<${DdbType[this.type]}>[${this.rows}r][${this.cols}c]`;
913
917
  default:
914
918
  return `${DdbForm[this.form]} ${tname}`;
915
919
  }
916
920
  })();
917
921
  const data = (() => {
918
922
  switch (this.form) {
919
- case DdbForm.pair:
920
- return `${(0, utils_js_1.inspect)(this.value[0], options)}, ${(0, utils_js_1.inspect)(this.value[1], options)}`;
921
923
  case DdbForm.scalar:
924
+ return format(this.type, this.value, this.le, options);
925
+ case DdbForm.vector:
926
+ case DdbForm.pair:
927
+ case DdbForm.set: {
928
+ const form = this.form;
929
+ function format_array(items, ellipsis) {
930
+ const str_items = items.join(', ') + (ellipsis ? ', ...' : '');
931
+ return form === DdbForm.pair ?
932
+ str_items
933
+ :
934
+ str_items.bracket('square');
935
+ }
936
+ if (64 <= this.type && this.type < 128) { // array vector
937
+ // 因为 array vector 目前只支持:Logical, Integral(不包括 INT128, COMPRESS 类型), Floating, Temporal
938
+ // 都对应 TypedArray 中的一格,所以直接根据 index 去取即可
939
+ // av = array(INT[], 0, 5)
940
+ // append!(av, [1..1])
941
+ // append!(av, [1..70000])
942
+ // append!(av, [1..1])
943
+ // append!(av, [1..500])
944
+ // ...
945
+ // av
946
+ const _type = this.type - 64;
947
+ const limit = 10;
948
+ let items = new Array(Math.min(limit, this.rows));
949
+ let i_items = 0;
950
+ for (const { lengths, data } of this.value) {
951
+ let acc_len = 0;
952
+ for (const length of lengths) {
953
+ let _items = new Array(Math.min(limit, length));
954
+ for (let i = 0; i < _items.length; i++)
955
+ _items[i] = format(_type, data[acc_len + i], this.le, options);
956
+ items[i_items++] = format_array(_items, length > limit);
957
+ acc_len += length;
958
+ }
959
+ if (i_items >= limit)
960
+ break;
961
+ }
962
+ return format_array(items, this.rows > limit);
963
+ }
922
964
  switch (this.type) {
923
- case DdbType.functiondef:
924
- return (0, utils_js_1.inspect)(this.value.name, options);
925
- case DdbType.duration: {
926
- const { data, unit } = this.value;
927
- const str = `${data}${DdbDurationUnit[unit]}`;
928
- return options.colors ? str.green : str;
965
+ case DdbType.symbol_extended: {
966
+ const limit = 50;
967
+ const { base, data } = this.value;
968
+ let items = new Array(Math.min(limit, data.length));
969
+ for (let i = 0; i < items.length; i++)
970
+ items[i] = (0, utils_js_1.inspect)(base[data[i]], options);
971
+ return format_array(items, data.length > limit);
972
+ }
973
+ case DdbType.uuid:
974
+ case DdbType.int128:
975
+ case DdbType.ipaddr: {
976
+ const limit = 10;
977
+ const value = this.value;
978
+ const len_data = value.length / 16;
979
+ let items = new Array(Math.min(limit, len_data));
980
+ for (let i = 0; i < items.length; i++)
981
+ items[i] = format(this.type, value.subarray(16 * i, 16 * (i + 1)), this.le, options);
982
+ return format_array(items, len_data > limit);
983
+ }
984
+ case DdbType.complex:
985
+ case DdbType.point: {
986
+ const limit = 20;
987
+ const value = this.value;
988
+ const len_data = value.length / 2;
989
+ let items = new Array(Math.min(limit, len_data));
990
+ for (let i = 0; i < items.length; i++)
991
+ items[i] = format(this.type, value.subarray(2 * i, 2 * (i + 1)), this.le, options);
992
+ return format_array(items, len_data > limit);
993
+ }
994
+ default: {
995
+ const limit = 50;
996
+ let items = new Array(Math.min(limit, this.value.length));
997
+ for (let i = 0; i < items.length; i++)
998
+ items[i] = format(this.type, this.value[i], this.le, options);
999
+ return format_array(items, this.value.length > limit);
929
1000
  }
930
1001
  }
931
- break;
1002
+ }
932
1003
  }
933
1004
  if (this.value instanceof Uint8Array)
934
1005
  return (0, utils_js_1.inspect)((0, utils_js_1.typed_array_to_buffer)(this.value), options);
@@ -938,7 +1009,7 @@ class DdbObj {
938
1009
  }
939
1010
  to_rows() {
940
1011
  if (this.form !== DdbForm.table)
941
- throw new Error('this.form is not DdbForm.table, cannot call to_rows');
1012
+ throw new Error('this.form 不是 DdbForm.table, 不能 to_rows');
942
1013
  let rows = new Array(this.rows);
943
1014
  for (let i = 0; i < this.rows; i++) {
944
1015
  let row = {};
@@ -971,7 +1042,7 @@ class DdbObj {
971
1042
  }
972
1043
  to_dict() {
973
1044
  if (this.form !== DdbForm.dict)
974
- throw new Error('this.form is not DdbForm.dict, cannot convert to Object');
1045
+ throw new Error('this.form 不是 DdbForm.dict, 不能转换为 Object');
975
1046
  const [{ value: keys }, { value: values }] = this.value;
976
1047
  let obj = {};
977
1048
  for (let i = 0; i < this.rows; i++)
@@ -988,6 +1059,183 @@ DdbObj.enc = new TextEncoder();
988
1059
  DdbObj.symbol_bases = {};
989
1060
  /** little endian (client) */
990
1061
  DdbObj.le_client = Boolean(new Uint8Array(Uint32Array.of(1).buffer)[0]);
1062
+ function format(type, value, le, options) {
1063
+ switch (type) {
1064
+ case DdbType.bool:
1065
+ return (0, utils_js_1.inspect)((value === null || value === exports.nulls.int8) ?
1066
+ null
1067
+ :
1068
+ Boolean(value), options);
1069
+ case DdbType.char:
1070
+ return (0, utils_js_1.inspect)((value === null || value === exports.nulls.int8) ?
1071
+ null
1072
+ :
1073
+ value, options);
1074
+ case DdbType.short:
1075
+ return (0, utils_js_1.inspect)((value === null || value === exports.nulls.int16) ?
1076
+ null
1077
+ :
1078
+ value, options);
1079
+ case DdbType.int:
1080
+ return (0, utils_js_1.inspect)((value === null || value === exports.nulls.int32) ?
1081
+ null
1082
+ :
1083
+ value, options);
1084
+ case DdbType.long:
1085
+ return (value === null || value === exports.nulls.int64) ?
1086
+ 'null'
1087
+ :
1088
+ options.colors ?
1089
+ String(value).green
1090
+ :
1091
+ String(value);
1092
+ case DdbType.date:
1093
+ return (value === null || value === exports.nulls.int32) ?
1094
+ 'null'
1095
+ :
1096
+ options.colors ?
1097
+ date2str(value).green
1098
+ :
1099
+ date2str(value);
1100
+ case DdbType.month:
1101
+ return (value === null || value === exports.nulls.int32) ?
1102
+ 'null'
1103
+ :
1104
+ options.colors ?
1105
+ month2str(value).green
1106
+ :
1107
+ month2str(value);
1108
+ case DdbType.time:
1109
+ return (value === null || value === exports.nulls.int32) ?
1110
+ 'null'
1111
+ :
1112
+ options.colors ?
1113
+ time2str(value).green
1114
+ :
1115
+ time2str(value);
1116
+ case DdbType.minute:
1117
+ return (value === null || value === exports.nulls.int32) ?
1118
+ 'null'
1119
+ :
1120
+ options.colors ?
1121
+ minute2str(value).green
1122
+ :
1123
+ minute2str(value);
1124
+ case DdbType.second:
1125
+ return (value === null || value === exports.nulls.int32) ?
1126
+ 'null'
1127
+ :
1128
+ options.colors ?
1129
+ second2str(value).green
1130
+ :
1131
+ second2str(value);
1132
+ case DdbType.datetime:
1133
+ return (value === null || value === exports.nulls.int32) ?
1134
+ 'null'
1135
+ :
1136
+ options.colors ?
1137
+ datetime2str(value).green
1138
+ :
1139
+ datetime2str(value);
1140
+ case DdbType.timestamp:
1141
+ return (value === null || value === exports.nulls.int64) ?
1142
+ 'null'
1143
+ :
1144
+ options.colors ?
1145
+ timestamp2str(value).green
1146
+ :
1147
+ timestamp2str(value);
1148
+ case DdbType.nanotime:
1149
+ return (value === null || value === exports.nulls.int64) ?
1150
+ 'null'
1151
+ :
1152
+ options.colors ?
1153
+ nanotime2str(value).green
1154
+ :
1155
+ nanotime2str(value);
1156
+ case DdbType.nanotimestamp:
1157
+ return (value === null || value === exports.nulls.int64) ?
1158
+ 'null'
1159
+ :
1160
+ options.colors ?
1161
+ nanotimestamp2str(value).green
1162
+ :
1163
+ nanotimestamp2str(value);
1164
+ case DdbType.float:
1165
+ return (value === null || value === exports.nulls.float32) ?
1166
+ 'null'
1167
+ :
1168
+ (0, utils_js_1.inspect)(value, options);
1169
+ case DdbType.double:
1170
+ return (value === null || value === exports.nulls.double) ?
1171
+ 'null'
1172
+ :
1173
+ (0, utils_js_1.inspect)(value, options);
1174
+ case DdbType.symbol:
1175
+ case DdbType.string:
1176
+ return (0, utils_js_1.inspect)(value, options);
1177
+ case DdbType.uuid:
1178
+ return options.colors ?
1179
+ uuid2str(value, le).green
1180
+ :
1181
+ uuid2str(value, le);
1182
+ case DdbType.functiondef:
1183
+ return (0, utils_js_1.inspect)(value.name, options);
1184
+ case DdbType.handle:
1185
+ case DdbType.code:
1186
+ return (0, utils_js_1.inspect)(value, options);
1187
+ case DdbType.datehour:
1188
+ return (value === null || value === exports.nulls.int32) ?
1189
+ 'null'
1190
+ :
1191
+ options.colors ?
1192
+ datehour2str(value).green
1193
+ :
1194
+ datehour2str(value);
1195
+ case DdbType.ipaddr:
1196
+ return options.colors ?
1197
+ ipaddr2str(value, le).green
1198
+ :
1199
+ ipaddr2str(value, le);
1200
+ case DdbType.int128:
1201
+ return options.colors ?
1202
+ int1282str(value, le).green
1203
+ :
1204
+ int1282str(value, le);
1205
+ case DdbType.blob:
1206
+ return (0, utils_js_1.inspect)(DdbObj.dec.decode(value.subarray(0, 1000)), options);
1207
+ case DdbType.point: {
1208
+ let [x, y] = value;
1209
+ if (x === exports.nulls.double)
1210
+ x = null;
1211
+ if (y === exports.nulls.double)
1212
+ y = null;
1213
+ return options.colors ?
1214
+ `(${String(x).green}, ${String(y).green})`
1215
+ :
1216
+ `(${String(x)}, ${String(y)})`;
1217
+ }
1218
+ case DdbType.complex: {
1219
+ let [x, y] = value;
1220
+ if (x === exports.nulls.double)
1221
+ x = null;
1222
+ if (y === exports.nulls.double)
1223
+ y = null;
1224
+ return options.colors ?
1225
+ `${String(x).green}+${`${String(y)}i`.green}`
1226
+ :
1227
+ `${String(x)}+${String(y)}i`;
1228
+ }
1229
+ case DdbType.duration: {
1230
+ const { data, unit } = value;
1231
+ const str = `${data}${DdbDurationUnit[unit]}`;
1232
+ return options.colors ? str.green : str;
1233
+ }
1234
+ default:
1235
+ return (0, utils_js_1.inspect)(value, options);
1236
+ }
1237
+ }
1238
+ exports.format = format;
991
1239
  class DdbVoid extends DdbObj {
992
1240
  constructor() {
993
1241
  super({
@@ -1298,15 +1546,49 @@ class DdbTable extends DdbObj {
1298
1546
  }
1299
1547
  exports.DdbTable = DdbTable;
1300
1548
  function date2str(date, format = 'YYYY.MM.DD') {
1301
- return date === exports.nulls.int32 ?
1302
- ''
1549
+ return (date === null || date === exports.nulls.int32) ?
1550
+ 'null'
1303
1551
  :
1304
1552
  (0, dayjs_1.default)(exports.timezone_offset + 1000 * 3600 * 24 * date).format(format);
1305
1553
  }
1306
1554
  exports.date2str = date2str;
1555
+ function month2str(month) {
1556
+ if (month === null || month === exports.nulls.int32)
1557
+ return 'null';
1558
+ if (month < 0)
1559
+ return String(month);
1560
+ const _month = month % 12;
1561
+ const year = Math.trunc(month / 12);
1562
+ return `${String(year).padStart(4, '0')}.${String(_month + 1).padStart(2, '0')}M`;
1563
+ }
1564
+ exports.month2str = month2str;
1565
+ function time2str(time, format = 'HH:mm:ss.SSS') {
1566
+ return (time === null || time === exports.nulls.int32) ?
1567
+ 'null'
1568
+ :
1569
+ (0, dayjs_1.default)(exports.timezone_offset + time)
1570
+ .format(format);
1571
+ }
1572
+ exports.time2str = time2str;
1573
+ function minute2str(minute, format = 'HH:mm') {
1574
+ return (minute === null || minute === exports.nulls.int32) ?
1575
+ 'null'
1576
+ :
1577
+ (0, dayjs_1.default)(exports.timezone_offset + 60 * 1000 * minute)
1578
+ .format(format);
1579
+ }
1580
+ exports.minute2str = minute2str;
1581
+ function second2str(second, format = 'HH:mm:ss') {
1582
+ return (second === null || second === exports.nulls.int32) ?
1583
+ 'null'
1584
+ :
1585
+ (0, dayjs_1.default)(exports.timezone_offset + 1000 * second)
1586
+ .format(format);
1587
+ }
1588
+ exports.second2str = second2str;
1307
1589
  function datetime2str(datetime, format = 'YYYY.MM.DD HH:mm:ss') {
1308
- return datetime === exports.nulls.int32 ?
1309
- ''
1590
+ return (datetime === null || datetime === exports.nulls.int32) ?
1591
+ 'null'
1310
1592
  :
1311
1593
  (0, dayjs_1.default)(exports.timezone_offset + 1000 * datetime).format(format);
1312
1594
  }
@@ -1318,20 +1600,27 @@ exports.datetime2str = datetime2str;
1318
1600
  https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
1319
1601
  */
1320
1602
  function timestamp2str(timestamp, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
1321
- return timestamp === exports.nulls.int64 ?
1322
- ''
1603
+ return (timestamp === null || timestamp === exports.nulls.int64) ?
1604
+ 'null'
1323
1605
  :
1324
1606
  (0, dayjs_1.default)(exports.timezone_offset + Number(timestamp)).format(format);
1325
1607
  }
1326
1608
  exports.timestamp2str = timestamp2str;
1609
+ function datehour2str(datehour, format = 'YYYY.MM.DDTHH') {
1610
+ return (datehour === null || datehour === exports.nulls.int32) ?
1611
+ 'null'
1612
+ :
1613
+ (0, dayjs_1.default)(exports.timezone_offset + 1000 * 3600 * datehour).format(format);
1614
+ }
1615
+ exports.datehour2str = datehour2str;
1327
1616
  /** parse timestamp string to bigint value
1328
- - str: timestamp string, If it is an empty string, it will return the corresponding empty value (nulls.int64)
1617
+ - str: timestamp string, If it is an empty string or 'null', it will return the corresponding empty value (nulls.int64)
1329
1618
  - format?:
1330
1619
  The format string corresponding to the incoming string, the default is `YYYY.MM.DD HH:mm:ss.SSS`
1331
1620
  https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
1332
1621
  */
1333
1622
  function str2timestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
1334
- if (!str)
1623
+ if (!str || str === 'null')
1335
1624
  return exports.nulls.int64;
1336
1625
  if (str.length !== format.length)
1337
1626
  throw new Error('The length of the timestamp string is not equal to the length of the format string');
@@ -1339,6 +1628,23 @@ function str2timestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
1339
1628
  (0, dayjs_1.default)(str, format).valueOf());
1340
1629
  }
1341
1630
  exports.str2timestamp = str2timestamp;
1631
+ function nanotime2str(nanotime, format = 'HH:mm:ss.SSSSSSSSS') {
1632
+ if (nanotime === null || nanotime === exports.nulls.int64)
1633
+ return 'null';
1634
+ if (nanotime < 0n)
1635
+ return String(nanotime);
1636
+ const i_second_start = format.indexOf('ss');
1637
+ if (i_second_start === -1)
1638
+ throw new Error('The format string must contain the format for seconds (ss)');
1639
+ const i_second_end = i_second_start + 2;
1640
+ const i_nanosecond_start = format.indexOf('SSSSSSSSS', i_second_end);
1641
+ if (i_nanosecond_start === -1)
1642
+ throw new Error('The format string must contain the format for nanoseconds (SSSSSSSSS)');
1643
+ return ((0, dayjs_1.default)(exports.timezone_offset + (Number(nanotime) / 1000000)).format(format.slice(0, i_second_end)) +
1644
+ format.slice(i_second_end, i_nanosecond_start) +
1645
+ String(nanotime % 1000000000n).padStart(9, '0'));
1646
+ }
1647
+ exports.nanotime2str = nanotime2str;
1342
1648
  /** format nanotimestamp value (bigint) to string
1343
1649
  - nanotimestamp: bigint value
1344
1650
  - format?:
@@ -1347,8 +1653,14 @@ exports.str2timestamp = str2timestamp;
1347
1653
  https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
1348
1654
  */
1349
1655
  function nanotimestamp2str(nanotimestamp, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSSS') {
1350
- if (nanotimestamp === exports.nulls.int64)
1351
- return '';
1656
+ // tests:
1657
+ // nanotimestamp2str(0n)
1658
+ // nanotimestamp2str(-1n)
1659
+ // nanotimestamp2str(-9_9999_9999n)
1660
+ // nanotimestamp2str(-10_0000_0000n)
1661
+ // nanotimestamp2str(-10_0000_0001n)
1662
+ if (nanotimestamp === null || nanotimestamp === exports.nulls.int64)
1663
+ return 'null';
1352
1664
  const i_second_start = format.indexOf('ss');
1353
1665
  if (i_second_start === -1)
1354
1666
  throw new Error('The format string must contain the format for seconds (ss)');
@@ -1356,20 +1668,27 @@ function nanotimestamp2str(nanotimestamp, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSS
1356
1668
  const i_nanosecond_start = format.indexOf('SSSSSSSSS', i_second_end);
1357
1669
  if (i_nanosecond_start === -1)
1358
1670
  throw new Error('The format string must contain the format for nanoseconds (SSSSSSSSS)');
1359
- return ((0, dayjs_1.default)(exports.timezone_offset + (Number(nanotimestamp) / 1000000)).format(format.slice(0, i_second_end)) +
1671
+ const remainder = nanotimestamp % 1000000000n;
1672
+ const borrow = remainder < 0n;
1673
+ return ((0, dayjs_1.default)(exports.timezone_offset +
1674
+ // 去掉 9 位的纳秒部分,转化为毫秒
1675
+ Number((nanotimestamp - remainder + (borrow ? -1000000000n : 0n)) / 1000000n)).format(format.slice(0, i_second_end)) +
1360
1676
  format.slice(i_second_end, i_nanosecond_start) +
1361
- String(nanotimestamp % 1000000000n));
1677
+ String(borrow ?
1678
+ (remainder + 1000000000n) % 1000000000n
1679
+ :
1680
+ remainder).padStart(9, '0'));
1362
1681
  }
1363
1682
  exports.nanotimestamp2str = nanotimestamp2str;
1364
1683
  /** parse nano timestamp string to bigint value
1365
- - str: nano timestamp string, If it is an empty string, it will return the corresponding empty value (nulls.int64)
1684
+ - str: nano timestamp string, If it is an empty string or 'null', it will return the corresponding empty value (nulls.int64)
1366
1685
  - format?:
1367
1686
  The format string corresponding to the incoming string, the default is `YYYY.MM.DD HH:mm:ss.SSSSSSSSS`
1368
1687
  Seconds are in the format ss (must be included); nanoseconds are in the format SSSSSSSSS (must be included)
1369
1688
  https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
1370
1689
  */
1371
1690
  function str2nanotimestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSSS') {
1372
- if (!str)
1691
+ if (!str || str === 'null')
1373
1692
  return exports.nulls.int64;
1374
1693
  if (str.length !== format.length)
1375
1694
  throw new Error('nanotimestamp string length is not equal to format string length');
@@ -1386,6 +1705,31 @@ function str2nanotimestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSSS') {
1386
1705
  BigInt(str.slice(i_nanosecond_start, i_nanosecond_start + 9)));
1387
1706
  }
1388
1707
  exports.str2nanotimestamp = str2nanotimestamp;
1708
+ function ipaddr2str(buffer, le = true) {
1709
+ let buf = buffer;
1710
+ if (le)
1711
+ buf = buffer.slice().reverse();
1712
+ const i_non_zero = buf.findIndex(x => x);
1713
+ if (i_non_zero !== -1 && i_non_zero < 12) // ipv6 (i_non_zero === -1 或 0 < i_non_zero < 12)
1714
+ return [...buf].map(x => x.toString(16)
1715
+ .padStart(2, '0')).join(':');
1716
+ else // ipv4
1717
+ return buf.subarray(12).join('.');
1718
+ }
1719
+ exports.ipaddr2str = ipaddr2str;
1720
+ function uuid2str(buffer, le = true) {
1721
+ const str = int1282str(buffer, le);
1722
+ return `${str.slice(0, 8)}-${str.slice(8, 12)}-${str.slice(12, 16)}-${str.slice(16, 20)}-${str.slice(20)}`;
1723
+ }
1724
+ exports.uuid2str = uuid2str;
1725
+ function int1282str(buffer, le = true) {
1726
+ let buf = buffer;
1727
+ if (le)
1728
+ buf = buffer.slice().reverse();
1729
+ return [...buf].map(x => x.toString(16)
1730
+ .padStart(2, '0')).join('');
1731
+ }
1732
+ exports.int1282str = int1282str;
1389
1733
  class DDB {
1390
1734
  /**
1391
1735
  Initialize an instance of DolphinDB Client using the WebSocket URL
@@ -1559,9 +1903,9 @@ class DDB {
1559
1903
  - options:
1560
1904
  - urgent?: 决定 `行为标识` 那一行字符串的取值(只适用于 script 和 function)
1561
1905
  - vars?: type === 'variable' 时必传,variable 指令中待上传的变量名
1562
- - handler?: Process messages during this rpc (DdbMessage)
1563
- - parse_object?: 在该次 rpc 期间设置 parse_object, 结束后恢复原有,为 false 时返回的 DdbObj 仅含有 buffer 和 le,
1564
- 不做解析,以便后续转发、序列化
1906
+ - listener?: 处理本次 rpc 期间的消息 (DdbMessage)
1907
+ - parse_object?: 在本次 rpc 期间设置 parse_object, 结束后恢复原有
1908
+ 为 false 时返回的 DdbObj 仅含有 buffer 和 le,不做解析,以便后续转发、序列化
1565
1909
  */
1566
1910
  async rpc(type, { script, func, args = [], vars = [], urgent, listener, parse_object, }) {
1567
1911
  if (!this.websocket)
@@ -1785,5 +2129,4 @@ class DDB {
1785
2129
  exports.DDB = DDB;
1786
2130
  /** little endian (client) */
1787
2131
  DDB.le_client = Boolean(new Uint8Array(Uint32Array.of(1).buffer)[0]);
1788
- exports.default = DDB;
1789
2132
  //# sourceMappingURL=index.js.map