index.js 3.1 KB

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