@bbn/bbn 2.0.152 → 2.0.153

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.
@@ -43,7 +43,7 @@ export default class bbnDtDateTime extends bbnDt {
43
43
  return '';
44
44
  }
45
45
  const date = new Date(this.year(), this.month() - 1, this.day(), this.hour(), this.minute(), this.second());
46
- const opt = Object.assign(Object.assign({ year: 'numeric', month: long ? 'long' : 'numeric', day: 'numeric' }, (weekday ? { weekday: (long ? 'long' : 'short') } : {})), (withTime ? { hour: (long ? '2-digit' : 'numeric'), minute: '2-digit' } : {}));
46
+ const opt = Object.assign(Object.assign({ year: 'numeric', month: long ? 'long' : '2-digit', day: '2-digit' }, (weekday ? { weekday: (long ? 'long' : 'short') } : {})), (withTime ? { hour: (long ? '2-digit' : 'numeric'), minute: '2-digit' } : {}));
47
47
  const d = new Intl.DateTimeFormat([bbn.env.lang, ...navigator.languages], opt);
48
48
  return d.format(date);
49
49
  }
@@ -53,6 +53,7 @@ export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
53
53
  isSame(other: any, unit?: string): boolean;
54
54
  equals(other: any): boolean;
55
55
  toJSON(): string;
56
+ timezone(d: any): string | bbnDt<any>;
56
57
  toString(): string;
57
58
  year(v?: any): number | bbnDt<any>;
58
59
  month(v?: any): number | bbnDt<any>;
@@ -111,7 +112,7 @@ export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
111
112
  diff(date: any, unit?: string, abs?: boolean): number;
112
113
  guessUnit(valueInMs: number): string | null;
113
114
  fromNow(unit?: string): string;
114
- fromDate(date: any, unit?: string): string;
115
+ fromDate(date: bbnDt<any>, unit?: string): string;
115
116
  startOf(unit?: string): bbnDt<any>;
116
117
  endOf(unit?: string): bbnDt<any>;
117
118
  clone(): bbnDt<any>;
@@ -101,7 +101,7 @@ export class bbnDt {
101
101
  case 'dateTime': {
102
102
  const v = this.value;
103
103
  // RFC 9557 string: "YYYY-MM-DDTHH:mm:ss[Europe/Rome]"
104
- return v.toZonedDateTime(tz, { disambiguation: "earlier" }).toInstant().epochMilliseconds;
104
+ return v.toZonedDateTime(tz).toInstant().epochMilliseconds;
105
105
  iso = `${v.toString()}[${tz}]`;
106
106
  }
107
107
  case 'date': {
@@ -345,7 +345,45 @@ export class bbnDt {
345
345
  }
346
346
  // ---- Serialization ----
347
347
  toJSON() {
348
- return this.format('YYYY-MM-DDTHH:II:SS.SSS[Z]');
348
+ if ('toZonedDateTime' in __classPrivateFieldGet(this, _bbnDt_value, "f")) {
349
+ const zdt = __classPrivateFieldGet(this, _bbnDt_value, "f").toZonedDateTime(bbnDt.systemTimeZoneId);
350
+ const zdt2 = zdt.withTimeZone('UTC');
351
+ return bbn.dt(zdt2).format('YYYY-MM-DDTHH:II:SS.SSS') + 'Z';
352
+ }
353
+ if (this.kind === 'zoned') {
354
+ return this.timezone('America/New_York').format('YYYY-MM-DDTHH:II:SS.SSS') + 'Z';
355
+ }
356
+ }
357
+ timezone(d) {
358
+ switch (this.kind) {
359
+ case 'zoned':
360
+ if (d === undefined) {
361
+ const v = this.value;
362
+ return v.timeZoneId;
363
+ }
364
+ else {
365
+ const v = this.value;
366
+ const newZdt = v.withTimeZone(d);
367
+ return bbn.dt(newZdt);
368
+ }
369
+ break;
370
+ case 'dateTime':
371
+ if (d === undefined) {
372
+ const v = this.value;
373
+ const iso = `${v.toString()}[${bbnDt.systemTimeZoneId}]`;
374
+ const zdt = Temporal.ZonedDateTime.from(iso);
375
+ return zdt.timeZoneId;
376
+ }
377
+ else {
378
+ const v = this.value;
379
+ const iso = `${v.toString()}[${d}]`;
380
+ const zdt = Temporal.ZonedDateTime.from(iso);
381
+ return bbn.dt(zdt);
382
+ }
383
+ break;
384
+ default:
385
+ throw new Error(`timezone() is not supported for kind '${this.kind}'`);
386
+ }
349
387
  }
350
388
  toString() {
351
389
  return new Date(this.valueOf()).toString();
@@ -502,7 +540,7 @@ export class bbnDt {
502
540
  if (!this.value) {
503
541
  return '';
504
542
  }
505
- return bbn.dt.locales.formatters.short.format(new Date(this.toEpochMs()));
543
+ return bbn.dt.locales.formatters[long ? 'long' : 'numeric'].format(new Date(this.toEpochMs()));
506
544
  }
507
545
  ftime(withSeconds = true) {
508
546
  if (!this.value) {
@@ -514,10 +552,25 @@ export class bbnDt {
514
552
  if (!this.value) {
515
553
  return undefined;
516
554
  }
517
- if (!('weekOfYear' in this.value)) {
518
- throw new Error('week() is not supported for this type');
519
- }
520
- return this.value.weekOfYear;
555
+ const year = this.year();
556
+ // Normalize to UTC midnight to avoid DST issues
557
+ const d = new Date(Date.UTC(year, this.month() - 1, this.day()));
558
+ const jan1 = new Date(Date.UTC(year, 0, 1));
559
+ const MS_PER_DAY = 24 * 60 * 60 * 1000;
560
+ const diffDays = Math.floor((d.getTime() - jan1.getTime()) / MS_PER_DAY); // 0 for Jan 1
561
+ const jan1Dow = jan1.getUTCDay(); // 0–6, Sunday=0
562
+ // How many days long is week 1?
563
+ // Week 1 starts on Jan 1 and ends the day before the first `weekStart` after Jan 1.
564
+ let offset = (bbn.dt.locales.weekStart - jan1Dow + 7) % 7;
565
+ const firstWeekLength = offset === 0 ? 7 : offset;
566
+ // Still in the first (possibly partial) week
567
+ if (diffDays < firstWeekLength) {
568
+ return 1;
569
+ }
570
+ // Remaining days after week 1
571
+ const remainingDays = diffDays - firstWeekLength;
572
+ // Each full block of 7 days is one more week
573
+ return 2 + Math.floor(remainingDays / 7);
521
574
  }
522
575
  get YYYY() {
523
576
  if ('year' in this.value) {
@@ -746,13 +799,16 @@ export class bbnDt {
746
799
  const diffDays = startThis.diff(startNow, "day");
747
800
  const rtf = new Intl.RelativeTimeFormat(bbn.env.lang, { numeric: "auto" });
748
801
  let phrase;
749
- if (diffDays >= -6 && diffDays <= 6) {
802
+ if (Math.abs(diffDays) <= 6) {
750
803
  phrase = rtf.format(diffDays, "day");
751
804
  }
752
- else {
805
+ else if (Math.abs(diffDays) <= 30) {
753
806
  const diffWeeks = Math.floor(diffDays / 7);
754
807
  phrase = rtf.format(diffWeeks, "week");
755
808
  }
809
+ else {
810
+ return this.fdate();
811
+ }
756
812
  return `${phrase} ${this.ftime()}`;
757
813
  }
758
814
  matchFormat(value, format) {
@@ -895,7 +951,7 @@ export class bbnDt {
895
951
  }
896
952
  fromNow(unit = '') {
897
953
  const nowValue = bbnDt.nowForKind(this.kind);
898
- const temp = bbn.dt(nowValue, null, this.kind);
954
+ const temp = bbn.dt(undefined, null, this.kind);
899
955
  const rawDiffMs = this.diff(temp);
900
956
  const chosenUnit = unitsCorrespondence[unit] || this.guessUnit(rawDiffMs);
901
957
  if (!chosenUnit) {
@@ -235,7 +235,9 @@ const buildLocaleFromIntl = () => {
235
235
  const fmtWeekShort = new Intl.DateTimeFormat(langs, { weekday: 'short' });
236
236
  const fmtShort = new Intl.DateTimeFormat(langs, { dateStyle: 'short', timeStyle: 'short' });
237
237
  const fmtLong = new Intl.DateTimeFormat(langs, { dateStyle: 'long', timeStyle: 'long' });
238
+ const fmtNum = new Intl.DateTimeFormat(langs, { year: 'numeric', month: 'numeric', day: 'numeric' });
238
239
  const rtf = new Intl.RelativeTimeFormat(langs, { numeric: 'auto' });
240
+ const locale = new Intl.Locale(langs[0]);
239
241
  // Create 12 dates for months (2020 chosen arbitrarily)
240
242
  const monthsLong = [];
241
243
  const monthsShort = [];
@@ -259,12 +261,15 @@ const buildLocaleFromIntl = () => {
259
261
  formatters: {
260
262
  short: fmtShort,
261
263
  long: fmtLong,
264
+ numeric: fmtNum,
262
265
  relative: rtf
263
266
  },
267
+ firstDayOfWeek: locale.weekInfo.firstDay,
264
268
  monthsLong,
265
269
  monthsShort,
266
270
  weekdaysLong,
267
271
  weekdaysShort,
272
+ locale,
268
273
  date,
269
274
  time,
270
275
  datetime
@@ -476,6 +476,7 @@ export default function parse(input, format, cls = 'auto', force, locale) {
476
476
  }
477
477
  ];
478
478
  function parseWithFormat(fmt, cls = 'auto') {
479
+ var _a;
479
480
  const currentDate = new bbnDtDateTime();
480
481
  const ctx = {
481
482
  year: currentDate.year(),
@@ -602,7 +603,7 @@ export default function parse(input, format, cls = 'auto', force, locale) {
602
603
  try {
603
604
  pdt = new T.PlainDateTime(ctx.year, ctx.month, ctx.day, ctx.hour, ctx.minute, ctx.second, ctx.ms * 1000000);
604
605
  }
605
- catch (_a) {
606
+ catch (_b) {
606
607
  throw new Error('Invalid date/time components');
607
608
  }
608
609
  if (ctx.timeZone) {
@@ -612,7 +613,7 @@ export default function parse(input, format, cls = 'auto', force, locale) {
612
613
  }
613
614
  else {
614
615
  const utcMs = Date.UTC(ctx.year, ctx.month - 1, ctx.day, ctx.hour, ctx.minute, ctx.second, ctx.ms);
615
- const epochMs = utcMs; // - (ctx.offsetMinutes ?? 0) * 60_000;
616
+ const epochMs = utcMs - ((_a = ctx.offsetMinutes) !== null && _a !== void 0 ? _a : 0) * 60000;
616
617
  dtObj = new bbnDtZoned(T.Instant.fromEpochMilliseconds(epochMs).toZonedDateTimeISO(T.Now.timeZoneId()));
617
618
  }
618
619
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.152",
3
+ "version": "2.0.153",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",