@bbn/bbn 2.0.150 → 2.0.152

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.
@@ -15,8 +15,7 @@ export default class bbnDtDateTime extends bbnDt {
15
15
  value = new Temporal.PlainDateTime(y.getFullYear(), y.getMonth() + 1, y.getDate(), y.getHours(), y.getMinutes(), y.getSeconds(), y.getMilliseconds());
16
16
  }
17
17
  else if (typeof y === 'number') {
18
- const d = new Date(y);
19
- value = new Temporal.PlainDateTime(d.getFullYear(), d.getMonth() + 1, d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
18
+ value = Temporal.Instant.fromEpochMilliseconds(y).toZonedDateTimeISO(bbnDt.systemTimeZoneId).toPlainDateTime();
20
19
  }
21
20
  else {
22
21
  throw new Error('Invalid value for bbnDtDateTime');
@@ -30,7 +29,7 @@ export default class bbnDtDateTime extends bbnDt {
30
29
  }
31
30
  format(format) {
32
31
  // long
33
- if (format === true) {
32
+ if ([true, 'long', 'full'].includes(format)) {
34
33
  format = getRow(bbn.dt.locales.date, { year: 'numeric', month: 'long', day: 'long', weekday: 'long', hour: '2-digit', minute: '2-digit', second: undefined }).pattern;
35
34
  }
36
35
  // short
@@ -7,12 +7,13 @@ export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
7
7
  readonly __bbnNoData = true;
8
8
  readonly __isBbnDt = true;
9
9
  isValid: boolean;
10
+ defaultValue: TimeProperties | undefined;
10
11
  constructor(value?: TValue);
11
12
  setValid(isValid: boolean): void;
12
13
  get value(): TValue | undefined;
13
14
  get hasFullDate(): boolean;
14
15
  /** System time zone ID (e.g. "Europe/Rome") */
15
- private static readonly systemTimeZoneId;
16
+ static readonly systemTimeZoneId: string;
16
17
  /**
17
18
  * Convert this.value (PlainDate, PlainTime, PlainDateTime, YearMonth,
18
19
  * MonthDay, ZonedDateTime) into epoch milliseconds, using the system
@@ -51,10 +52,7 @@ export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
51
52
  isAfterOrSame(other: any, unit?: string): boolean;
52
53
  isSame(other: any, unit?: string): boolean;
53
54
  equals(other: any): boolean;
54
- toJSON(): {
55
- kind: bbnDtKind;
56
- value: string;
57
- };
55
+ toJSON(): string;
58
56
  toString(): string;
59
57
  year(v?: any): number | bbnDt<any>;
60
58
  month(v?: any): number | bbnDt<any>;
@@ -62,6 +60,7 @@ export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
62
60
  hour(v?: any): number | bbnDt<any>;
63
61
  minute(v?: any): number | bbnDt<any>;
64
62
  second(v?: any): number | bbnDt<any>;
63
+ millisecond(v?: any): number | bbnDt<any>;
65
64
  weekday(v?: any, past?: any): number | bbnDt<any>;
66
65
  date(v?: any): string | bbnDt<any>;
67
66
  datetime(v?: any): string | bbnDt<any>;
@@ -87,6 +86,7 @@ export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
87
86
  get mm(): string;
88
87
  get I(): string;
89
88
  get SS(): string;
89
+ get SSS(): string;
90
90
  get S(): string;
91
91
  get WW(): string;
92
92
  get W(): string;
@@ -110,7 +110,7 @@ export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
110
110
  setWeekday(weekday: number | string, past?: boolean, locale?: string): bbnDt<any>;
111
111
  diff(date: any, unit?: string, abs?: boolean): number;
112
112
  guessUnit(valueInMs: number): string | null;
113
- fromNow(unit?: string): any;
113
+ fromNow(unit?: string): string;
114
114
  fromDate(date: any, unit?: string): string;
115
115
  startOf(unit?: string): bbnDt<any>;
116
116
  endOf(unit?: string): bbnDt<any>;
@@ -51,6 +51,20 @@ export class bbnDt {
51
51
  configurable: true
52
52
  });
53
53
  __classPrivateFieldSet(this, _bbnDt_value, value, "f");
54
+ const d = new Date();
55
+ Object.defineProperty(this, 'defaultValue', {
56
+ value: {
57
+ year: d.getFullYear(),
58
+ month: d.getMonth() + 1,
59
+ day: d.getDate(),
60
+ hour: d.getHours(),
61
+ minute: d.getMinutes(),
62
+ second: d.getSeconds(),
63
+ millisecond: d.getMilliseconds()
64
+ },
65
+ writable: false,
66
+ configurable: false
67
+ });
54
68
  }
55
69
  setValid(isValid) {
56
70
  Object.defineProperty(this, 'isValid', {
@@ -77,52 +91,57 @@ export class bbnDt {
77
91
  * - month-day → that month/day in *this year* at 00:00 in system tz
78
92
  */
79
93
  toEpochMs() {
94
+ if (this.kind === 'zoned') {
95
+ const v = this.value;
96
+ return v.toInstant().epochMilliseconds;
97
+ }
80
98
  const tz = bbnDt.systemTimeZoneId;
99
+ let iso;
81
100
  switch (this.kind) {
82
- case 'zoned': {
83
- const v = this.value;
84
- return v.toInstant().epochMilliseconds;
85
- }
86
101
  case 'dateTime': {
87
102
  const v = this.value;
88
103
  // RFC 9557 string: "YYYY-MM-DDTHH:mm:ss[Europe/Rome]"
89
- const iso = `${v.toString()}[${tz}]`;
90
- const zdt = Temporal.ZonedDateTime.from(iso);
91
- return zdt.toInstant().epochMilliseconds;
104
+ return v.toZonedDateTime(tz, { disambiguation: "earlier" }).toInstant().epochMilliseconds;
105
+ iso = `${v.toString()}[${tz}]`;
92
106
  }
93
107
  case 'date': {
94
108
  const d = this.value;
109
+ return d.toZonedDateTime({
110
+ timeZone: tz,
111
+ plainTime: {
112
+ hour: this.defaultValue.hour,
113
+ minute: this.defaultValue.minute,
114
+ second: this.defaultValue.second,
115
+ millisecond: this.defaultValue.millisecond
116
+ }
117
+ }).toInstant().epochMilliseconds;
118
+ const today = new Date();
95
119
  // "YYYY-MM-DDT00:00[Europe/Rome]"
96
- const iso = `${d.toString()}T00:00[${tz}]`;
97
- const zdt = Temporal.ZonedDateTime.from(iso);
98
- return zdt.toInstant().epochMilliseconds;
120
+ iso = `${d.toString()}T00:00[${tz}]`;
99
121
  }
100
122
  case 'time': {
101
123
  const t = this.value;
102
124
  const today = Temporal.Now.plainDateISO();
103
125
  // "YYYY-MM-DDTHH:mm[:ss][Europe/Rome]"
104
- const iso = `${today.toString()}T${t.toString()}[${tz}]`;
105
- const zdt = Temporal.ZonedDateTime.from(iso);
106
- return zdt.toInstant().epochMilliseconds;
126
+ iso = `${today.toString()}T${t.toString()}[${tz}]`;
107
127
  }
108
128
  case 'yearMonth': {
109
129
  const ym = this.value;
110
130
  const d = ym.toPlainDate({ day: 1 }); // first day of month
111
- const iso = `${d.toString()}T00:00[${tz}]`;
112
- const zdt = Temporal.ZonedDateTime.from(iso);
113
- return zdt.toInstant().epochMilliseconds;
131
+ iso = `${d.toString()}T00:00[${tz}]`;
114
132
  }
115
133
  case 'monthDay': {
116
134
  const md = this.value;
117
135
  const today = Temporal.Now.plainDateISO();
118
136
  const d = md.toPlainDate({ year: today.year }); // current year
119
- const iso = `${d.toString()}T00:00[${tz}]`;
120
- const zdt = Temporal.ZonedDateTime.from(iso);
121
- return zdt.toInstant().epochMilliseconds;
137
+ iso = `${d.toString()}T00:00[${tz}]`;
122
138
  }
123
- default:
124
- throw new Error(`Unsupported kind '${this.kind}' in toEpochMs`);
125
139
  }
140
+ if (iso) {
141
+ const zdt = Temporal.ZonedDateTime.from(iso);
142
+ return zdt.toInstant().epochMilliseconds; // - Math.round(zdt.offsetNanoseconds / 1000000);
143
+ }
144
+ throw new Error(`Unsupported kind '${this.kind}' in toEpochMs`);
126
145
  }
127
146
  /**
128
147
  * "Now" value in the same *kind* as this instance.
@@ -326,13 +345,10 @@ export class bbnDt {
326
345
  }
327
346
  // ---- Serialization ----
328
347
  toJSON() {
329
- return {
330
- kind: this.kind,
331
- value: String(this.value)
332
- };
348
+ return this.format('YYYY-MM-DDTHH:II:SS.SSS[Z]');
333
349
  }
334
350
  toString() {
335
- return String(this.value);
351
+ return new Date(this.valueOf()).toString();
336
352
  }
337
353
  year(v) {
338
354
  if (!this.value) {
@@ -421,6 +437,22 @@ export class bbnDt {
421
437
  }
422
438
  return this.value.second;
423
439
  }
440
+ millisecond(v) {
441
+ if (!this.value) {
442
+ return undefined;
443
+ }
444
+ if (!('millisecond' in this.value)) {
445
+ if (this.hasFullDate) {
446
+ return 0;
447
+ }
448
+ throw new Error('millisecond() is not supported for this type');
449
+ }
450
+ if ((v !== undefined) && !(v instanceof Event)) {
451
+ const d = this.value.with({ millisecond: v });
452
+ return new this.constructor(d);
453
+ }
454
+ return this.value.millisecond;
455
+ }
424
456
  weekday(v, past) {
425
457
  if (!this.value) {
426
458
  return undefined;
@@ -567,7 +599,7 @@ export class bbnDt {
567
599
  return h < 10 ? '0' + h.toString() : h.toString();
568
600
  }
569
601
  else if (this.hasFullDate) {
570
- return '00';
602
+ return this.defaultValue.hour.toString().padStart(2, '0');
571
603
  }
572
604
  return undefined;
573
605
  }
@@ -586,7 +618,7 @@ export class bbnDt {
586
618
  return i < 10 ? '0' + i.toString() : i.toString();
587
619
  }
588
620
  else if (this.hasFullDate) {
589
- return '00';
621
+ return this.defaultValue.minute.toString().padStart(2, '0');
590
622
  }
591
623
  return undefined;
592
624
  }
@@ -596,7 +628,7 @@ export class bbnDt {
596
628
  return i < 10 ? '0' + i.toString() : i.toString();
597
629
  }
598
630
  else if (this.hasFullDate) {
599
- return '00';
631
+ return this.defaultValue.minute.toString().padStart(2, '0');
600
632
  }
601
633
  return undefined;
602
634
  }
@@ -615,7 +647,17 @@ export class bbnDt {
615
647
  return s < 10 ? '0' + s.toString() : s.toString();
616
648
  }
617
649
  else if (this.hasFullDate) {
618
- return '00';
650
+ return this.defaultValue.second.toString().padStart(2, '0');
651
+ }
652
+ return undefined;
653
+ }
654
+ get SSS() {
655
+ if ('millisecond' in this.value) {
656
+ const s = parseInt(this.millisecond().toString());
657
+ return s < 10 ? '00' + s.toString() : (s < 100 ? '0' + s.toString() : s.toString());
658
+ }
659
+ else if (this.hasFullDate) {
660
+ return this.defaultValue.millisecond.toString().padStart(3, '0');
619
661
  }
620
662
  return undefined;
621
663
  }
@@ -845,7 +887,7 @@ export class bbnDt {
845
887
  guessUnit(valueInMs) {
846
888
  const absDiff = Math.abs(valueInMs);
847
889
  for (const [shortUnit, rtfUnit, ms] of units) {
848
- if ((absDiff >= ms) || (rtfUnit === 'second')) {
890
+ if ((absDiff >= ms) || (rtfUnit === 'millisecond')) {
849
891
  return shortUnit;
850
892
  }
851
893
  }
@@ -12,15 +12,7 @@ export default class bbnDtDuration {
12
12
  hours(remaining?: boolean): number;
13
13
  minutes(remaining?: boolean): number;
14
14
  seconds(remaining?: boolean): number;
15
- toJSON(): {
16
- years: number;
17
- months: number;
18
- days: number;
19
- hours: number;
20
- minutes: number;
21
- seconds: number;
22
- milliseconds: number;
23
- };
15
+ toJSON(): string;
24
16
  /**
25
17
  * Returns the full duration expressed as X (float), like Day.js.
26
18
  */
@@ -83,15 +83,7 @@ class bbnDtDuration {
83
83
  // "asX" conversions
84
84
  // -----------------------
85
85
  toJSON() {
86
- return {
87
- years: this.years(true),
88
- months: this.months(true),
89
- days: this.days(true),
90
- hours: this.hours(true),
91
- minutes: this.minutes(true),
92
- seconds: this.seconds(true),
93
- milliseconds: this.toMilliseconds()
94
- };
86
+ return this.value.toJSON();
95
87
  }
96
88
  /**
97
89
  * Returns the full duration expressed as X (float), like Day.js.
@@ -6,7 +6,7 @@ export default class bbnDtZoned extends bbnDt {
6
6
  let value;
7
7
  if (!z) {
8
8
  const date = new Date();
9
- value = new Temporal.ZonedDateTime(BigInt(date.getTime() * 1000000), Intl.DateTimeFormat().resolvedOptions().timeZone);
9
+ value = Temporal.Now.zonedDateTimeISO(Intl.DateTimeFormat().resolvedOptions().timeZone);
10
10
  }
11
11
  else if (y === undefined) {
12
12
  if (z instanceof Temporal.ZonedDateTime) {
@@ -16,8 +16,7 @@ export default class bbnDtZoned extends bbnDt {
16
16
  value = fromJsDate(z, true);
17
17
  }
18
18
  else if (typeof z === 'number') {
19
- const d = new Date(z);
20
- value = fromJsDate(d, true);
19
+ value = Temporal.Instant.fromEpochMilliseconds(z).toZonedDateTimeISO(Intl.DateTimeFormat().resolvedOptions().timeZone);
21
20
  }
22
21
  else {
23
22
  throw new Error('Invalid value for bbnDtZoned');
@@ -476,7 +476,6 @@ export default function parse(input, format, cls = 'auto', force, locale) {
476
476
  }
477
477
  ];
478
478
  function parseWithFormat(fmt, cls = 'auto') {
479
- var _a;
480
479
  const currentDate = new bbnDtDateTime();
481
480
  const ctx = {
482
481
  year: currentDate.year(),
@@ -603,7 +602,7 @@ export default function parse(input, format, cls = 'auto', force, locale) {
603
602
  try {
604
603
  pdt = new T.PlainDateTime(ctx.year, ctx.month, ctx.day, ctx.hour, ctx.minute, ctx.second, ctx.ms * 1000000);
605
604
  }
606
- catch (_b) {
605
+ catch (_a) {
607
606
  throw new Error('Invalid date/time components');
608
607
  }
609
608
  if (ctx.timeZone) {
@@ -613,7 +612,7 @@ export default function parse(input, format, cls = 'auto', force, locale) {
613
612
  }
614
613
  else {
615
614
  const utcMs = Date.UTC(ctx.year, ctx.month - 1, ctx.day, ctx.hour, ctx.minute, ctx.second, ctx.ms);
616
- const epochMs = utcMs - ((_a = ctx.offsetMinutes) !== null && _a !== void 0 ? _a : 0) * 60000;
615
+ const epochMs = utcMs; // - (ctx.offsetMinutes ?? 0) * 60_000;
617
616
  dtObj = new bbnDtZoned(T.Instant.fromEpochMilliseconds(epochMs).toZonedDateTimeISO(T.Now.timeZoneId()));
618
617
  }
619
618
  }
@@ -1,6 +1,6 @@
1
- declare const units: [string, Intl.RelativeTimeFormatUnit, number][];
1
+ declare const units: [TimeFormatSymbol, TimeFormatUnit, number][];
2
2
  declare const unitsCorrespondence: {
3
- [key: string]: string;
3
+ [key: string]: TimeFormatSymbol;
4
4
  };
5
5
  declare const unitsMap: {
6
6
  [key: string]: string;
@@ -5,7 +5,8 @@ const units = [
5
5
  ['d', "day", 24 * 60 * 60 * 1000],
6
6
  ['h', "hour", 60 * 60 * 1000],
7
7
  ['i', "minute", 60 * 1000],
8
- ['s', "second", 1000]
8
+ ['s', "second", 1000],
9
+ ['l', "millisecond", 1]
9
10
  ];
10
11
  const unitsCorrespondence = {
11
12
  'years': 'y',
@@ -60,6 +61,9 @@ const unitsCorrespondence = {
60
61
  'mn': 'i',
61
62
  'mm': 'i',
62
63
  'min': 'i',
64
+ 'ms': 'l',
65
+ 'SSS': 'l',
66
+ 'sss': 'l',
63
67
  'SS': 's',
64
68
  'ss': 's',
65
69
  'seconds': 's',
@@ -97,6 +101,7 @@ const unitsMap = {
97
101
  'h': 'Hours',
98
102
  'i': 'Minutes',
99
103
  's': 'Seconds',
104
+ 'l': 'Milliseconds',
100
105
  'z': 'Offset'
101
106
  };
102
107
  const formatsMap = {
package/dist/dt.js CHANGED
@@ -190,7 +190,7 @@ const dt = (value, inputFormat = null, cls = 'auto') => {
190
190
  }
191
191
  }
192
192
  if (typeof value === 'number') {
193
- return new bbnDtDateTime(value < 99999999999 ? value * 1000 : value);
193
+ return new bbnDtDateTime(Math.abs(value) < 99999999999 ? value * 1000 : value);
194
194
  }
195
195
  else if (value.__isBbnDt) {
196
196
  return value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.150",
3
+ "version": "2.0.152",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",