@trackunit/date-and-time-utils 0.0.28 → 0.0.30
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 +77 -7
- package/index.esm.js +77 -7
- package/package.json +2 -2
package/index.cjs.js
CHANGED
|
@@ -984,24 +984,28 @@ const toDuration = (number, unit, roundAt) => {
|
|
|
984
984
|
throw new Error("Not a valid unit", { cause: unit });
|
|
985
985
|
}
|
|
986
986
|
};
|
|
987
|
+
/**
|
|
988
|
+
* Build a duration string
|
|
989
|
+
*
|
|
990
|
+
* @param number - number to convert
|
|
991
|
+
* @param unit - "days | "hours" | minutes
|
|
992
|
+
*/
|
|
987
993
|
const durationBuilder = (number, unit) => {
|
|
988
|
-
const
|
|
989
|
-
const
|
|
990
|
-
const minutes = unit === "minutes" ? Math.floor(number) : Math.floor((number * 24 * 60) % 60);
|
|
991
|
-
const seconds = Math.floor((number * 24 * 60 * 60) % 60);
|
|
994
|
+
const isInt = Number.isSafeInteger(number);
|
|
995
|
+
const { days, hours, minutes, seconds } = calculateTime(number, unit);
|
|
992
996
|
switch (unit) {
|
|
993
997
|
case "days":
|
|
994
|
-
if (
|
|
998
|
+
if (isInt) {
|
|
995
999
|
return `P${number}D`;
|
|
996
1000
|
}
|
|
997
1001
|
return `P${days}DT${hours}H${minutes}M${seconds}S`;
|
|
998
1002
|
case "hours":
|
|
999
|
-
if (
|
|
1003
|
+
if (isInt) {
|
|
1000
1004
|
return `PT${number}H`;
|
|
1001
1005
|
}
|
|
1002
1006
|
return `PT${hours}H${minutes}M${seconds}S`;
|
|
1003
1007
|
case "minutes":
|
|
1004
|
-
if (
|
|
1008
|
+
if (isInt) {
|
|
1005
1009
|
return `PT${number}M`;
|
|
1006
1010
|
}
|
|
1007
1011
|
return `PT${minutes}M${seconds}S`;
|
|
@@ -1009,6 +1013,72 @@ const durationBuilder = (number, unit) => {
|
|
|
1009
1013
|
return `PT0S`;
|
|
1010
1014
|
}
|
|
1011
1015
|
};
|
|
1016
|
+
/**
|
|
1017
|
+
* Calculate time in days, hours, minutes, and seconds
|
|
1018
|
+
*
|
|
1019
|
+
* @param {number} number - number to convert
|
|
1020
|
+
* @param {TemporalTimeTypes} unit - "days | "hours" | minutes
|
|
1021
|
+
*/
|
|
1022
|
+
const calculateTime = (number, unit) => {
|
|
1023
|
+
switch (unit) {
|
|
1024
|
+
case "days":
|
|
1025
|
+
return convertToDays(number);
|
|
1026
|
+
case "hours":
|
|
1027
|
+
return Object.assign({ days: 0 }, convertToHours(number));
|
|
1028
|
+
case "minutes":
|
|
1029
|
+
return Object.assign({ days: 0, hours: 0 }, convertToMinutes(number));
|
|
1030
|
+
default:
|
|
1031
|
+
return { days: 0, hours: 0, minutes: 0, seconds: 0 };
|
|
1032
|
+
}
|
|
1033
|
+
};
|
|
1034
|
+
/**
|
|
1035
|
+
* Convert a number to days, hours, minutes, and seconds
|
|
1036
|
+
*
|
|
1037
|
+
* @param number - number to convert
|
|
1038
|
+
*/
|
|
1039
|
+
const convertToDays = (number) => {
|
|
1040
|
+
const days = Math.floor(number);
|
|
1041
|
+
const remainingHours = (number - days) * 24;
|
|
1042
|
+
const hours = Math.floor(remainingHours);
|
|
1043
|
+
const remainingMinutes = (remainingHours - hours) * 60;
|
|
1044
|
+
const minutes = Math.floor(remainingMinutes);
|
|
1045
|
+
const seconds = Math.round((remainingMinutes - minutes) * 60);
|
|
1046
|
+
return {
|
|
1047
|
+
days,
|
|
1048
|
+
hours,
|
|
1049
|
+
minutes,
|
|
1050
|
+
seconds,
|
|
1051
|
+
};
|
|
1052
|
+
};
|
|
1053
|
+
/**
|
|
1054
|
+
* Convert a number to hours, minutes, and seconds
|
|
1055
|
+
*
|
|
1056
|
+
* @param number - number to convert
|
|
1057
|
+
*/
|
|
1058
|
+
const convertToHours = (number) => {
|
|
1059
|
+
const hours = Math.floor(number);
|
|
1060
|
+
const decimalMinutes = (number - hours) * 60;
|
|
1061
|
+
const minutes = Math.floor(decimalMinutes);
|
|
1062
|
+
const seconds = Math.round((decimalMinutes - minutes) * 60);
|
|
1063
|
+
return {
|
|
1064
|
+
hours,
|
|
1065
|
+
minutes,
|
|
1066
|
+
seconds,
|
|
1067
|
+
};
|
|
1068
|
+
};
|
|
1069
|
+
/**
|
|
1070
|
+
* Convert a number to minutes and seconds
|
|
1071
|
+
*
|
|
1072
|
+
* @param number - number to convert
|
|
1073
|
+
*/
|
|
1074
|
+
const convertToMinutes = (number) => {
|
|
1075
|
+
const minutes = number < 0 ? 0 : Math.floor(number);
|
|
1076
|
+
const seconds = Math.round((number - minutes) * 60);
|
|
1077
|
+
return {
|
|
1078
|
+
minutes,
|
|
1079
|
+
seconds,
|
|
1080
|
+
};
|
|
1081
|
+
};
|
|
1012
1082
|
/**
|
|
1013
1083
|
* Converts a JavaScript Date object to a Temporal Instant.
|
|
1014
1084
|
*
|
package/index.esm.js
CHANGED
|
@@ -980,24 +980,28 @@ const toDuration = (number, unit, roundAt) => {
|
|
|
980
980
|
throw new Error("Not a valid unit", { cause: unit });
|
|
981
981
|
}
|
|
982
982
|
};
|
|
983
|
+
/**
|
|
984
|
+
* Build a duration string
|
|
985
|
+
*
|
|
986
|
+
* @param number - number to convert
|
|
987
|
+
* @param unit - "days | "hours" | minutes
|
|
988
|
+
*/
|
|
983
989
|
const durationBuilder = (number, unit) => {
|
|
984
|
-
const
|
|
985
|
-
const
|
|
986
|
-
const minutes = unit === "minutes" ? Math.floor(number) : Math.floor((number * 24 * 60) % 60);
|
|
987
|
-
const seconds = Math.floor((number * 24 * 60 * 60) % 60);
|
|
990
|
+
const isInt = Number.isSafeInteger(number);
|
|
991
|
+
const { days, hours, minutes, seconds } = calculateTime(number, unit);
|
|
988
992
|
switch (unit) {
|
|
989
993
|
case "days":
|
|
990
|
-
if (
|
|
994
|
+
if (isInt) {
|
|
991
995
|
return `P${number}D`;
|
|
992
996
|
}
|
|
993
997
|
return `P${days}DT${hours}H${minutes}M${seconds}S`;
|
|
994
998
|
case "hours":
|
|
995
|
-
if (
|
|
999
|
+
if (isInt) {
|
|
996
1000
|
return `PT${number}H`;
|
|
997
1001
|
}
|
|
998
1002
|
return `PT${hours}H${minutes}M${seconds}S`;
|
|
999
1003
|
case "minutes":
|
|
1000
|
-
if (
|
|
1004
|
+
if (isInt) {
|
|
1001
1005
|
return `PT${number}M`;
|
|
1002
1006
|
}
|
|
1003
1007
|
return `PT${minutes}M${seconds}S`;
|
|
@@ -1005,6 +1009,72 @@ const durationBuilder = (number, unit) => {
|
|
|
1005
1009
|
return `PT0S`;
|
|
1006
1010
|
}
|
|
1007
1011
|
};
|
|
1012
|
+
/**
|
|
1013
|
+
* Calculate time in days, hours, minutes, and seconds
|
|
1014
|
+
*
|
|
1015
|
+
* @param {number} number - number to convert
|
|
1016
|
+
* @param {TemporalTimeTypes} unit - "days | "hours" | minutes
|
|
1017
|
+
*/
|
|
1018
|
+
const calculateTime = (number, unit) => {
|
|
1019
|
+
switch (unit) {
|
|
1020
|
+
case "days":
|
|
1021
|
+
return convertToDays(number);
|
|
1022
|
+
case "hours":
|
|
1023
|
+
return Object.assign({ days: 0 }, convertToHours(number));
|
|
1024
|
+
case "minutes":
|
|
1025
|
+
return Object.assign({ days: 0, hours: 0 }, convertToMinutes(number));
|
|
1026
|
+
default:
|
|
1027
|
+
return { days: 0, hours: 0, minutes: 0, seconds: 0 };
|
|
1028
|
+
}
|
|
1029
|
+
};
|
|
1030
|
+
/**
|
|
1031
|
+
* Convert a number to days, hours, minutes, and seconds
|
|
1032
|
+
*
|
|
1033
|
+
* @param number - number to convert
|
|
1034
|
+
*/
|
|
1035
|
+
const convertToDays = (number) => {
|
|
1036
|
+
const days = Math.floor(number);
|
|
1037
|
+
const remainingHours = (number - days) * 24;
|
|
1038
|
+
const hours = Math.floor(remainingHours);
|
|
1039
|
+
const remainingMinutes = (remainingHours - hours) * 60;
|
|
1040
|
+
const minutes = Math.floor(remainingMinutes);
|
|
1041
|
+
const seconds = Math.round((remainingMinutes - minutes) * 60);
|
|
1042
|
+
return {
|
|
1043
|
+
days,
|
|
1044
|
+
hours,
|
|
1045
|
+
minutes,
|
|
1046
|
+
seconds,
|
|
1047
|
+
};
|
|
1048
|
+
};
|
|
1049
|
+
/**
|
|
1050
|
+
* Convert a number to hours, minutes, and seconds
|
|
1051
|
+
*
|
|
1052
|
+
* @param number - number to convert
|
|
1053
|
+
*/
|
|
1054
|
+
const convertToHours = (number) => {
|
|
1055
|
+
const hours = Math.floor(number);
|
|
1056
|
+
const decimalMinutes = (number - hours) * 60;
|
|
1057
|
+
const minutes = Math.floor(decimalMinutes);
|
|
1058
|
+
const seconds = Math.round((decimalMinutes - minutes) * 60);
|
|
1059
|
+
return {
|
|
1060
|
+
hours,
|
|
1061
|
+
minutes,
|
|
1062
|
+
seconds,
|
|
1063
|
+
};
|
|
1064
|
+
};
|
|
1065
|
+
/**
|
|
1066
|
+
* Convert a number to minutes and seconds
|
|
1067
|
+
*
|
|
1068
|
+
* @param number - number to convert
|
|
1069
|
+
*/
|
|
1070
|
+
const convertToMinutes = (number) => {
|
|
1071
|
+
const minutes = number < 0 ? 0 : Math.floor(number);
|
|
1072
|
+
const seconds = Math.round((number - minutes) * 60);
|
|
1073
|
+
return {
|
|
1074
|
+
minutes,
|
|
1075
|
+
seconds,
|
|
1076
|
+
};
|
|
1077
|
+
};
|
|
1008
1078
|
/**
|
|
1009
1079
|
* Converts a JavaScript Date object to a Temporal Instant.
|
|
1010
1080
|
*
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/date-and-time-utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.30",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=20.x"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@formatjs/intl-enumerator": "^1.4.1",
|