You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dateUtil.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // $FlowFixMe
  57. if (moment.duration(duration).format('h') !== '0') {
  58. // $FlowFixMe
  59. return moment.duration(duration).format('h:mm:ss');
  60. }
  61. // $FlowFixMe
  62. return moment.duration(duration).format('mm:ss', { trim: false });
  63. }
  64. /**
  65. * A lenient locale matcher to match language and dialect if possible.
  66. *
  67. * @private
  68. * @returns {string}
  69. */
  70. function _getSupportedLocale() {
  71. const i18nLocale = i18next.language;
  72. let supportedLocale;
  73. if (i18nLocale) {
  74. const localeRegexp = new RegExp('^([a-z]{2,2})(-)*([a-z]{2,2})*$');
  75. const localeResult = localeRegexp.exec(i18nLocale.toLowerCase());
  76. if (localeResult) {
  77. const currentLocaleRegexp
  78. = new RegExp(
  79. `^${localeResult[1]}(-)*${`(${localeResult[3]})*` || ''}`);
  80. supportedLocale
  81. // FIXME The flow-type definition of moment is v2.3 while our
  82. // package.json states v2.19 so maybe locales on moment was
  83. // introduced in between?
  84. //
  85. // $FlowFixMe
  86. = moment.locales().find(lang => currentLocaleRegexp.exec(lang));
  87. }
  88. }
  89. return supportedLocale || 'en';
  90. }