Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

timeFunctions.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // @flow
  2. /**
  3. * Counts how many whole hours are included in the given time total.
  4. *
  5. * @param {number} milliseconds - The millisecond total to get hours from.
  6. * @private
  7. * @returns {number}
  8. */
  9. function getHoursCount(milliseconds) {
  10. return Math.floor(milliseconds / (60 * 60 * 1000));
  11. }
  12. /**
  13. * Counts how many whole minutes are included in the given time total.
  14. *
  15. * @param {number} milliseconds - The millisecond total to get minutes from.
  16. * @private
  17. * @returns {number}
  18. */
  19. function getMinutesCount(milliseconds) {
  20. return Math.floor(milliseconds / (60 * 1000) % 60);
  21. }
  22. /**
  23. * Counts how many whole seconds are included in the given time total.
  24. *
  25. * @param {number} milliseconds - The millisecond total to get seconds from.
  26. * @private
  27. * @returns {number}
  28. */
  29. function getSecondsCount(milliseconds) {
  30. return Math.floor(milliseconds / 1000 % 60);
  31. }
  32. /**
  33. * Creates human readable localized time string.
  34. *
  35. * @param {number} time - Value in milliseconds.
  36. * @param {Function} t - Translate function.
  37. * @returns {string}
  38. */
  39. export function createLocalizedTime(time: number, t: Function) {
  40. const hours = getHoursCount(time);
  41. const minutes = getMinutesCount(time);
  42. const seconds = getSecondsCount(time);
  43. const timeElapsed = [];
  44. if (hours) {
  45. const hourPassed
  46. = createTimeDisplay(hours, 'speakerStats.hours', t);
  47. timeElapsed.push(hourPassed);
  48. }
  49. if (hours || minutes) {
  50. const minutesPassed
  51. = createTimeDisplay(
  52. minutes,
  53. 'speakerStats.minutes',
  54. t);
  55. timeElapsed.push(minutesPassed);
  56. }
  57. const secondsPassed
  58. = createTimeDisplay(
  59. seconds,
  60. 'speakerStats.seconds',
  61. t);
  62. timeElapsed.push(secondsPassed);
  63. return timeElapsed;
  64. }
  65. /**
  66. * Returns a string to display the passed in count and a count noun.
  67. *
  68. * @private
  69. * @param {number} count - The number used for display and to check for
  70. * count noun plurality.
  71. * @param {string} countNounKey - Translation key for the time's count noun.
  72. * @param {Function} t - What is being counted. Used as the element's
  73. * key for react to iterate upon.
  74. * @returns {string}
  75. */
  76. function createTimeDisplay(count, countNounKey, t) {
  77. return t(countNounKey, { count });
  78. }