index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. var buildFormattingTokensRegExp = require('../../_lib/build_formatting_tokens_reg_exp/index.js')
  2. function buildFormatLocale () {
  3. var months3char = ['ene', 'feb', 'mar', 'abr', 'may', 'jun', 'jul', 'ago', 'sep', 'oct', 'nov', 'dic']
  4. var monthsFull = ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre']
  5. var weekdays2char = ['do', 'lu', 'ma', 'mi', 'ju', 'vi', 'sa']
  6. var weekdays3char = ['dom', 'lun', 'mar', 'mié', 'jue', 'vie', 'sáb']
  7. var weekdaysFull = ['domingo', 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado']
  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. return number + 'º'
  59. }
  60. module.exports = buildFormatLocale