@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.
- package/index.cjs.js +30 -23
- package/index.esm.js +30 -23
- 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.
|
|
@@ -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
|
|
996
|
-
const
|
|
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
|
|
1010
|
+
return `P${number}D`;
|
|
1004
1011
|
}
|
|
1005
|
-
return
|
|
1012
|
+
return `P${days}DT${hours}H${minutes}M${seconds}S`;
|
|
1006
1013
|
case "hours":
|
|
1007
1014
|
if (isInt) {
|
|
1008
|
-
return
|
|
1015
|
+
return `PT${number}H`;
|
|
1009
1016
|
}
|
|
1010
|
-
return
|
|
1017
|
+
return `PT${hours}H${minutes}M${seconds}S`;
|
|
1011
1018
|
case "minutes":
|
|
1012
1019
|
if (isInt) {
|
|
1013
|
-
return
|
|
1020
|
+
return `PT${number}M`;
|
|
1014
1021
|
}
|
|
1015
|
-
return
|
|
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
|
-
|
|
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.
|
|
@@ -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
|
|
992
|
-
const
|
|
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
|
|
1006
|
+
return `P${number}D`;
|
|
1000
1007
|
}
|
|
1001
|
-
return
|
|
1008
|
+
return `P${days}DT${hours}H${minutes}M${seconds}S`;
|
|
1002
1009
|
case "hours":
|
|
1003
1010
|
if (isInt) {
|
|
1004
|
-
return
|
|
1011
|
+
return `PT${number}H`;
|
|
1005
1012
|
}
|
|
1006
|
-
return
|
|
1013
|
+
return `PT${hours}H${minutes}M${seconds}S`;
|
|
1007
1014
|
case "minutes":
|
|
1008
1015
|
if (isInt) {
|
|
1009
|
-
return
|
|
1016
|
+
return `PT${number}M`;
|
|
1010
1017
|
}
|
|
1011
|
-
return
|
|
1018
|
+
return `PT${minutes}M${seconds}S`;
|
|
1012
1019
|
default:
|
|
1013
1020
|
return `PT0S`;
|
|
1014
1021
|
}
|