zzz-pc-view 0.0.12 → 0.0.14

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zzz-pc-view",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "main": "src/index.umd.js",
5
5
  "module": "src/index.es.js",
6
6
  "types": "src/index.d.ts",
package/src/index.es.js CHANGED
@@ -691,6 +691,34 @@ const setDateByFormat = (date, dateStr, format, ...params) => {
691
691
  }
692
692
  return date.getTime();
693
693
  };
694
+ const timeConfigs = [
695
+ {
696
+ unit: "天",
697
+ step: 864e5,
698
+ pad: NaN,
699
+ max: Infinity
700
+ },
701
+ {
702
+ unit: "小时",
703
+ step: 36e5,
704
+ pad: 2,
705
+ max: 23
706
+ },
707
+ {
708
+ unit: "分",
709
+ step: 6e4,
710
+ pad: 2,
711
+ max: 59
712
+ },
713
+ {
714
+ unit: "秒",
715
+ step: 1e3,
716
+ pad: 2,
717
+ max: 59
718
+ }
719
+ ];
720
+ const timeConfigsLen = timeConfigs.length;
721
+ const lastTimeConfigsIndex = timeConfigsLen - 1;
694
722
  class ZDate extends Date {
695
723
  /**
696
724
  * 设置日期的季度。
@@ -922,6 +950,92 @@ class ZDate extends Date {
922
950
  setByFormat(...args) {
923
951
  return setDateByFormat(this, ...args);
924
952
  }
953
+ /**
954
+ * 获取两个时间之间的持续时间格式字符串。
955
+ *
956
+ * @param beginTime - 开始时间,可以是字符串、数字、Date 对象或 ZDate 对象。
957
+ * @param endTime - 结束时间,可以是字符串、数字、Date 对象或 ZDate 对象。
958
+ * @param lastEndIndex - 最后一个时间单位的索引,默认为 timeConfigs 数组的最后一个索引。
959
+ * @returns 持续时间的格式字符串。
960
+ *
961
+ * @example
962
+ const beginTime = new ZDate('2023/01/01 00:00:00')
963
+ const endTime = new ZDate('2023/01/01 00:00:05')
964
+ const durationStr = ZUtils.ZDate.getDurationFormatStr(beginTime, endTime)
965
+ console.log(durationStr)
966
+ *
967
+ * @example
968
+ const beginTime = new ZDate('2023/01/01 00:00:00')
969
+ const endTime = new ZDate('2023/01/01 00:05:00')
970
+ const durationStr = ZUtils.ZDate.getDurationFormatStr(beginTime, endTime, 2)
971
+ console.log(durationStr)
972
+
973
+ * @example
974
+ const beginTime = new ZDate('2023/01/01 00:00:00')
975
+ const endTime = new ZDate('2023/01/01 00:05:00')
976
+ const durationStr = ZUtils.ZDate.getDurationFormatStr(beginTime, endTime, -1)
977
+ console.log(durationStr)
978
+ */
979
+ static getDurationFormatStr(beginTime, endTime, lastEndIndex = lastTimeConfigsIndex) {
980
+ const results = [];
981
+ lastEndIndex = Math.max(
982
+ 0,
983
+ Math.min(
984
+ lastEndIndex + (lastEndIndex >> -1 & 1) * lastTimeConfigsIndex,
985
+ lastTimeConfigsIndex
986
+ )
987
+ );
988
+ const endIndex = lastEndIndex + 1;
989
+ let duration = new Date(endTime).getTime() - new Date(beginTime).getTime();
990
+ if (duration === 0) {
991
+ results.push([0, timeConfigs[lastEndIndex]]);
992
+ } else {
993
+ for (let i = 0; i < endIndex; i++) {
994
+ const timeConfig = timeConfigs[i];
995
+ const { step } = timeConfig;
996
+ const result = duration / step;
997
+ results.push([result, timeConfig]);
998
+ duration = duration % step;
999
+ if (duration === 0) {
1000
+ break;
1001
+ }
1002
+ }
1003
+ }
1004
+ const lastResultsIndex = results.length - 1;
1005
+ const lastResult = results[lastResultsIndex];
1006
+ lastResult[0] = Math.ceil(lastResult[0]);
1007
+ if (lastResult[0] > lastResult[1].max) {
1008
+ lastResult[0] = 0;
1009
+ for (let i = lastResultsIndex - 1; i >= 0; i--) {
1010
+ const result = results[i];
1011
+ result[0] = Math.floor(result[0]) + 1;
1012
+ if (result[0] > result[1].max) {
1013
+ result[0] = 0;
1014
+ } else {
1015
+ break;
1016
+ }
1017
+ }
1018
+ }
1019
+ let firstNotZeroIndex = results.findIndex((result) => result[0] >= 1);
1020
+ if (firstNotZeroIndex === -1) {
1021
+ firstNotZeroIndex = lastResultsIndex;
1022
+ }
1023
+ let lastNotZeroIndex = lastResultsIndex;
1024
+ for (; lastNotZeroIndex > firstNotZeroIndex; lastNotZeroIndex--) {
1025
+ const result = results[lastNotZeroIndex];
1026
+ if (result[0] > 0) {
1027
+ break;
1028
+ }
1029
+ }
1030
+ let response2 = "";
1031
+ for (let i = firstNotZeroIndex; i <= lastNotZeroIndex; i++) {
1032
+ const result = results[i];
1033
+ const { unit, pad } = result[1];
1034
+ const value = Math.floor(result[0]);
1035
+ response2 += `${Number.isFinite(pad) ? value.toString().padStart(pad, "0") : value}${unit}`;
1036
+ }
1037
+ return response2;
1038
+ }
925
1039
  }
926
1040
  const completeByDate = (list2, dataTimeProp, dateRange, completeConfig, missDataFormatter = (data) => data) => {
927
1041
  const { interval } = completeConfig;