dolphindb 3.0.205 → 3.0.208

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/i18n/dict.json CHANGED
@@ -106,5 +106,8 @@
106
106
  },
107
107
  "iotany 的 meta vector 长度至少为 2": {
108
108
  "en": "The length of the iotany meta vector is at least 2"
109
+ },
110
+ "value 不能转换为 {{classname}}": {
111
+ "en": "value cannot be converted to {{classname}}"
109
112
  }
110
113
  }
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { InspectOptions as UtilInspectOptions } from 'util';
2
+ import { type Dayjs } from 'dayjs';
2
3
  import { inspect, Lock, type WebSocket, type WebSocketConnectionError } from 'xshell';
3
4
  export declare const nulls: {
4
5
  readonly int8: -128;
@@ -406,7 +407,7 @@ export interface ConvertOptions {
406
407
  /** `'ms'` timestamp 类型转换为字符串表示时显示到秒还是毫秒 */
407
408
  timestamp?: 's' | 'ms';
408
409
  }
409
- export declare function convert(type: DdbType, value: DdbValue, le: boolean, { blob, char, timestamp }?: ConvertOptions): string | number | bigint | boolean | string[] | number[] | DdbFunctionDefValue | DdbDurationValue | DdbDecimal32Value | DdbDecimal64Value | DdbDecimal32VectorValue | DdbDecimal64VectorValue | DdbDecimal128VectorValue | BigInt128Array | DdbSymbolExtendedValue | DdbArrayVectorValue | DdbMatrixValue | Uint8Array<ArrayBufferLike> | Int8Array<ArrayBufferLike> | Int16Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Float32Array<ArrayBufferLike> | Float64Array<ArrayBufferLike> | BigInt64Array<ArrayBufferLike> | Uint8Array<ArrayBufferLike>[] | DdbObj<DdbValue>[] | IotVectorItemValue | DdbDurationVectorValue | DdbTensorValue | DdbChartValue;
410
+ export declare function convert(type: DdbType, value: DdbValue, le: boolean, { blob, char, timestamp }?: ConvertOptions): string | number | bigint | boolean | number[] | string[] | Uint8Array<ArrayBufferLike> | DdbFunctionDefValue | DdbDurationValue | DdbDecimal32Value | DdbDecimal64Value | Int8Array<ArrayBufferLike> | Int16Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Float32Array<ArrayBufferLike> | Float64Array<ArrayBufferLike> | BigInt64Array<ArrayBufferLike> | BigInt128Array | Uint8Array<ArrayBufferLike>[] | DdbObj<DdbValue>[] | IotVectorItemValue | DdbSymbolExtendedValue | DdbArrayVectorValue | DdbDecimal32VectorValue | DdbDecimal64VectorValue | DdbDecimal128VectorValue | DdbDurationVectorValue | DdbMatrixValue | DdbChartValue | DdbTensorValue;
410
411
  /** 转换一个向量到 js 原生数组 */
411
412
  export declare function converts(type: DdbType, value: DdbVectorValue, rows: number, le: boolean, options?: ConvertOptions): any[] | Uint8Array;
412
413
  export declare class DdbVoid extends DdbObj<undefined> {
@@ -431,16 +432,16 @@ export declare class DdbDouble extends DdbObj<number> {
431
432
  constructor(value: number | null);
432
433
  }
433
434
  export declare class DdbDateTime extends DdbObj<number> {
434
- constructor(value: number | null);
435
+ constructor(value?: null | number | string | Date | Dayjs);
435
436
  }
436
437
  export declare class DdbTimeStamp extends DdbObj<bigint> {
437
- /** @example
438
- new DdbTimeStamp(new Date().getTime())
439
- new DdbTimeStamp(dayjs().valueOf()) */
440
- constructor(value?: bigint | null | number);
438
+ constructor(value?: null | number | string | Date | Dayjs);
441
439
  }
442
440
  export declare class DdbNanoTimeStamp extends DdbObj<bigint> {
443
- constructor(value: bigint | null);
441
+ constructor(value?: null | number | string | Date | Dayjs);
442
+ }
443
+ export declare class DdbDate extends DdbObj<number> {
444
+ constructor(value?: null | number | string | Date | Dayjs);
444
445
  }
445
446
  export declare class DdbBlob extends DdbObj<Uint8Array> {
446
447
  constructor(value: Uint8Array | ArrayBuffer);
package/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import dayjs from 'dayjs';
1
+ import { default as dayjs } from 'dayjs';
2
2
  import DayjsCustomParseFormat from 'dayjs/plugin/customParseFormat.js';
3
3
  dayjs.extend(DayjsCustomParseFormat);
4
4
  import ipaddrjs from 'ipaddr.js';
5
5
  const { fromByteArray: buf2ipaddr } = ipaddrjs;
6
- import { concat, assert, inspect, typed_array_to_buffer, connect_websocket, Lock, genid, seq, zip_object, WebSocketOpen, WebSocketClosed, WebSocketClosing, decode, delay } from 'xshell';
6
+ import { concat, assert, inspect, typed_array_to_buffer, connect_websocket, Lock, genid, seq, zip_object, WebSocketOpen, WebSocketClosed, WebSocketClosing, decode, delay, check } from 'xshell';
7
7
  import { t } from "./i18n/index.js";
8
8
  export const nulls = {
9
9
  int8: -0x80, // -128
@@ -2195,24 +2195,16 @@ export class DdbDateTime extends DdbObj {
2195
2195
  super({
2196
2196
  form: DdbForm.scalar,
2197
2197
  type: DdbType.datetime,
2198
- value
2198
+ value: get_ddb_time_value('DdbDateTime', value)
2199
2199
  });
2200
2200
  }
2201
2201
  }
2202
2202
  export class DdbTimeStamp extends DdbObj {
2203
- /** @example
2204
- new DdbTimeStamp(new Date().getTime())
2205
- new DdbTimeStamp(dayjs().valueOf()) */
2206
2203
  constructor(value) {
2207
- if (value === undefined)
2208
- value = new Date().getTime();
2209
- if (typeof value === 'number')
2210
- value = BigInt(-(1000 * 60 * new Date(value).getTimezoneOffset()) +
2211
- value);
2212
2204
  super({
2213
2205
  form: DdbForm.scalar,
2214
2206
  type: DdbType.timestamp,
2215
- value
2207
+ value: get_ddb_time_value('DdbTimeStamp', value)
2216
2208
  });
2217
2209
  }
2218
2210
  }
@@ -2221,10 +2213,46 @@ export class DdbNanoTimeStamp extends DdbObj {
2221
2213
  super({
2222
2214
  form: DdbForm.scalar,
2223
2215
  type: DdbType.nanotimestamp,
2224
- value
2216
+ value: get_ddb_time_value('DdbNanoTimeStamp', value)
2225
2217
  });
2226
2218
  }
2227
2219
  }
2220
+ export class DdbDate extends DdbObj {
2221
+ constructor(value) {
2222
+ super({
2223
+ form: DdbForm.scalar,
2224
+ type: DdbType.date,
2225
+ value: get_ddb_time_value('DdbDate', value)
2226
+ });
2227
+ }
2228
+ }
2229
+ function get_ddb_time_value(classname, value) {
2230
+ if (value === null)
2231
+ return null;
2232
+ if (classname === 'DdbNanoTimeStamp' && typeof value === 'string')
2233
+ return str2nanotimestamp(value);
2234
+ let date;
2235
+ if (value === undefined)
2236
+ date = new Date();
2237
+ else if (typeof value === 'number' || typeof value === 'string')
2238
+ date = new Date(value);
2239
+ else if (value instanceof Date)
2240
+ date = value;
2241
+ else if (dayjs.isDayjs(value))
2242
+ date = new Date(value.valueOf());
2243
+ else
2244
+ throw new Error(t('value 不能转换为 {{classname}}', { classname }));
2245
+ switch (classname) {
2246
+ case 'DdbDateTime':
2247
+ return (date.getTime() - 1000 * 60 * date.getTimezoneOffset()) / 1000;
2248
+ case 'DdbTimeStamp':
2249
+ return BigInt(date.getTime() - 1000 * 60 * date.getTimezoneOffset());
2250
+ case 'DdbNanoTimeStamp':
2251
+ return BigInt(date.getTime() - 1000 * 60 * date.getTimezoneOffset()) * 1000000n;
2252
+ case 'DdbDate':
2253
+ return Math.floor((date.getTime() - 1000 * 60 * date.getTimezoneOffset()) / (1000 * 3600 * 24));
2254
+ }
2255
+ }
2228
2256
  export class DdbBlob extends DdbObj {
2229
2257
  constructor(value) {
2230
2258
  assert(value, t('new DdbBlob 不能传空的 value'));
@@ -2647,12 +2675,12 @@ export function nanotimestamp2str(nanotimestamp, format = 'YYYY.MM.DD HH:mm:ss.S
2647
2675
  export function str2nanotimestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSSS') {
2648
2676
  if (!str || str === 'null')
2649
2677
  return nulls.int64;
2650
- assert(str.length === format.length, t('nanotimestamp 字符串长度必须等于格式串长度'));
2678
+ check(str.length === format.length, t('nanotimestamp 字符串长度必须等于格式串长度'));
2651
2679
  const i_second_start = format.indexOf('ss');
2652
- assert(i_second_start !== -1, t('格式串必须包含秒的格式 (ss)'));
2680
+ check(i_second_start !== -1, t('格式串必须包含秒的格式 (ss)'));
2653
2681
  const i_second_end = i_second_start + 2;
2654
2682
  const i_nanosecond_start = format.indexOf('SSSSSSSSS', i_second_end);
2655
- assert(i_nanosecond_start !== -1, t('格式串必须包含纳秒的格式 (SSSSSSSSS)'));
2683
+ check(i_nanosecond_start !== -1, t('格式串必须包含纳秒的格式 (SSSSSSSSS)'));
2656
2684
  const ms = dayjs(str.slice(0, i_second_end), format.slice(0, i_second_end)).valueOf();
2657
2685
  return (BigInt(-(1000 * 60 * new Date(ms).getTimezoneOffset()) +
2658
2686
  ms) * 1000000n