index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var buildFormattingTokensRegExp = require('../../_lib/build_formatting_tokens_reg_exp/index.js')
  2. function buildFormatLocale () {
  3. var months3char = ['gen', 'feb', 'mar', 'abr', 'mai', 'jun', 'jul', 'ago', 'set', 'oct', 'nov', 'des']
  4. var monthsFull = ['gener', 'febrer', 'març', 'abril', 'maig', 'juny', 'juliol', 'agost', 'setembre', 'octobre', 'novembre', 'desembre']
  5. var weekdays2char = ['dg', 'dl', 'dt', 'dc', 'dj', 'dv', 'ds']
  6. var weekdays3char = ['dge', 'dls', 'dts', 'dcs', 'djs', 'dvs', 'dss']
  7. var weekdaysFull = ['diumenge', 'dilluns', 'dimarts', 'dimecres', 'dijous', 'divendres', 'dissabte']
  8. var meridiemUppercase = ['AM', 'PM']
  9. var meridiemLowercase = ['am', 'pm']
  10. var meridiemFull = ['a.m.', 'p.m.']
  11. var formatters = {
  12. // Month: Jan, Feb, ..., Dec
  13. 'MMM': function (date) {
  14. return months3char[date.getMonth()]
  15. },
  16. // Month: January, February, ..., December
  17. 'MMMM': function (date) {
  18. return monthsFull[date.getMonth()]
  19. },
  20. // Day of week: Su, Mo, ..., Sa
  21. 'dd': function (date) {
  22. return weekdays2char[date.getDay()]
  23. },
  24. // Day of week: Sun, Mon, ..., Sat
  25. 'ddd': function (date) {
  26. return weekdays3char[date.getDay()]
  27. },
  28. // Day of week: Sunday, Monday, ..., Saturday
  29. 'dddd': function (date) {
  30. return weekdaysFull[date.getDay()]
  31. },
  32. // AM, PM
  33. 'A': function (date) {
  34. return (date.getHours() / 12) >= 1 ? meridiemUppercase[1] : meridiemUppercase[0]
  35. },
  36. // am, pm
  37. 'a': function (date) {
  38. return (date.getHours() / 12) >= 1 ? meridiemLowercase[1] : meridiemLowercase[0]
  39. },
  40. // a.m., p.m.
  41. 'aa': function (date) {
  42. return (date.getHours() / 12) >= 1 ? meridiemFull[1] : meridiemFull[0]
  43. }
  44. }
  45. // Generate ordinal version of formatters: M -> Mo, D -> Do, etc.
  46. var ordinalFormatters = ['M', 'D', 'DDD', 'd', 'Q', 'W']
  47. ordinalFormatters.forEach(function (formatterToken) {
  48. formatters[formatterToken + 'o'] = function (date, formatters) {
  49. return ordinal(formatters[formatterToken](date))
  50. }
  51. })
  52. return {
  53. formatters: formatters,
  54. formattingTokensRegExp: buildFormattingTokensRegExp(formatters)
  55. }
  56. }
  57. function ordinal (number) {
  58. switch (number) {
  59. case 1:
  60. return '1r'
  61. case 2:
  62. return '2n'
  63. case 3:
  64. return '3r'
  65. case 4:
  66. return '4t'
  67. default:
  68. return number + 'è'
  69. }
  70. }
  71. module.exports = buildFormatLocale