@trackunit/date-and-time-utils 0.0.30 → 0.0.33

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.
Files changed (3) hide show
  1. package/index.cjs.js +22 -11
  2. package/index.esm.js +22 -11
  3. package/package.json +1 -1
package/index.cjs.js CHANGED
@@ -782,32 +782,43 @@ const isEqualUtil = (from, to) => {
782
782
  * Output: "1 year ago"
783
783
  */
784
784
  const timeSinceAuto = (from, to, timeZone, locale) => {
785
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
786
- largestUnit: "years",
787
- smallestUnit: "seconds",
788
- }));
785
+ let since;
786
+ try {
787
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
788
+ largestUnit: "years",
789
+ smallestUnit: "seconds",
790
+ });
791
+ }
792
+ catch (error) {
793
+ // eslint-disable-next-line no-console
794
+ console.warn(new Error("Not a valid duration", { cause: error }));
795
+ return "";
796
+ }
797
+ const duration = polyfill.Temporal.Duration.from(since);
789
798
  if (duration.blank) {
790
799
  return getRelativeTimeFormat(locale).format(-1, "seconds");
791
800
  }
792
801
  if (duration.years !== 0) {
793
- return timeSinceInYears(from, to, timeZone, locale);
802
+ return getRelativeTimeFormat(locale).format(duration.years, "years");
794
803
  }
795
804
  if (duration.months !== 0) {
796
- return timeSinceInMonths(from, to, timeZone, locale);
805
+ return getRelativeTimeFormat(locale).format(duration.months, "months");
797
806
  }
798
807
  if (duration.days !== 0) {
799
- return timeSinceInDays(from, to, timeZone, locale);
808
+ return getRelativeTimeFormat(locale).format(duration.days, "days");
800
809
  }
801
810
  if (duration.hours !== 0) {
802
- return timeSinceInHours(from, to, timeZone, locale);
811
+ return getRelativeTimeFormat(locale).format(duration.hours, "hours");
803
812
  }
804
813
  if (duration.minutes !== 0) {
805
- return timeSinceInMinutes(from, to, timeZone, locale);
814
+ return getRelativeTimeFormat(locale).format(duration.minutes, "minutes");
806
815
  }
807
816
  if (duration.seconds !== 0) {
808
- return timeSinceInSeconds(from, to, timeZone, locale);
817
+ return getRelativeTimeFormat(locale).format(duration.seconds, "seconds");
809
818
  }
810
- throw new Error("Not a valid duration", { cause: duration });
819
+ // eslint-disable-next-line no-console
820
+ console.warn(new Error("Not a valid duration", { cause: duration }));
821
+ return "";
811
822
  };
812
823
  /**
813
824
  * Calculates the time duration between two TemporalDate objects and returns the duration in seconds as a formatted string.
package/index.esm.js CHANGED
@@ -778,32 +778,43 @@ const isEqualUtil = (from, to) => {
778
778
  * Output: "1 year ago"
779
779
  */
780
780
  const timeSinceAuto = (from, to, timeZone, locale) => {
781
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
782
- largestUnit: "years",
783
- smallestUnit: "seconds",
784
- }));
781
+ let since;
782
+ try {
783
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
784
+ largestUnit: "years",
785
+ smallestUnit: "seconds",
786
+ });
787
+ }
788
+ catch (error) {
789
+ // eslint-disable-next-line no-console
790
+ console.warn(new Error("Not a valid duration", { cause: error }));
791
+ return "";
792
+ }
793
+ const duration = Temporal.Duration.from(since);
785
794
  if (duration.blank) {
786
795
  return getRelativeTimeFormat(locale).format(-1, "seconds");
787
796
  }
788
797
  if (duration.years !== 0) {
789
- return timeSinceInYears(from, to, timeZone, locale);
798
+ return getRelativeTimeFormat(locale).format(duration.years, "years");
790
799
  }
791
800
  if (duration.months !== 0) {
792
- return timeSinceInMonths(from, to, timeZone, locale);
801
+ return getRelativeTimeFormat(locale).format(duration.months, "months");
793
802
  }
794
803
  if (duration.days !== 0) {
795
- return timeSinceInDays(from, to, timeZone, locale);
804
+ return getRelativeTimeFormat(locale).format(duration.days, "days");
796
805
  }
797
806
  if (duration.hours !== 0) {
798
- return timeSinceInHours(from, to, timeZone, locale);
807
+ return getRelativeTimeFormat(locale).format(duration.hours, "hours");
799
808
  }
800
809
  if (duration.minutes !== 0) {
801
- return timeSinceInMinutes(from, to, timeZone, locale);
810
+ return getRelativeTimeFormat(locale).format(duration.minutes, "minutes");
802
811
  }
803
812
  if (duration.seconds !== 0) {
804
- return timeSinceInSeconds(from, to, timeZone, locale);
813
+ return getRelativeTimeFormat(locale).format(duration.seconds, "seconds");
805
814
  }
806
- throw new Error("Not a valid duration", { cause: duration });
815
+ // eslint-disable-next-line no-console
816
+ console.warn(new Error("Not a valid duration", { cause: duration }));
817
+ return "";
807
818
  };
808
819
  /**
809
820
  * Calculates the time duration between two TemporalDate objects and returns the duration in seconds as a formatted string.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/date-and-time-utils",
3
- "version": "0.0.30",
3
+ "version": "0.0.33",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {