utils.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.parseTimeData = parseTimeData;
  6. exports.parseFormat = parseFormat;
  7. exports.isSameSecond = isSameSecond;
  8. function padZero(num) {
  9. var targetLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
  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. function parseFormat(format, timeData) {
  35. var days = timeData.days;
  36. var hours = timeData.hours,
  37. minutes = timeData.minutes,
  38. seconds = timeData.seconds,
  39. milliseconds = timeData.milliseconds;
  40. if (format.indexOf("DD") === -1) {
  41. hours += days * 24;
  42. } else {
  43. format = format.replace("DD", padZero(days));
  44. }
  45. if (format.indexOf("HH") === -1) {
  46. minutes += hours * 60;
  47. } else {
  48. format = format.replace("HH", padZero(hours));
  49. }
  50. if (format.indexOf("mm") === -1) {
  51. seconds += minutes * 60;
  52. } else {
  53. format = format.replace("mm", padZero(minutes));
  54. }
  55. if (format.indexOf("ss") === -1) {
  56. milliseconds += seconds * 1000;
  57. } else {
  58. format = format.replace("ss", padZero(seconds));
  59. }
  60. return format.replace("SSS", padZero(milliseconds, 3));
  61. }
  62. function isSameSecond(time1, time2) {
  63. return Math.floor(time1 / 1000) === Math.floor(time2 / 1000);
  64. }