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

dateUtil.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // @flow
  2. import moment from 'moment';
  3. import i18next from './i18next';
  4. // allows for moment durations to be formatted
  5. import 'moment-duration-format';
  6. // MomentJS uses static language bundle loading, so in order to support dynamic
  7. // language selection in the app we need to load all bundles that we support in
  8. // the app.
  9. require('moment/locale/bg');
  10. require('moment/locale/de');
  11. require('moment/locale/eo');
  12. require('moment/locale/es');
  13. require('moment/locale/fr');
  14. require('moment/locale/hy-am');
  15. require('moment/locale/it');
  16. require('moment/locale/nb');
  17. // OC is not available. Please submit OC translation to the MomentJS project.
  18. require('moment/locale/pl');
  19. require('moment/locale/pt');
  20. require('moment/locale/pt-br');
  21. require('moment/locale/ru');
  22. require('moment/locale/sk');
  23. require('moment/locale/sl');
  24. require('moment/locale/sv');
  25. require('moment/locale/tr');
  26. require('moment/locale/zh-cn');
  27. /**
  28. * Returns a localized date formatter initialized with a specific {@code Date}
  29. * or timestamp ({@code number}).
  30. *
  31. * @private
  32. * @param {Date | number} dateOrTimeStamp - The date or unix timestamp (ms)
  33. * to format.
  34. * @returns {Object}
  35. */
  36. export function getLocalizedDateFormatter(dateOrTimeStamp: Date | number) {
  37. return moment(dateOrTimeStamp).locale(_getSupportedLocale());
  38. }
  39. /**
  40. * Returns a localized duration formatter initialized with a
  41. * specific duration ({@code number}).
  42. *
  43. * @private
  44. * @param {number} duration - The duration (ms)
  45. * to format.
  46. * @returns {Object}
  47. */
  48. export function getLocalizedDurationFormatter(duration: number) {
  49. // FIXME The flow-type definition of moment is v2.3 while our package.json
  50. // states v2.19 so maybe locale on moment's duration was introduced in
  51. // between?
  52. //
  53. // If the conference is under an hour long we want to display it without
  54. // showing the hour and we want to include the hour if the conference is
  55. // more than an hour long
  56. if (moment.duration(duration).format('h') !== '0') {
  57. return moment.duration(duration).format('h:mm:ss');
  58. }
  59. return moment.duration(duration).format('mm:ss', { trim: false });
  60. }
  61. /**
  62. * A lenient locale matcher to match language and dialect if possible.
  63. *
  64. * @private
  65. * @returns {string}
  66. */
  67. function _getSupportedLocale() {
  68. const i18nLocale = i18next.language;
  69. let supportedLocale;
  70. if (i18nLocale) {
  71. const localeRegexp = new RegExp('^([a-z]{2,2})(-)*([a-z]{2,2})*$');
  72. const localeResult = localeRegexp.exec(i18nLocale.toLowerCase());
  73. if (localeResult) {
  74. const currentLocaleRegexp
  75. = new RegExp(
  76. `^${localeResult[1]}(-)*${`(${localeResult[3]})*` || ''}`);
  77. supportedLocale
  78. // FIXME The flow-type definition of moment is v2.3 while our
  79. // package.json states v2.19 so maybe locales on moment was
  80. // introduced in between?
  81. = moment.locales().find(lang => currentLocaleRegexp.exec(lang));
  82. }
  83. }
  84. return supportedLocale || 'en';
  85. }