您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

timeFunctions.ts 2.3KB

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