utils.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { padZero } from '../utils/format/string';
  2. var SECOND = 1000;
  3. var MINUTE = 60 * SECOND;
  4. var HOUR = 60 * MINUTE;
  5. var DAY = 24 * HOUR;
  6. export function parseTimeData(time) {
  7. var days = Math.floor(time / DAY);
  8. var hours = Math.floor(time % DAY / HOUR);
  9. var minutes = Math.floor(time % HOUR / MINUTE);
  10. var seconds = Math.floor(time % MINUTE / SECOND);
  11. var milliseconds = Math.floor(time % SECOND);
  12. return {
  13. days: days,
  14. hours: hours,
  15. minutes: minutes,
  16. seconds: seconds,
  17. milliseconds: milliseconds
  18. };
  19. }
  20. export function parseFormat(format, timeData) {
  21. var days = timeData.days;
  22. var hours = timeData.hours,
  23. minutes = timeData.minutes,
  24. seconds = timeData.seconds,
  25. milliseconds = timeData.milliseconds;
  26. if (format.indexOf('DD') === -1) {
  27. hours += days * 24;
  28. } else {
  29. format = format.replace('DD', padZero(days));
  30. }
  31. if (format.indexOf('HH') === -1) {
  32. minutes += hours * 60;
  33. } else {
  34. format = format.replace('HH', padZero(hours));
  35. }
  36. if (format.indexOf('mm') === -1) {
  37. seconds += minutes * 60;
  38. } else {
  39. format = format.replace('mm', padZero(minutes));
  40. }
  41. if (format.indexOf('ss') === -1) {
  42. milliseconds += seconds * 1000;
  43. } else {
  44. format = format.replace('ss', padZero(seconds));
  45. }
  46. if (format.indexOf('S') !== -1) {
  47. var ms = padZero(milliseconds, 3);
  48. if (format.indexOf('SSS') !== -1) {
  49. format = format.replace('SSS', ms);
  50. } else if (format.indexOf('SS') !== -1) {
  51. format = format.replace('SS', ms.slice(0, 2));
  52. } else {
  53. format = format.replace('S', ms.charAt(0));
  54. }
  55. }
  56. return format;
  57. }
  58. export function isSameSecond(time1, time2) {
  59. return Math.floor(time1 / 1000) === Math.floor(time2 / 1000);
  60. }