index.js 12 KB

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