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