@trackunit/date-and-time-utils 0.0.31 → 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 +30 -23
  2. package/index.esm.js +30 -23
  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.
@@ -984,7 +995,6 @@ const toDuration = (number, unit, roundAt) => {
984
995
  throw new Error("Not a valid unit", { cause: unit });
985
996
  }
986
997
  };
987
- const defaultDuration = { days: 0, hours: 0, minutes: 0, seconds: 0 };
988
998
  /**
989
999
  * Build a duration string
990
1000
  *
@@ -992,27 +1002,24 @@ const defaultDuration = { days: 0, hours: 0, minutes: 0, seconds: 0 };
992
1002
  * @param unit - "days | "hours" | minutes
993
1003
  */
994
1004
  const durationBuilder = (number, unit) => {
995
- const isSigned = Math.sign(number) === -1;
996
- const sign = isSigned ? "-" : "";
997
- const unsignedNumber = Math.abs(number);
998
- const isInt = Number.isSafeInteger(unsignedNumber);
999
- const { days, hours, minutes, seconds } = !isInt ? calculateTime(unsignedNumber, unit) : defaultDuration;
1005
+ const isInt = Number.isSafeInteger(number);
1006
+ const { days, hours, minutes, seconds } = calculateTime(number, unit);
1000
1007
  switch (unit) {
1001
1008
  case "days":
1002
1009
  if (isInt) {
1003
- return `${sign}P${unsignedNumber}D`;
1010
+ return `P${number}D`;
1004
1011
  }
1005
- return `${sign}P${days}DT${hours}H${minutes}M${seconds}S`;
1012
+ return `P${days}DT${hours}H${minutes}M${seconds}S`;
1006
1013
  case "hours":
1007
1014
  if (isInt) {
1008
- return `${sign}PT${unsignedNumber}H`;
1015
+ return `PT${number}H`;
1009
1016
  }
1010
- return `${sign}PT${hours}H${minutes}M${seconds}S`;
1017
+ return `PT${hours}H${minutes}M${seconds}S`;
1011
1018
  case "minutes":
1012
1019
  if (isInt) {
1013
- return `${sign}PT${unsignedNumber}M`;
1020
+ return `PT${number}M`;
1014
1021
  }
1015
- return `${sign}PT${minutes}M${seconds}S`;
1022
+ return `PT${minutes}M${seconds}S`;
1016
1023
  default:
1017
1024
  return `PT0S`;
1018
1025
  }
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.
@@ -980,7 +991,6 @@ const toDuration = (number, unit, roundAt) => {
980
991
  throw new Error("Not a valid unit", { cause: unit });
981
992
  }
982
993
  };
983
- const defaultDuration = { days: 0, hours: 0, minutes: 0, seconds: 0 };
984
994
  /**
985
995
  * Build a duration string
986
996
  *
@@ -988,27 +998,24 @@ const defaultDuration = { days: 0, hours: 0, minutes: 0, seconds: 0 };
988
998
  * @param unit - "days | "hours" | minutes
989
999
  */
990
1000
  const durationBuilder = (number, unit) => {
991
- const isSigned = Math.sign(number) === -1;
992
- const sign = isSigned ? "-" : "";
993
- const unsignedNumber = Math.abs(number);
994
- const isInt = Number.isSafeInteger(unsignedNumber);
995
- const { days, hours, minutes, seconds } = !isInt ? calculateTime(unsignedNumber, unit) : defaultDuration;
1001
+ const isInt = Number.isSafeInteger(number);
1002
+ const { days, hours, minutes, seconds } = calculateTime(number, unit);
996
1003
  switch (unit) {
997
1004
  case "days":
998
1005
  if (isInt) {
999
- return `${sign}P${unsignedNumber}D`;
1006
+ return `P${number}D`;
1000
1007
  }
1001
- return `${sign}P${days}DT${hours}H${minutes}M${seconds}S`;
1008
+ return `P${days}DT${hours}H${minutes}M${seconds}S`;
1002
1009
  case "hours":
1003
1010
  if (isInt) {
1004
- return `${sign}PT${unsignedNumber}H`;
1011
+ return `PT${number}H`;
1005
1012
  }
1006
- return `${sign}PT${hours}H${minutes}M${seconds}S`;
1013
+ return `PT${hours}H${minutes}M${seconds}S`;
1007
1014
  case "minutes":
1008
1015
  if (isInt) {
1009
- return `${sign}PT${unsignedNumber}M`;
1016
+ return `PT${number}M`;
1010
1017
  }
1011
- return `${sign}PT${minutes}M${seconds}S`;
1018
+ return `PT${minutes}M${seconds}S`;
1012
1019
  default:
1013
1020
  return `PT0S`;
1014
1021
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/date-and-time-utils",
3
- "version": "0.0.31",
3
+ "version": "0.0.33",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {