utils.js 1.9KB

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