@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.
- package/index.cjs.js +22 -11
- package/index.esm.js +22 -11
- 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
|
-
|
|
786
|
-
|
|
787
|
-
|
|
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
|
|
802
|
+
return getRelativeTimeFormat(locale).format(duration.years, "years");
|
|
794
803
|
}
|
|
795
804
|
if (duration.months !== 0) {
|
|
796
|
-
return
|
|
805
|
+
return getRelativeTimeFormat(locale).format(duration.months, "months");
|
|
797
806
|
}
|
|
798
807
|
if (duration.days !== 0) {
|
|
799
|
-
return
|
|
808
|
+
return getRelativeTimeFormat(locale).format(duration.days, "days");
|
|
800
809
|
}
|
|
801
810
|
if (duration.hours !== 0) {
|
|
802
|
-
return
|
|
811
|
+
return getRelativeTimeFormat(locale).format(duration.hours, "hours");
|
|
803
812
|
}
|
|
804
813
|
if (duration.minutes !== 0) {
|
|
805
|
-
return
|
|
814
|
+
return getRelativeTimeFormat(locale).format(duration.minutes, "minutes");
|
|
806
815
|
}
|
|
807
816
|
if (duration.seconds !== 0) {
|
|
808
|
-
return
|
|
817
|
+
return getRelativeTimeFormat(locale).format(duration.seconds, "seconds");
|
|
809
818
|
}
|
|
810
|
-
|
|
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
|
-
|
|
782
|
-
|
|
783
|
-
|
|
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
|
|
798
|
+
return getRelativeTimeFormat(locale).format(duration.years, "years");
|
|
790
799
|
}
|
|
791
800
|
if (duration.months !== 0) {
|
|
792
|
-
return
|
|
801
|
+
return getRelativeTimeFormat(locale).format(duration.months, "months");
|
|
793
802
|
}
|
|
794
803
|
if (duration.days !== 0) {
|
|
795
|
-
return
|
|
804
|
+
return getRelativeTimeFormat(locale).format(duration.days, "days");
|
|
796
805
|
}
|
|
797
806
|
if (duration.hours !== 0) {
|
|
798
|
-
return
|
|
807
|
+
return getRelativeTimeFormat(locale).format(duration.hours, "hours");
|
|
799
808
|
}
|
|
800
809
|
if (duration.minutes !== 0) {
|
|
801
|
-
return
|
|
810
|
+
return getRelativeTimeFormat(locale).format(duration.minutes, "minutes");
|
|
802
811
|
}
|
|
803
812
|
if (duration.seconds !== 0) {
|
|
804
|
-
return
|
|
813
|
+
return getRelativeTimeFormat(locale).format(duration.seconds, "seconds");
|
|
805
814
|
}
|
|
806
|
-
|
|
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.
|