index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { MILLISECONDS_A_MINUTE, MIN } from '../../constant';
  2. var REGEX_VALID_OFFSET_FORMAT = /[+-]\d\d(?::?\d\d)?/g;
  3. var REGEX_OFFSET_HOURS_MINUTES_FORMAT = /([+-]|\d\d)/g;
  4. function offsetFromString(value) {
  5. if (value === void 0) {
  6. value = '';
  7. }
  8. var offset = value.match(REGEX_VALID_OFFSET_FORMAT);
  9. if (!offset) {
  10. return null;
  11. }
  12. var _ref = ("" + offset[0]).match(REGEX_OFFSET_HOURS_MINUTES_FORMAT) || ['-', 0, 0],
  13. indicator = _ref[0],
  14. hoursOffset = _ref[1],
  15. minutesOffset = _ref[2];
  16. var totalOffsetInMinutes = +hoursOffset * 60 + +minutesOffset;
  17. if (totalOffsetInMinutes === 0) {
  18. return 0;
  19. }
  20. return indicator === '+' ? totalOffsetInMinutes : -totalOffsetInMinutes;
  21. }
  22. export default (function (option, Dayjs, dayjs) {
  23. var proto = Dayjs.prototype;
  24. dayjs.utc = function (date) {
  25. var cfg = {
  26. date: date,
  27. utc: true,
  28. args: arguments
  29. }; // eslint-disable-line prefer-rest-params
  30. return new Dayjs(cfg); // eslint-disable-line no-use-before-define
  31. };
  32. proto.utc = function (keepLocalTime) {
  33. var ins = dayjs(this.toDate(), {
  34. locale: this.$L,
  35. utc: true
  36. });
  37. if (keepLocalTime) {
  38. return ins.add(this.utcOffset(), MIN);
  39. }
  40. return ins;
  41. };
  42. proto.local = function () {
  43. return dayjs(this.toDate(), {
  44. locale: this.$L,
  45. utc: false
  46. });
  47. };
  48. var oldParse = proto.parse;
  49. proto.parse = function (cfg) {
  50. if (cfg.utc) {
  51. this.$u = true;
  52. }
  53. if (!this.$utils().u(cfg.$offset)) {
  54. this.$offset = cfg.$offset;
  55. }
  56. oldParse.call(this, cfg);
  57. };
  58. var oldInit = proto.init;
  59. proto.init = function () {
  60. if (this.$u) {
  61. var $d = this.$d;
  62. this.$y = $d.getUTCFullYear();
  63. this.$M = $d.getUTCMonth();
  64. this.$D = $d.getUTCDate();
  65. this.$W = $d.getUTCDay();
  66. this.$H = $d.getUTCHours();
  67. this.$m = $d.getUTCMinutes();
  68. this.$s = $d.getUTCSeconds();
  69. this.$ms = $d.getUTCMilliseconds();
  70. } else {
  71. oldInit.call(this);
  72. }
  73. };
  74. var oldUtcOffset = proto.utcOffset;
  75. proto.utcOffset = function (input, keepLocalTime) {
  76. var _this$$utils = this.$utils(),
  77. u = _this$$utils.u;
  78. if (u(input)) {
  79. if (this.$u) {
  80. return 0;
  81. }
  82. if (!u(this.$offset)) {
  83. return this.$offset;
  84. }
  85. return oldUtcOffset.call(this);
  86. }
  87. if (typeof input === 'string') {
  88. input = offsetFromString(input);
  89. if (input === null) {
  90. return this;
  91. }
  92. }
  93. var offset = Math.abs(input) <= 16 ? input * 60 : input;
  94. var ins = this;
  95. if (keepLocalTime) {
  96. ins.$offset = offset;
  97. ins.$u = input === 0;
  98. return ins;
  99. }
  100. if (input !== 0) {
  101. var localTimezoneOffset = this.$u ? this.toDate().getTimezoneOffset() : -1 * this.utcOffset();
  102. ins = this.local().add(offset + localTimezoneOffset, MIN);
  103. ins.$offset = offset;
  104. ins.$x.$localOffset = localTimezoneOffset;
  105. } else {
  106. ins = this.utc();
  107. }
  108. return ins;
  109. };
  110. var oldFormat = proto.format;
  111. var UTC_FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ss[Z]';
  112. proto.format = function (formatStr) {
  113. var str = formatStr || (this.$u ? UTC_FORMAT_DEFAULT : '');
  114. return oldFormat.call(this, str);
  115. };
  116. proto.valueOf = function () {
  117. var addedOffset = !this.$utils().u(this.$offset) ? this.$offset + (this.$x.$localOffset || this.$d.getTimezoneOffset()) : 0;
  118. return this.$d.valueOf() - addedOffset * MILLISECONDS_A_MINUTE;
  119. };
  120. proto.isUTC = function () {
  121. return !!this.$u;
  122. };
  123. proto.toISOString = function () {
  124. return this.toDate().toISOString();
  125. };
  126. proto.toString = function () {
  127. return this.toDate().toUTCString();
  128. };
  129. var oldToDate = proto.toDate;
  130. proto.toDate = function (type) {
  131. if (type === 's' && this.$offset) {
  132. return dayjs(this.format('YYYY-MM-DD HH:mm:ss:SSS')).toDate();
  133. }
  134. return oldToDate.call(this);
  135. };
  136. var oldDiff = proto.diff;
  137. proto.diff = function (input, units, _float) {
  138. if (input && this.$u === input.$u) {
  139. return oldDiff.call(this, input, units, _float);
  140. }
  141. var localThis = this.local();
  142. var localInput = dayjs(input).local();
  143. return oldDiff.call(localThis, localInput, units, _float);
  144. };
  145. });