dolphindb 2.0.1102 → 2.0.1103

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
@@ -277,13 +277,13 @@ export declare function second2ms(second: number | null): number | null;
277
277
  export declare function second2str(second: number | null, format?: string): string;
278
278
  export declare function datetime2ms(datetime: number | null): number | null;
279
279
  export declare function datetime2str(datetime: number | null, format?: string): string;
280
+ /** _datetime_formatter.format 会在 date 为 Invalid Date 时抛出错误 */
280
281
  export declare function timestamp2ms(timestamp: bigint | null): number | null;
281
282
  /** format timestamp (bigint) to string
282
283
  - timestamp: bigint value
283
284
  - format?:
284
285
  格式串,默认是 `YYYY.MM.DD HH:mm:ss.SSS` format string, default to `YYYY.MM.DD HH:mm:ss.SSS`
285
- https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
286
- */
286
+ https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens */
287
287
  export declare function timestamp2str(timestamp: bigint | null, format?: string): string;
288
288
  export declare function datehour2ms(datehour: number | null): number | null;
289
289
  export declare function datehour2str(datehour: number | null, format?: string): string;
@@ -293,8 +293,7 @@ export declare function datehour2str(datehour: number | null, format?: string):
293
293
  - format?:
294
294
  对应传入字符串的格式串,默认是 `YYYY.MM.DD HH:mm:ss.SSS`
295
295
  The format string corresponding to the incoming string, the default is `YYYY.MM.DD HH:mm:ss.SSS`
296
- https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
297
- */
296
+ https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens */
298
297
  export declare function str2timestamp(str: string, format?: string): bigint;
299
298
  export declare function nanotime2ns(nanotime: bigint | null): bigint | null;
300
299
  export declare function nanotime2str(nanotime: bigint | null, format?: string): string;
package/index.js CHANGED
@@ -1299,6 +1299,22 @@ export function format(type, value, le, options = {}) {
1299
1299
  }
1300
1300
  return options.decimals === undefined || options.decimals === null ? default_formatter : _formatter;
1301
1301
  })();
1302
+ function format_time(formatter, _null) {
1303
+ if (value === null || value === _null)
1304
+ return inspect(null, options);
1305
+ let str;
1306
+ // formatter 可能会在 value 不属于 new Date() 有效值时,调用 抛出错误,这里统一处理
1307
+ try {
1308
+ str = formatter(value);
1309
+ }
1310
+ catch (error) {
1311
+ if (error instanceof RangeError)
1312
+ str = 'Invalid Date';
1313
+ else
1314
+ throw error;
1315
+ }
1316
+ return options.colors ? str.magenta : str;
1317
+ }
1302
1318
  switch (type) {
1303
1319
  case DdbType.bool:
1304
1320
  return inspect((value === null || value === nulls.int8) ?
@@ -1340,77 +1356,23 @@ export function format(type, value, le, options = {}) {
1340
1356
  :
1341
1357
  default_formatter.format(value);
1342
1358
  case DdbType.date:
1343
- return (value === null || value === nulls.int32) ?
1344
- inspect(null, options)
1345
- :
1346
- options.colors ?
1347
- date2str(value).magenta
1348
- :
1349
- date2str(value);
1359
+ return format_time(date2str, nulls.int32);
1350
1360
  case DdbType.month:
1351
- return (value === null || value === nulls.int32) ?
1352
- inspect(null, options)
1353
- :
1354
- options.colors ?
1355
- month2str(value).magenta
1356
- :
1357
- month2str(value);
1361
+ return format_time(month2str, nulls.int32);
1358
1362
  case DdbType.time:
1359
- return (value === null || value === nulls.int32) ?
1360
- inspect(null, options)
1361
- :
1362
- options.colors ?
1363
- time2str(value).magenta
1364
- :
1365
- time2str(value);
1363
+ return format_time(time2str, nulls.int32);
1366
1364
  case DdbType.minute:
1367
- return (value === null || value === nulls.int32) ?
1368
- inspect(null, options)
1369
- :
1370
- options.colors ?
1371
- minute2str(value).magenta
1372
- :
1373
- minute2str(value);
1365
+ return format_time(minute2str, nulls.int32);
1374
1366
  case DdbType.second:
1375
- return (value === null || value === nulls.int32) ?
1376
- inspect(null, options)
1377
- :
1378
- options.colors ?
1379
- second2str(value).magenta
1380
- :
1381
- second2str(value);
1367
+ return format_time(second2str, nulls.int32);
1382
1368
  case DdbType.datetime:
1383
- return (value === null || value === nulls.int32) ?
1384
- inspect(null, options)
1385
- :
1386
- options.colors ?
1387
- datetime2str(value).magenta
1388
- :
1389
- datetime2str(value);
1369
+ return format_time(datetime2str, nulls.int32);
1390
1370
  case DdbType.timestamp:
1391
- return (value === null || value === nulls.int64) ?
1392
- inspect(null, options)
1393
- :
1394
- options.colors ?
1395
- timestamp2str(value).magenta
1396
- :
1397
- timestamp2str(value);
1371
+ return format_time(timestamp2str, nulls.int64);
1398
1372
  case DdbType.nanotime:
1399
- return (value === null || value === nulls.int64) ?
1400
- inspect(null, options)
1401
- :
1402
- options.colors ?
1403
- nanotime2str(value).magenta
1404
- :
1405
- nanotime2str(value);
1373
+ return format_time(nanotime2str, nulls.int64);
1406
1374
  case DdbType.nanotimestamp:
1407
- return (value === null || value === nulls.int64) ?
1408
- inspect(null, options)
1409
- :
1410
- options.colors ?
1411
- nanotimestamp2str(value).magenta
1412
- :
1413
- nanotimestamp2str(value);
1375
+ return format_time(nanotimestamp2str, nulls.int64);
1414
1376
  case DdbType.float:
1415
1377
  return (value === null || value === nulls.float32) ?
1416
1378
  inspect(null, options)
@@ -1443,13 +1405,7 @@ export function format(type, value, le, options = {}) {
1443
1405
  case DdbType.resource:
1444
1406
  return inspect(value, options);
1445
1407
  case DdbType.datehour:
1446
- return (value === null || value === nulls.int32) ?
1447
- 'null'
1448
- :
1449
- options.colors ?
1450
- datehour2str(value).magenta
1451
- :
1452
- datehour2str(value);
1408
+ return format_time(datehour2str, nulls.int32);
1453
1409
  case DdbType.ipaddr:
1454
1410
  return options.colors ?
1455
1411
  ipaddr2str(value, le).cyan
@@ -1998,6 +1954,7 @@ export function datetime2str(datetime, format = 'YYYY.MM.DD HH:mm:ss') {
1998
1954
  :
1999
1955
  dayjs(datetime2ms(datetime)).format(format);
2000
1956
  }
1957
+ /** _datetime_formatter.format 会在 date 为 Invalid Date 时抛出错误 */
2001
1958
  export function timestamp2ms(timestamp) {
2002
1959
  if (timestamp === null || timestamp === nulls.int64)
2003
1960
  return null;
@@ -2008,8 +1965,7 @@ export function timestamp2ms(timestamp) {
2008
1965
  - timestamp: bigint value
2009
1966
  - format?:
2010
1967
  格式串,默认是 `YYYY.MM.DD HH:mm:ss.SSS` format string, default to `YYYY.MM.DD HH:mm:ss.SSS`
2011
- https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
2012
- */
1968
+ https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens */
2013
1969
  export function timestamp2str(timestamp, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
2014
1970
  return (timestamp === null || timestamp === nulls.int64) ?
2015
1971
  'null'
@@ -2034,8 +1990,7 @@ export function datehour2str(datehour, format = 'YYYY.MM.DDTHH') {
2034
1990
  - format?:
2035
1991
  对应传入字符串的格式串,默认是 `YYYY.MM.DD HH:mm:ss.SSS`
2036
1992
  The format string corresponding to the incoming string, the default is `YYYY.MM.DD HH:mm:ss.SSS`
2037
- https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
2038
- */
1993
+ https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens */
2039
1994
  export function str2timestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
2040
1995
  if (!str || str === 'null')
2041
1996
  return nulls.int64;