ng-prime-tools 1.1.17 → 1.1.18

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.
@@ -6338,21 +6338,81 @@ class PTNotificationComponent {
6338
6338
  if (!notification.date) {
6339
6339
  return '';
6340
6340
  }
6341
- if (notification.date instanceof Date) {
6342
- return this.formatDate(notification.date);
6341
+ const date = this.toDate(notification.date);
6342
+ if (!date) {
6343
+ return String(notification.date);
6343
6344
  }
6344
- return notification.date;
6345
+ return this.formatRelativeDate(date);
6345
6346
  }
6346
6347
  hasMetadata(notification) {
6347
6348
  return !!(notification.date || notification.sender?.trim());
6348
6349
  }
6349
- formatDate(date) {
6350
+ formatRelativeDate(date) {
6351
+ const now = new Date();
6352
+ const differenceMs = now.getTime() - date.getTime();
6353
+ if (differenceMs < 0) {
6354
+ return this.formatAbsoluteDate(date);
6355
+ }
6356
+ const differenceSeconds = Math.floor(differenceMs / 1000);
6357
+ const differenceMinutes = Math.floor(differenceSeconds / 60);
6358
+ const differenceHours = Math.floor(differenceMinutes / 60);
6359
+ const differenceDays = this.getCalendarDayDifference(now, date);
6360
+ if (differenceSeconds < 60) {
6361
+ return "À l'instant";
6362
+ }
6363
+ if (differenceMinutes < 60) {
6364
+ return `Il y a ${differenceMinutes} min`;
6365
+ }
6366
+ if (differenceDays === 0) {
6367
+ return `Il y a ${differenceHours} h`;
6368
+ }
6369
+ if (differenceDays === 1) {
6370
+ return `Hier à ${this.formatTime(date)}`;
6371
+ }
6372
+ if (differenceDays < 7) {
6373
+ return `Il y a ${differenceDays} j`;
6374
+ }
6375
+ return this.formatAbsoluteDate(date);
6376
+ }
6377
+ getCalendarDayDifference(left, right) {
6378
+ const leftDate = new Date(left.getFullYear(), left.getMonth(), left.getDate());
6379
+ const rightDate = new Date(right.getFullYear(), right.getMonth(), right.getDate());
6380
+ const millisecondsPerDay = 24 * 60 * 60 * 1000;
6381
+ return Math.round((leftDate.getTime() - rightDate.getTime()) / millisecondsPerDay);
6382
+ }
6383
+ toDate(value) {
6384
+ if (value instanceof Date) {
6385
+ return Number.isNaN(value.getTime()) ? null : value;
6386
+ }
6387
+ const normalized = value.trim();
6388
+ if (!normalized) {
6389
+ return null;
6390
+ }
6391
+ const parsedDate = new Date(normalized);
6392
+ if (!Number.isNaN(parsedDate.getTime())) {
6393
+ return parsedDate;
6394
+ }
6395
+ const isoWithoutTimezoneMatch = normalized.match(/^(\d{4})-(\d{2})-(\d{2})[T\s](\d{2}):(\d{2})(?::(\d{2}))?(?:\.(\d+))?$/);
6396
+ if (!isoWithoutTimezoneMatch) {
6397
+ return null;
6398
+ }
6399
+ const [, year, month, day, hours, minutes, seconds, fraction] = isoWithoutTimezoneMatch;
6400
+ const milliseconds = fraction
6401
+ ? Number(fraction.padEnd(3, '0').substring(0, 3))
6402
+ : 0;
6403
+ const date = new Date(Number(year), Number(month) - 1, Number(day), Number(hours), Number(minutes), Number(seconds ?? 0), milliseconds);
6404
+ return Number.isNaN(date.getTime()) ? null : date;
6405
+ }
6406
+ formatAbsoluteDate(date) {
6350
6407
  const day = this.pad2(date.getDate());
6351
6408
  const month = this.pad2(date.getMonth() + 1);
6352
6409
  const year = date.getFullYear();
6410
+ return `${day}/${month}/${year} ${this.formatTime(date)}`;
6411
+ }
6412
+ formatTime(date) {
6353
6413
  const hours = this.pad2(date.getHours());
6354
6414
  const minutes = this.pad2(date.getMinutes());
6355
- return `${day}/${month}/${year} ${hours}:${minutes}`;
6415
+ return `${hours}:${minutes}`;
6356
6416
  }
6357
6417
  pad2(value) {
6358
6418
  return String(value).padStart(2, '0');