index.js 3.4 KB

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