index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. import * as C from './constant';
  2. import en from './locale/en';
  3. import U from './utils';
  4. var L = 'en'; // global locale
  5. var Ls = {}; // global loaded locale
  6. Ls[L] = en;
  7. var isDayjs = function isDayjs(d) {
  8. return d instanceof Dayjs;
  9. }; // eslint-disable-line no-use-before-define
  10. var parseLocale = function parseLocale(preset, object, isLocal) {
  11. var l;
  12. if (!preset) return L;
  13. if (typeof preset === 'string') {
  14. if (Ls[preset]) {
  15. l = preset;
  16. }
  17. if (object) {
  18. Ls[preset] = object;
  19. l = preset;
  20. }
  21. } else {
  22. var name = preset.name;
  23. Ls[name] = preset;
  24. l = name;
  25. }
  26. if (!isLocal && l) L = l;
  27. return l || !isLocal && L;
  28. };
  29. var dayjs = function dayjs(date, c) {
  30. if (isDayjs(date)) {
  31. return date.clone();
  32. } // eslint-disable-next-line no-nested-ternary
  33. var cfg = typeof c === 'object' ? c : {};
  34. cfg.date = date;
  35. cfg.args = arguments; // eslint-disable-line prefer-rest-params
  36. return new Dayjs(cfg); // eslint-disable-line no-use-before-define
  37. };
  38. var wrapper = function wrapper(date, instance) {
  39. return dayjs(date, {
  40. locale: instance.$L,
  41. utc: instance.$u,
  42. x: instance.$x,
  43. $offset: instance.$offset // todo: refactor; do not use this.$offset in you code
  44. });
  45. };
  46. var Utils = U; // for plugin use
  47. Utils.l = parseLocale;
  48. Utils.i = isDayjs;
  49. Utils.w = wrapper;
  50. var parseDate = function parseDate(cfg) {
  51. var date = cfg.date,
  52. utc = cfg.utc;
  53. if (date === null) return new Date(NaN); // null is invalid
  54. if (Utils.u(date)) return new Date(); // today
  55. if (date instanceof Date) return new Date(date);
  56. if (typeof date === 'string' && !/Z$/i.test(date)) {
  57. var d = date.match(C.REGEX_PARSE);
  58. if (d) {
  59. var m = d[2] - 1 || 0;
  60. var ms = (d[7] || '0').substring(0, 3);
  61. if (utc) {
  62. return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms));
  63. }
  64. return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms);
  65. }
  66. }
  67. return new Date(date); // everything else
  68. };
  69. var Dayjs = /*#__PURE__*/function () {
  70. function Dayjs(cfg) {
  71. this.$L = parseLocale(cfg.locale, null, true);
  72. this.parse(cfg); // for plugin
  73. }
  74. var _proto = Dayjs.prototype;
  75. _proto.parse = function parse(cfg) {
  76. this.$d = parseDate(cfg);
  77. this.$x = cfg.x || {};
  78. this.init();
  79. };
  80. _proto.init = function init() {
  81. var $d = this.$d;
  82. this.$y = $d.getFullYear();
  83. this.$M = $d.getMonth();
  84. this.$D = $d.getDate();
  85. this.$W = $d.getDay();
  86. this.$H = $d.getHours();
  87. this.$m = $d.getMinutes();
  88. this.$s = $d.getSeconds();
  89. this.$ms = $d.getMilliseconds();
  90. } // eslint-disable-next-line class-methods-use-this
  91. ;
  92. _proto.$utils = function $utils() {
  93. return Utils;
  94. };
  95. _proto.isValid = function isValid() {
  96. return !(this.$d.toString() === C.INVALID_DATE_STRING);
  97. };
  98. _proto.isSame = function isSame(that, units) {
  99. var other = dayjs(that);
  100. return this.startOf(units) <= other && other <= this.endOf(units);
  101. };
  102. _proto.isAfter = function isAfter(that, units) {
  103. return dayjs(that) < this.startOf(units);
  104. };
  105. _proto.isBefore = function isBefore(that, units) {
  106. return this.endOf(units) < dayjs(that);
  107. };
  108. _proto.$g = function $g(input, get, set) {
  109. if (Utils.u(input)) return this[get];
  110. return this.set(set, input);
  111. };
  112. _proto.unix = function unix() {
  113. return Math.floor(this.valueOf() / 1000);
  114. };
  115. _proto.valueOf = function valueOf() {
  116. // timezone(hour) * 60 * 60 * 1000 => ms
  117. return this.$d.getTime();
  118. };
  119. _proto.startOf = function startOf(units, _startOf) {
  120. var _this = this;
  121. // startOf -> endOf
  122. var isStartOf = !Utils.u(_startOf) ? _startOf : true;
  123. var unit = Utils.p(units);
  124. var instanceFactory = function instanceFactory(d, m) {
  125. var ins = Utils.w(_this.$u ? Date.UTC(_this.$y, m, d) : new Date(_this.$y, m, d), _this);
  126. return isStartOf ? ins : ins.endOf(C.D);
  127. };
  128. var instanceFactorySet = function instanceFactorySet(method, slice) {
  129. var argumentStart = [0, 0, 0, 0];
  130. var argumentEnd = [23, 59, 59, 999];
  131. return Utils.w(_this.toDate()[method].apply( // eslint-disable-line prefer-spread
  132. _this.toDate('s'), (isStartOf ? argumentStart : argumentEnd).slice(slice)), _this);
  133. };
  134. var $W = this.$W,
  135. $M = this.$M,
  136. $D = this.$D;
  137. var utcPad = "set" + (this.$u ? 'UTC' : '');
  138. switch (unit) {
  139. case C.Y:
  140. return isStartOf ? instanceFactory(1, 0) : instanceFactory(31, 11);
  141. case C.M:
  142. return isStartOf ? instanceFactory(1, $M) : instanceFactory(0, $M + 1);
  143. case C.W:
  144. {
  145. var weekStart = this.$locale().weekStart || 0;
  146. var gap = ($W < weekStart ? $W + 7 : $W) - weekStart;
  147. return instanceFactory(isStartOf ? $D - gap : $D + (6 - gap), $M);
  148. }
  149. case C.D:
  150. case C.DATE:
  151. return instanceFactorySet(utcPad + "Hours", 0);
  152. case C.H:
  153. return instanceFactorySet(utcPad + "Minutes", 1);
  154. case C.MIN:
  155. return instanceFactorySet(utcPad + "Seconds", 2);
  156. case C.S:
  157. return instanceFactorySet(utcPad + "Milliseconds", 3);
  158. default:
  159. return this.clone();
  160. }
  161. };
  162. _proto.endOf = function endOf(arg) {
  163. return this.startOf(arg, false);
  164. };
  165. _proto.$set = function $set(units, _int) {
  166. var _C$D$C$DATE$C$M$C$Y$C;
  167. // private set
  168. var unit = Utils.p(units);
  169. var utcPad = "set" + (this.$u ? 'UTC' : '');
  170. var name = (_C$D$C$DATE$C$M$C$Y$C = {}, _C$D$C$DATE$C$M$C$Y$C[C.D] = utcPad + "Date", _C$D$C$DATE$C$M$C$Y$C[C.DATE] = utcPad + "Date", _C$D$C$DATE$C$M$C$Y$C[C.M] = utcPad + "Month", _C$D$C$DATE$C$M$C$Y$C[C.Y] = utcPad + "FullYear", _C$D$C$DATE$C$M$C$Y$C[C.H] = utcPad + "Hours", _C$D$C$DATE$C$M$C$Y$C[C.MIN] = utcPad + "Minutes", _C$D$C$DATE$C$M$C$Y$C[C.S] = utcPad + "Seconds", _C$D$C$DATE$C$M$C$Y$C[C.MS] = utcPad + "Milliseconds", _C$D$C$DATE$C$M$C$Y$C)[unit];
  171. var arg = unit === C.D ? this.$D + (_int - this.$W) : _int;
  172. if (unit === C.M || unit === C.Y) {
  173. // clone is for badMutable plugin
  174. var date = this.clone().set(C.DATE, 1);
  175. date.$d[name](arg);
  176. date.init();
  177. this.$d = date.set(C.DATE, Math.min(this.$D, date.daysInMonth())).$d;
  178. } else if (name) this.$d[name](arg);
  179. this.init();
  180. return this;
  181. };
  182. _proto.set = function set(string, _int2) {
  183. return this.clone().$set(string, _int2);
  184. };
  185. _proto.get = function get(unit) {
  186. return this[Utils.p(unit)]();
  187. };
  188. _proto.add = function add(number, units) {
  189. var _this2 = this,
  190. _C$MIN$C$H$C$S$unit;
  191. number = Number(number); // eslint-disable-line no-param-reassign
  192. var unit = Utils.p(units);
  193. var instanceFactorySet = function instanceFactorySet(n) {
  194. var d = dayjs(_this2);
  195. return Utils.w(d.date(d.date() + Math.round(n * number)), _this2);
  196. };
  197. if (unit === C.M) {
  198. return this.set(C.M, this.$M + number);
  199. }
  200. if (unit === C.Y) {
  201. return this.set(C.Y, this.$y + number);
  202. }
  203. if (unit === C.D) {
  204. return instanceFactorySet(1);
  205. }
  206. if (unit === C.W) {
  207. return instanceFactorySet(7);
  208. }
  209. var step = (_C$MIN$C$H$C$S$unit = {}, _C$MIN$C$H$C$S$unit[C.MIN] = C.MILLISECONDS_A_MINUTE, _C$MIN$C$H$C$S$unit[C.H] = C.MILLISECONDS_A_HOUR, _C$MIN$C$H$C$S$unit[C.S] = C.MILLISECONDS_A_SECOND, _C$MIN$C$H$C$S$unit)[unit] || 1; // ms
  210. var nextTimeStamp = this.$d.getTime() + number * step;
  211. return Utils.w(nextTimeStamp, this);
  212. };
  213. _proto.subtract = function subtract(number, string) {
  214. return this.add(number * -1, string);
  215. };
  216. _proto.format = function format(formatStr) {
  217. var _this3 = this;
  218. if (!this.isValid()) return C.INVALID_DATE_STRING;
  219. var str = formatStr || C.FORMAT_DEFAULT;
  220. var zoneStr = Utils.z(this);
  221. var locale = this.$locale();
  222. var $H = this.$H,
  223. $m = this.$m,
  224. $M = this.$M;
  225. var weekdays = locale.weekdays,
  226. months = locale.months,
  227. meridiem = locale.meridiem;
  228. var getShort = function getShort(arr, index, full, length) {
  229. return arr && (arr[index] || arr(_this3, str)) || full[index].substr(0, length);
  230. };
  231. var get$H = function get$H(num) {
  232. return Utils.s($H % 12 || 12, num, '0');
  233. };
  234. var meridiemFunc = meridiem || function (hour, minute, isLowercase) {
  235. var m = hour < 12 ? 'AM' : 'PM';
  236. return isLowercase ? m.toLowerCase() : m;
  237. };
  238. var matches = {
  239. YY: String(this.$y).slice(-2),
  240. YYYY: this.$y,
  241. M: $M + 1,
  242. MM: Utils.s($M + 1, 2, '0'),
  243. MMM: getShort(locale.monthsShort, $M, months, 3),
  244. MMMM: getShort(months, $M),
  245. D: this.$D,
  246. DD: Utils.s(this.$D, 2, '0'),
  247. d: String(this.$W),
  248. dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2),
  249. ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3),
  250. dddd: weekdays[this.$W],
  251. H: String($H),
  252. HH: Utils.s($H, 2, '0'),
  253. h: get$H(1),
  254. hh: get$H(2),
  255. a: meridiemFunc($H, $m, true),
  256. A: meridiemFunc($H, $m, false),
  257. m: String($m),
  258. mm: Utils.s($m, 2, '0'),
  259. s: String(this.$s),
  260. ss: Utils.s(this.$s, 2, '0'),
  261. SSS: Utils.s(this.$ms, 3, '0'),
  262. Z: zoneStr // 'ZZ' logic below
  263. };
  264. return str.replace(C.REGEX_FORMAT, function (match, $1) {
  265. return $1 || matches[match] || zoneStr.replace(':', '');
  266. }); // 'ZZ'
  267. };
  268. _proto.utcOffset = function utcOffset() {
  269. // Because a bug at FF24, we're rounding the timezone offset around 15 minutes
  270. // https://github.com/moment/moment/pull/1871
  271. return -Math.round(this.$d.getTimezoneOffset() / 15) * 15;
  272. };
  273. _proto.diff = function diff(input, units, _float) {
  274. var _C$Y$C$M$C$Q$C$W$C$D$;
  275. var unit = Utils.p(units);
  276. var that = dayjs(input);
  277. var zoneDelta = (that.utcOffset() - this.utcOffset()) * C.MILLISECONDS_A_MINUTE;
  278. var diff = this - that;
  279. var result = Utils.m(this, that);
  280. result = (_C$Y$C$M$C$Q$C$W$C$D$ = {}, _C$Y$C$M$C$Q$C$W$C$D$[C.Y] = result / 12, _C$Y$C$M$C$Q$C$W$C$D$[C.M] = result, _C$Y$C$M$C$Q$C$W$C$D$[C.Q] = result / 3, _C$Y$C$M$C$Q$C$W$C$D$[C.W] = (diff - zoneDelta) / C.MILLISECONDS_A_WEEK, _C$Y$C$M$C$Q$C$W$C$D$[C.D] = (diff - zoneDelta) / C.MILLISECONDS_A_DAY, _C$Y$C$M$C$Q$C$W$C$D$[C.H] = diff / C.MILLISECONDS_A_HOUR, _C$Y$C$M$C$Q$C$W$C$D$[C.MIN] = diff / C.MILLISECONDS_A_MINUTE, _C$Y$C$M$C$Q$C$W$C$D$[C.S] = diff / C.MILLISECONDS_A_SECOND, _C$Y$C$M$C$Q$C$W$C$D$)[unit] || diff; // milliseconds
  281. return _float ? result : Utils.a(result);
  282. };
  283. _proto.daysInMonth = function daysInMonth() {
  284. return this.endOf(C.M).$D;
  285. };
  286. _proto.$locale = function $locale() {
  287. // get locale object
  288. return Ls[this.$L];
  289. };
  290. _proto.locale = function locale(preset, object) {
  291. if (!preset) return this.$L;
  292. var that = this.clone();
  293. var nextLocaleName = parseLocale(preset, object, true);
  294. if (nextLocaleName) that.$L = nextLocaleName;
  295. return that;
  296. };
  297. _proto.clone = function clone() {
  298. return Utils.w(this.$d, this);
  299. };
  300. _proto.toDate = function toDate() {
  301. return new Date(this.valueOf());
  302. };
  303. _proto.toJSON = function toJSON() {
  304. return this.isValid() ? this.toISOString() : null;
  305. };
  306. _proto.toISOString = function toISOString() {
  307. // ie 8 return
  308. // new Dayjs(this.valueOf() + this.$d.getTimezoneOffset() * 60000)
  309. // .format('YYYY-MM-DDTHH:mm:ss.SSS[Z]')
  310. return this.$d.toISOString();
  311. };
  312. _proto.toString = function toString() {
  313. return this.$d.toUTCString();
  314. };
  315. return Dayjs;
  316. }();
  317. var proto = Dayjs.prototype;
  318. dayjs.prototype = proto;
  319. [['$ms', C.MS], ['$s', C.S], ['$m', C.MIN], ['$H', C.H], ['$W', C.D], ['$M', C.M], ['$y', C.Y], ['$D', C.DATE]].forEach(function (g) {
  320. proto[g[1]] = function (input) {
  321. return this.$g(input, g[0], g[1]);
  322. };
  323. });
  324. dayjs.extend = function (plugin, option) {
  325. if (!plugin.$i) {
  326. // install plugin only once
  327. plugin(option, Dayjs, dayjs);
  328. plugin.$i = true;
  329. }
  330. return dayjs;
  331. };
  332. dayjs.locale = parseLocale;
  333. dayjs.isDayjs = isDayjs;
  334. dayjs.unix = function (timestamp) {
  335. return dayjs(timestamp * 1e3);
  336. };
  337. dayjs.en = Ls[L];
  338. dayjs.Ls = Ls;
  339. dayjs.p = {};
  340. export default dayjs;