utils.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.isSameSecond = exports.parseFormat = exports.parseTimeData = void 0;
  6. function padZero(num, targetLength) {
  7. if (targetLength === void 0) {
  8. targetLength = 2;
  9. }
  10. var str = num + '';
  11. while (str.length < targetLength) {
  12. str = '0' + str;
  13. }
  14. return str;
  15. }
  16. var SECOND = 1000;
  17. var MINUTE = 60 * SECOND;
  18. var HOUR = 60 * MINUTE;
  19. var DAY = 24 * HOUR;
  20. function parseTimeData(time) {
  21. var days = Math.floor(time / DAY);
  22. var hours = Math.floor((time % DAY) / HOUR);
  23. var minutes = Math.floor((time % HOUR) / MINUTE);
  24. var seconds = Math.floor((time % MINUTE) / SECOND);
  25. var milliseconds = Math.floor(time % SECOND);
  26. return {
  27. days: days,
  28. hours: hours,
  29. minutes: minutes,
  30. seconds: seconds,
  31. milliseconds: milliseconds
  32. };
  33. }
  34. exports.parseTimeData = parseTimeData;
  35. function parseFormat(format, timeData) {
  36. var days = timeData.days;
  37. var hours = timeData.hours;
  38. var minutes = timeData.minutes;
  39. var seconds = timeData.seconds;
  40. var milliseconds = timeData.milliseconds;
  41. if (format.indexOf('DD') === -1) {
  42. hours += days * 24;
  43. } else {
  44. format = format.replace('DD', padZero(days));
  45. }
  46. if (format.indexOf('HH') === -1) {
  47. minutes += hours * 60;
  48. } else {
  49. format = format.replace('HH', padZero(hours));
  50. }
  51. if (format.indexOf('mm') === -1) {
  52. seconds += minutes * 60;
  53. } else {
  54. format = format.replace('mm', padZero(minutes));
  55. }
  56. if (format.indexOf('ss') === -1) {
  57. milliseconds += seconds * 1000;
  58. } else {
  59. format = format.replace('ss', padZero(seconds));
  60. }
  61. return format.replace('SSS', padZero(milliseconds, 3));
  62. }
  63. exports.parseFormat = parseFormat;
  64. function isSameSecond(time1, time2) {
  65. return Math.floor(time1 / 1000) === Math.floor(time2 / 1000);
  66. }
  67. exports.isSameSecond = isSameSecond;