luxon.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { DateTime, Info } from 'luxon';
  2. var weekDayFormatMap = {
  3. zh_CN: 'narrow',
  4. zh_TW: 'narrow'
  5. };
  6. var weekDayLengthMap = {
  7. en_US: 2,
  8. en_GB: 2
  9. };
  10. /**
  11. * Normalizes part of a moment format string that should
  12. * not be escaped to a luxon compatible format string.
  13. *
  14. * @param part string
  15. * @returns string
  16. */
  17. var normalizeFormatPart = function normalizeFormatPart(part) {
  18. return part.replace(/Y/g, 'y').replace(/D/g, 'd').replace(/gg/g, 'kk').replace(/Q/g, 'q').replace(/([Ww])o/g, 'WW').replace(/A/g, 'a');
  19. };
  20. /**
  21. * Normalizes a moment compatible format string to a luxon compatible format string
  22. *
  23. * @param format string
  24. * @returns string
  25. */
  26. var normalizeFormat = function normalizeFormat(format) {
  27. return format
  28. // moment escapes strings contained in brackets
  29. .split(/[[\]]/).map(function (part, index) {
  30. var shouldEscape = index % 2 > 0;
  31. return shouldEscape ? part : normalizeFormatPart(part);
  32. })
  33. // luxon escapes strings contained in single quotes
  34. .join("'");
  35. };
  36. /**
  37. * Normalizes language tags used to luxon compatible
  38. * language tags by replacing underscores with hyphen-minus.
  39. *
  40. * @param locale string
  41. * @returns string
  42. */
  43. var normalizeLocale = function normalizeLocale(locale) {
  44. return locale.replace(/_/g, '-');
  45. };
  46. var generateConfig = {
  47. // get
  48. getNow: function getNow() {
  49. /**
  50. * The current time that can respond to tz settings is required. like `dayjs().tz()`.
  51. * @see: https://github.com/ant-design/ant-design/issues/51282
  52. * https://github.com/react-component/picker/pull/878
  53. */
  54. return DateTime.now();
  55. },
  56. getFixedDate: function getFixedDate(string) {
  57. return DateTime.fromFormat(string, 'yyyy-MM-dd');
  58. },
  59. getEndDate: function getEndDate(date) {
  60. return date.endOf('month');
  61. },
  62. getWeekDay: function getWeekDay(date) {
  63. return date.weekday;
  64. },
  65. getYear: function getYear(date) {
  66. return date.year;
  67. },
  68. getMonth: function getMonth(date) {
  69. return date.month - 1;
  70. },
  71. // getMonth should return 0-11, luxon month returns 1-12
  72. getDate: function getDate(date) {
  73. return date.day;
  74. },
  75. getHour: function getHour(date) {
  76. return date.hour;
  77. },
  78. getMinute: function getMinute(date) {
  79. return date.minute;
  80. },
  81. getSecond: function getSecond(date) {
  82. return date.second;
  83. },
  84. getMillisecond: function getMillisecond(date) {
  85. return date.millisecond;
  86. },
  87. // set
  88. addYear: function addYear(date, diff) {
  89. return date.plus({
  90. year: diff
  91. });
  92. },
  93. addMonth: function addMonth(date, diff) {
  94. return date.plus({
  95. month: diff
  96. });
  97. },
  98. addDate: function addDate(date, diff) {
  99. return date.plus({
  100. day: diff
  101. });
  102. },
  103. setYear: function setYear(date, year) {
  104. return date.set({
  105. year: year
  106. });
  107. },
  108. setMonth: function setMonth(date, month) {
  109. return date.set({
  110. month: month + 1
  111. });
  112. },
  113. // setMonth month argument is 0-11, luxon months are 1-12
  114. setDate: function setDate(date, day) {
  115. return date.set({
  116. day: day
  117. });
  118. },
  119. setHour: function setHour(date, hour) {
  120. return date.set({
  121. hour: hour
  122. });
  123. },
  124. setMinute: function setMinute(date, minute) {
  125. return date.set({
  126. minute: minute
  127. });
  128. },
  129. setSecond: function setSecond(date, second) {
  130. return date.set({
  131. second: second
  132. });
  133. },
  134. setMillisecond: function setMillisecond(date, milliseconds) {
  135. return date.set({
  136. millisecond: milliseconds
  137. });
  138. },
  139. // Compare
  140. isAfter: function isAfter(date1, date2) {
  141. return date1 > date2;
  142. },
  143. isValidate: function isValidate(date) {
  144. return date.isValid;
  145. },
  146. locale: {
  147. getWeekFirstDate: function getWeekFirstDate(locale, date) {
  148. return date.setLocale(normalizeLocale(locale)).startOf('week');
  149. },
  150. getWeekFirstDay: function getWeekFirstDay(locale) {
  151. return DateTime.local().setLocale(normalizeLocale(locale)).startOf('week').weekday;
  152. },
  153. getWeek: function getWeek(locale, date) {
  154. return date.setLocale(normalizeLocale(locale)).weekNumber;
  155. },
  156. getShortWeekDays: function getShortWeekDays(locale) {
  157. var weekdays = Info.weekdays(weekDayFormatMap[locale] || 'short', {
  158. locale: normalizeLocale(locale)
  159. });
  160. var shifted = weekdays.map(function (weekday) {
  161. return weekday.slice(0, weekDayLengthMap[locale]);
  162. });
  163. // getShortWeekDays should return weekday labels starting from Sunday.
  164. // luxon returns them starting from Monday, so we have to shift the results.
  165. shifted.unshift(shifted.pop());
  166. return shifted;
  167. },
  168. getShortMonths: function getShortMonths(locale) {
  169. return Info.months('short', {
  170. locale: normalizeLocale(locale)
  171. });
  172. },
  173. format: function format(locale, date, _format) {
  174. if (!date || !date.isValid) {
  175. return null;
  176. }
  177. return date.setLocale(normalizeLocale(locale)).toFormat(normalizeFormat(_format));
  178. },
  179. parse: function parse(locale, text, formats) {
  180. for (var i = 0; i < formats.length; i += 1) {
  181. var normalizedFormat = normalizeFormat(formats[i]);
  182. var date = DateTime.fromFormat(text, normalizedFormat, {
  183. locale: normalizeLocale(locale)
  184. });
  185. if (date.isValid) {
  186. return date;
  187. }
  188. }
  189. return null;
  190. }
  191. }
  192. };
  193. export default generateConfig;