@trackunit/date-and-time-utils 0.0.30 → 0.0.31
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 +12 -8
- package/index.esm.js +12 -8
- package/package.json +1 -1
package/index.cjs.js
CHANGED
|
@@ -984,6 +984,7 @@ const toDuration = (number, unit, roundAt) => {
|
|
|
984
984
|
throw new Error("Not a valid unit", { cause: unit });
|
|
985
985
|
}
|
|
986
986
|
};
|
|
987
|
+
const defaultDuration = { days: 0, hours: 0, minutes: 0, seconds: 0 };
|
|
987
988
|
/**
|
|
988
989
|
* Build a duration string
|
|
989
990
|
*
|
|
@@ -991,24 +992,27 @@ const toDuration = (number, unit, roundAt) => {
|
|
|
991
992
|
* @param unit - "days | "hours" | minutes
|
|
992
993
|
*/
|
|
993
994
|
const durationBuilder = (number, unit) => {
|
|
994
|
-
const
|
|
995
|
-
const
|
|
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;
|
|
996
1000
|
switch (unit) {
|
|
997
1001
|
case "days":
|
|
998
1002
|
if (isInt) {
|
|
999
|
-
return
|
|
1003
|
+
return `${sign}P${unsignedNumber}D`;
|
|
1000
1004
|
}
|
|
1001
|
-
return
|
|
1005
|
+
return `${sign}P${days}DT${hours}H${minutes}M${seconds}S`;
|
|
1002
1006
|
case "hours":
|
|
1003
1007
|
if (isInt) {
|
|
1004
|
-
return
|
|
1008
|
+
return `${sign}PT${unsignedNumber}H`;
|
|
1005
1009
|
}
|
|
1006
|
-
return
|
|
1010
|
+
return `${sign}PT${hours}H${minutes}M${seconds}S`;
|
|
1007
1011
|
case "minutes":
|
|
1008
1012
|
if (isInt) {
|
|
1009
|
-
return
|
|
1013
|
+
return `${sign}PT${unsignedNumber}M`;
|
|
1010
1014
|
}
|
|
1011
|
-
return
|
|
1015
|
+
return `${sign}PT${minutes}M${seconds}S`;
|
|
1012
1016
|
default:
|
|
1013
1017
|
return `PT0S`;
|
|
1014
1018
|
}
|
package/index.esm.js
CHANGED
|
@@ -980,6 +980,7 @@ const toDuration = (number, unit, roundAt) => {
|
|
|
980
980
|
throw new Error("Not a valid unit", { cause: unit });
|
|
981
981
|
}
|
|
982
982
|
};
|
|
983
|
+
const defaultDuration = { days: 0, hours: 0, minutes: 0, seconds: 0 };
|
|
983
984
|
/**
|
|
984
985
|
* Build a duration string
|
|
985
986
|
*
|
|
@@ -987,24 +988,27 @@ const toDuration = (number, unit, roundAt) => {
|
|
|
987
988
|
* @param unit - "days | "hours" | minutes
|
|
988
989
|
*/
|
|
989
990
|
const durationBuilder = (number, unit) => {
|
|
990
|
-
const
|
|
991
|
-
const
|
|
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;
|
|
992
996
|
switch (unit) {
|
|
993
997
|
case "days":
|
|
994
998
|
if (isInt) {
|
|
995
|
-
return
|
|
999
|
+
return `${sign}P${unsignedNumber}D`;
|
|
996
1000
|
}
|
|
997
|
-
return
|
|
1001
|
+
return `${sign}P${days}DT${hours}H${minutes}M${seconds}S`;
|
|
998
1002
|
case "hours":
|
|
999
1003
|
if (isInt) {
|
|
1000
|
-
return
|
|
1004
|
+
return `${sign}PT${unsignedNumber}H`;
|
|
1001
1005
|
}
|
|
1002
|
-
return
|
|
1006
|
+
return `${sign}PT${hours}H${minutes}M${seconds}S`;
|
|
1003
1007
|
case "minutes":
|
|
1004
1008
|
if (isInt) {
|
|
1005
|
-
return
|
|
1009
|
+
return `${sign}PT${unsignedNumber}M`;
|
|
1006
1010
|
}
|
|
1007
|
-
return
|
|
1011
|
+
return `${sign}PT${minutes}M${seconds}S`;
|
|
1008
1012
|
default:
|
|
1009
1013
|
return `PT0S`;
|
|
1010
1014
|
}
|