luxon.js 5.4 KB

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