utils.js 1.8 KB

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